My Pi Description

My Experiences With the Raspberry Pi -- Tracking My Learning -- My Pi Projects

Friday, June 14, 2013

A Raspberry Pi Thermometer - Software Introduction

The best place to start with the DS18B20 1-Wire temperature sensor is Simon Monk's "Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing".
If you have a recent Raspbian or Occidentals distribution, support for the the 1-Wire interface and the DS18B20 temperature sensor is built in. If you don't use a distro that has this support, you would probably have to write your own. Because of the timing requirements, I don't think you could write it in Python, but would have to write it in C/C++. As it is, using the Raspberry Pi's GPIO and the 1-Wire/DS18B20 software modules almost works. I have a couple of python scripts that make temperature measurements about two seconds apart and report the result. After a while, maybe one hour, eight hours, or 24 hours, the program just quits. I spent many hours trying to solve that problem. This project would be better implemented on a microcontroller like the ATMega on the Arduino and the Gertboard.
If you want to know more about the DS18B20 and 1 Wire interfaces you need the "DS18B20 Programmable Resolution 1-Wire Digital Thermometer" data sheet from Maxim Integrated. To get the datasheet from this blog, I could not make the link work properly. It always redirected my to the wrong URL so just do a Google search on DS18B20. Look for URL: It probably will be the first entry. FYI: The datasheet for the device from Dallas Semiconductor is actually the same datasheet.
From the datasheet, you will find that the device has programmable resolution, the ability to set low and high temperature limits, and can report whenever the temperature is outside of those limits. Quite a lot of capability for a device about the size of a pea. You may also connect as many temperature sensors, and other 1-Wire devices, as you like onto that one wire. So, how does the computer decide what device to communicate with? Each device has a unique 64 bit code programmed into non-volatile memory. No matter when or where the device was made, or what type of 1-Wire device you have, it's code will not be repeated on any other device. It's like Mac addresses for computer devices.
The 1-Wire software modules are not automatically loaded upon Raspberry Pi start-up. You use the Linux command "modprobe" to load them. The modules are "w1_gpio" and "w_therm". Loading these two modules also load the "wire" module, which, in turn, loads the "cn" module. As far as I can find out, the only thing you can do with these software modules is to read the temperatures from the devices. There is no documentation that I have been able to find (I posed the question on the Raspberry Pi and Adafruit forums) that tells how you can change the resolution, or set lower and upper temperature limits, and report temperatures out of range.
I think I have given enough background, so rather than me going on and on about how you use the 1-Wire modules, I suggest you look at Simon Monk's lesson. I'll have some examples of use of the temperature sensors in my following blogs.


  1. Marty, did you ever figure out how to change the resolution? I really want to do my entire project with my Pi, but the 750ms sampling with 12-bits is just too slow for my PID controller. I'll be forced to use an Arduino just to get a 9- or 10-bit temperature, and then send that data back to the Pi.

    1. It would be better to control the device from an Arduino where you get predictable timing.

      The Arduino IDE comes with a one-wire library and a Dallas temperature library. The Dallas library contains a function to change the resolution.

      I thought it might be fun to write my own one-wire interface for my DS18B20. My latest blog entry deals with accessing the ATmega328 registers on my Gertboard.

      This is in preparation for my one-wire project. Stay tuned