Debugging DS18B20 Sensor on a Leonardo

Hello All,

I wanted to share my experience using the DS18B20 sensor, as it took me a few hours to actually get it up and running. I hope this helps someone that is struggling like I was.

First, the code. The Arduino Temperature Control Library, the simple example does work. Pay attention to the #define ONE_WIRE_BUS 2 line near the top and make sure the data line for the sensor is on that pin. I ended up using pin 10 on my leonardo.

Second, this sensor is able to run in powered or parasitic mode. The sensor I have from here is attached to a 3m cable, and it has red, yellow, black wires, see image below

A 3m cable should not cause problems, but I found that the sensor only works in parasitic mode. I found this tip from 1-wire with long cables - #3 by hwestbrook - Hardware - Particle.

So, how do you wire the sensor in parasitic mode? It is simple, and it is in the available datasheet. I will provide a wiring diagram below that is hopefully very clear on what to do. You connect red + black to ground, yellow to your data pin, and also yellow to a 4.7Kohm resistor that connects to your 5v.

I did not have a 4.7Kohm resistor on hand, so I ended up using two 3.9Kohm in series, and it worked out fine.

Third, choosing your data pin. I chose to use pin 10 on the leonardo because it is a pwm pin. I have no idea if this actually makes a difference, but was something that I ended up keeping.

Lastly, even given all of the above and lots of trial and error and double checking, I still could not get it working. I had a few other things plugged into the board that I was not using, so I ended up unplugging everything and starting over. Once I plugged in just the temperature sensor, and triple checked that I had the data (yellow) line connected to the right pin on my board I was able to read temperatures.

I hope this is helpful for the next person. There are alot of posts about this sensor, but they are all fairly old. So for anyone looking to use this sensor, it is possible, and still works. I hope you found this helpful!

1 Like

You're dead right about that, which goes some way to explain why the sensor comes supplied with a 3m cable. Users are normally well-advised to avoid parasitic power like the plague that it is, and I think that applies as much to a Leonardo as it does to any other Arduino.

1 Like