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.
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?
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 ‘DS1820 - Library for reading temperature from DS1820, DS18… | Mbed’ 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.
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.
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.