DS18B20 Temperature Sensor

This code implements the Dallas Semi 1-wire serial protocol to program and read the DS18B20 temperature sensor.

At the moment it's pretty simple, and only works with a simple sensor.

It's what I'll call C+ (rather than C++ - that'll be next).


15 Feb 2010

Could You share code ?



16 Feb 2010

I have can you not access them?

17 Feb 2010

Oh , I'm blind a little, sorry.

Of course I have access them. Thanks for sharing code.




10 May 2010

Can you post the circuit diagram. Is it just on p9 and p10? Which way are the pins on the ds18b20 connected to 9 and 10? Thanks. mbed is a cool device, but the documentation is sure thin.



10 May 2010

Hi, You have to connect only one pin, no matter which. It's middle leg on DS. All information here : http://datasheets.maxim-ic.com/en/ds/DS18B20.pdf






10 May 2010

I've now published  my dirty onewire code, which works ok with both DS18B20 and DS18S20 sensors and with external or parasite power.




10 May 2010

Thanks......got it. Sorry, I should rtfm.



05 Apr 2011
Hi Niall, I've trying to use your code on my DS18B20, and I can read the Family code = 0x28 Serial Number: 00:00:01:B7 CRC 0x2F But when reading Temperatures, i always get 85.0C, wich is the powerup temperature... What could be wrong? I connected Vdd to mbed VU = 5v, any other command seems to work except for CONVERT, Looking at your code I can't find any flaws there, I try writing to Scratchpad, but same problem remains... Any ideas?
05 Apr 2011

When You connect DS18B20 in parasite power You need more time to convert (about 750ms) but as I remeber drivers don't work correctly. You have to try connect with power ( 3 linnes ) and check correct readings, should work.

05 Apr 2011


I've connected my DS1820 this way and it works:

DS1820/DS18S20 Temperature-sensor:

1 -> GND

2 (DQ) -> p23 and Pullup(4k7) -> 3V3(pin40)

3 -> 3V3(pin40)

PS: I use this library http://mbed.org/users/snatch59/programs/OneWireCRC/gpdz56

Maybe the type is the difference DS1820, DS18S20, DS18B20

05 Apr 2011 . Edited: 05 Apr 2011
The difference was, I had it connected to VU, 5V, now I change it to 3.3V,but still the same. I've tried a bunch of other libraries but end up with the same result. There's something wrong with the command CONVERSION but it's not timing. I believe my sensor is useless, I'm going to buy a new one and I'm sure it will work then. I even try changing Pull-up resistor from 4k7 to 3k3 But it didn't work.
01 Jan 2012

The code works fine on my shiny new LPC11U24 mbed - only change necessary was to replace the the old mbed library with the latest one.

See: http://mbed.org/cookbook/Compiler-Error-35

I found that an external pullup was necessary - I used 4k7, per the DS18B20 datasheet.


02 May 2013

Thank you very much. Works a treat.

Now I will have to study your code and learn something!

My ultimate aim is to generate a simple web server to access the reading from anywhere.

14 May 2013

Niall Cooling, sorry but where did you put the code? i can't seem to find it

13 Dec 2013 . Edited: 13 Dec 2013

Hi All


Using OneWire Driver test program.

Having connected the DS18b20 with a 4.7k pullup resistor between the Vdd and DQ legs....all i get is "short circuit".


When I unplug the sensor and all the wires ... I get "short circuit".

the mbed is pushed into a bread board across a division.

Actually, even when not connected to anything -- no sensor, and not in the breadboard OneWire Driver test program

gives the same message -- "short circuit"



Thanks for your help.



05 Mar 2014 . Edited: 05 Mar 2014


You need to log in to post a comment