Arm Mbed and Pelion Device Management support forum

STM32 F103C8T6 (blue pill) and DS18B20 temperature sensor

Hello.

I want to use STM32 F103C8T6 (blue pill) to read the temperature from a few DS18B20 temperature sensors.
As I understand Mbed has some performance issues with IO operations (switching pin modes) on my target board so there are no working libraries.

I tested the code suggested by Zoltan Hudak

DigitalInOut    line(PA_10);
Timer           timer;

int main()
{
    line.output();
    timer.start();
    line.input();
    printf("delay1 = %dus\r\n", timer.read_us());
    timer.reset();
    line.read();
    printf("delay2 = %dus\r\n", timer.read_us());
 
    while (true) { }
}

And got:
delay1 = 11us
delay2 = 8us

Any suggestions on my issue? Should I abandon the idea of using a cheap STM32 board?

When is comes to changing pin mode the NUCLEO-L073RZ seems to be very slow. But the STM32 F103C8T6 should be fast enough to drive a DS18B20 sensor. Have you tried this example?

1 Like

I wasn’t able to import this example into Mbed Studio so I just copied the code into my project and imported the required library, as a result: “No DS1820 sensor found!”

Just to clarify I haven’t found F103C8T6 target thats why I use NUCLEO-F103RB.

DS18B20’s work fine on the 103, but you will need to adjust the one-wire timing its really critical and every target is different.
With Erik’s ‘https://os.mbed.com/users/Sissors/code/DS1820/’ and up-to date Mbed-2.
Using the ‘Black Pill’ 103 board (much the same as the blue version), I have 5 sensors, some on 15 meter cables, connected to one pin with a single 4k7R pull up resistor. Be careful when using multiple sensors on one pin, you may get the odd drop out, randomly I get 3-4 readings back and 1 or 2 at zero. I haven’t checked why because I intend to use separate pins for each sensor.

In the DS1820.cpp file you will see…

bool DS1820::onewire_bit_in(DigitalInOut *pin) {
   bool answer;
   ONEWIRE_OUTPUT(pin);
   pin->write(0);
   //ONEWIRE_DELAY_US(3);    // reduce this if slow GPIO switching, can be zero!!
   ONEWIRE_INPUT(pin);
   ONEWIRE_DELAY_US(5);      // DXP modified from 5
   answer = pin->read();
   ONEWIRE_DELAY_US(45);     // DXP modified from 50
   return answer;

}

The problem is you need to be able to switch the data pin to be ready to ‘read’ within 15uS after dropping the data pin low. You already have an 11uS MCU delay to do this so in theory you need no further delay. I’ve just tried by removing the delay completely and it works fine. 4uS is the maximum delay I have tried that works on the 103, I generally leave at 3uS.

Zoltan’s library did work for me too but on another project, can’t remember where the timing settings are though.

The online mbed-2 is more economic with Flash at the moment, I would use that for now. Studio bare metal does consume more Flash/RAM.

1 Like

Okay, at least now I understand the problem a bit deeper, thank you.
Tried to play with delays (using online mbed 2):

bool DS1820::onewire_bit_in(DigitalInOut *pin) {
    bool answer;
    ONEWIRE_OUTPUT(pin);
    pin->write(0);
    ONEWIRE_DELAY_US(3); // tried: none, 2, 3, 4, 5                // DXP modofied from 5
    ONEWIRE_INPUT(pin);
    ONEWIRE_DELAY_US(3); // tried: none, 2, 3, 4, 5               // DXP modified from 5
    answer = pin->read();
    ONEWIRE_DELAY_US(50); // tried: 40, 45, 40               // DXP modified from 50
    return answer;
}

However the resalt is constant:

Found 1 device(s)
Device 0 returns nanoC

In general, Mbed is something relatively new to me, so I still have to learn a lot. I started to use it as an Arduino alternative, managed to connect Blue Pill to MQTT broker without significant efforts however stuck on DS18B20.

It works.
A stupid mistake, as always… accidentally connected vcc to 3VB pin instead of 3.3.

Thank you, everyone.

If you want 100% avoid all problems and anyway want to use the DS18B20 you can also use a 1-Wire to I2C bridge DS2484 or a module with it . And then use a library from Maxim Integrated OneWire . Ofc it is not so simple as connected the sensor directly to a pin of the board. It is optional.

BR, Jan

1 Like

Interesting. I haven’t heard about DS2484 before.
I’ll start with a direct connection to check how reliable this approach is.

Thank you for the additional information.