Saturday, December 8, 2012

Raspberry Pi - Dallas Semiconductor Temperature Sensors

Today I set out to follow Edward O'regan's procedure for connecting a Dallas Semiconductor DS18B20 temperature sensor to the Raspberry Pi development board.

The first problem I ran into was with the sort of IDE cable I was using. If you look close you can see it's an 80-pin IDE connector. 

The problem with an 80-pin IDE connector as it turns out, is that it not only interleaves a ground between each pin on the ribbon cable, it also grounds specific pins within the socket. In the picture above we can see the ground plane that I pulled out of the socket for illustration purposes. we can see that at various points along the plane there are sharp protuding pins that make contact with the various pins in the socket. this is BAD for the raspberry pi as I found out (GPIO 4 worked but when I rebooted the pi, it locked up).

But, this wasn't all for nothing. Since I opened up the socket while troubleshooting, I noticed that the pins stick out far enough to be soldered to. Next I had to check to see if the plastic could take the high temperatures incurred during soldering. They did, very nicely. I heated them all at the same time for about 20 seconds and the plastic didn't give way whatsoever.

So I took a 6-foot hunk of cat-5e and soldered Brown, Orange, and Green to socket pins corresponding to Ground, +3.3v, and GPIO-4 (these are the pins needed to interface with a 1-wire device such as the DS18b20 temperature sensor).

Everything worked great on the first try. I added a second DS18B20 in parallel with the first and the second one was detected within a few seconds. I pulled temperature readings from both sensors. The fastest I could poll the sensors was bout 1-second per check. Not too fast, but not bad all things considering.

Next I tried configuring the sensor for "1-wire" or "parasitic" operation by shorting pins 1-3 to ground and leaving GPIO-4 connected to pin2 for data.

At first  it seemed to work, but upon closer inspection, both sensors returned 85-degrees-celcius every time regardless of the actual temperature. The strange thing is that the checksum was correct each time.

Since I don't mind stringing a third wire to each temp sensor I went forward with the 3-wire configuration.

Next I made a more permanent cable.

At the base of the wire I have the first of two sensors. And at the end of the cable is the second sensor.

close-up of both sensors along with the header.

1-liner used for testing

root@raspberrypi:/sys/bus/w1/devices# while : ; do for I in 28-* ; do echo $I ; cat $I/w1_slave ; done; done

Sample output

59 01 4b 46 7f ff 07 10 a2 : crc=a2 YES
59 01 4b 46 7f ff 07 10 a2 t=21562
50 05 4b 46 7f ff 0c 10 1c : crc=1c YES
50 05 4b 46 7f ff 0c 10 1c t=85000

Remaining Tasks

All that remains is to write a script to integrate the temperature readings with snmp, and write some scripts to collect and store the information. 


  1. Any idea why parasitic power does not work? I accidentally purchased the PAR model of the sensor

  2. Hi Kyle - I have a hunch it's not working either because of the timing or logic used by w1-therm kernel-module code. Yesterday I read something that led me to this conclusion, although today I can't seem to find the article. The kernel module is "bit banging" the 1-wire protocol so there's surely some wiggle room for things like leaving the bus powered up for a longer period so that the sensor has time to charge its internal capacitor. Also with parasitic power the voltage in the response signal may be somewhat degraded. I wish I had a scope to actually check. It would probably be quite clear what is going on with the use of a scope. One thing that was suggested in the sensor's datasheet is increasing the power to the bus, but I didn't try this because I think the GPIO pins on the Raspberry Pi can only handle up to 3.3v.

  3. Hey that's really a great post and a wonderful description out here, I really like the way things are being executed and discussed here.
    Temperature Sensors