DS18B20 Temperature Sensor with Raspberry Pi

28Jan13

After bringing my Nanode based temperature sensor back to life I thought about some other projects that I might do. One was simply to add an external sensor to the Nanode project, and another was to turn my slow cooker into a sous vide water bath (along the lines of ‘Sous Vader‘). Those projects would need a temperature sensor that could withstand weather/immersion, and the waterproof DS18B20 fits the bill (such as this on eBay).

The two sensors that I ordered arrived today, and since I’ve got a Raspberry Pi on my desk I thought I’d try them out with that first. I quick hunt for DS18B20, onewire and 1wire in the forums suggested that things would be very easy (e.g. too trivial for anybody to explain) or very hard (e.g. requiring ninja kernel hacking skills). As I didn’t come across any code samples I was starting to fear the latter, but luckily it turned out to be the former. A bit more digging around led me to this tutorial by Matthew Kirk on the University of Cambridge site. Things have clearly moved on since he wrote it though, and I didn’t need to patch my (reasonably up to date) Raspbian to get w1-gpio and w1-therm support. It was simply a question of connecting the three wires black-GND red-3.3v and white-GPIO4 and I was all set (I didn’t use a pull up resistor).

With the appropriate modules installed (sudo modprobe w1-gpio && sudo modprobe w1-therm) the temperature can be read with:

cat /sys/bus/w1/devices/serial_number/w1_slave

The serial_number is something like 28-000003ea00b4 and the one on my desk is presently reading something like this:

48 01 4b 46 7f ff 08 10 ad : crc=ad YES
48 01 4b 46 7f ff 08 10 ad t=20500

So anything that can read from a file can read the temperature, and the crucial part in all of that is t=20500, which is 20.5 Celcius.

I’m off to solder some header pins onto the wires so that they don’t fall out of my breadboard so easily…



7 Responses to “DS18B20 Temperature Sensor with Raspberry Pi”

  1. 1 Don

    Any idea what the maximum cable length might be for this sensor? I need very accurate temperature measurement for my Pi-based (Raspbian) aquarium control system – have been using the Adafruit epoxy thermistor and a 12-bit A/D converter but it turned out that it wasn’t actually waterproof so now i need to replace it :-(. It also wasn’t that accurate – theoretically 1% but then there is the voltage divider resistor, power supply voltage, etc. so i ended up fudging it. The DS18B20 sounds like it might be a better solution – but i need to run the wire about 5 feet.

    thx
    Don

    • 5 feet shouldn’t be a problem (these come with about 3ft of cable attached). I’ve read about people having timing issues on the 1wire bus when running stuff for tens of metres (and needing a smaller pull up resistor for the data line), but that’s loads more than you’re after.

      By default the sensor resolution is 12 bit – 1/16th of a degree C, and the data sheet quotes an accuracy of +/-0.5C.

      Any control loop tuning tips gratefully received.

  2. 3 Don

    Thanks – definitely sounds like a better solution than the thermistor! I’m not actually controlling the temperature (yet) – just monitoring it, with alarms if it goes outside the range i want. My control loop controls the flow into my Nitrate reactor via an ORP probe, and thats a whole other story…

  3. 4 Don

    Hi Chris,

    received my sensor and attached it to pin 7, and ran the modprobe commands. However, when i look at the directory:
    /sys/bus/w1/devices/
    I just see a a soft link named w1_bus_master1
    This points to /sys/devices/w1_bus_master1

    and underneath this i see a bunch of files and soft links…
    $ ls -l
    total 0
    lrwxrwxrwx 1 root root 0 Apr 19 19:11 driver -> ../../bus/w1/drivers/w1_master_driver
    drwxr-xr-x 2 root root 0 Apr 19 19:11 power
    lrwxrwxrwx 1 root root 0 Apr 19 19:10 subsystem -> ../../bus/w1
    -rw-r–r– 1 root root 4096 Apr 19 19:10 uevent
    -rw-rw-r– 1 root root 4096 Apr 19 19:11 w1_master_add
    -r–r–r– 1 root root 4096 Apr 19 19:11 w1_master_attempts
    -r–r–r– 1 root root 4096 Apr 19 19:11 w1_master_max_slave_count
    -r–r–r– 1 root root 4096 Apr 19 19:11 w1_master_name
    -r–r–r– 1 root root 4096 Apr 19 19:11 w1_master_pointer
    -rw-rw-r– 1 root root 4096 Apr 19 19:11 w1_master_pullup
    -rw-rw-r– 1 root root 4096 Apr 19 19:11 w1_master_remove
    -rw-rw-r– 1 root root 4096 Apr 19 19:11 w1_master_search
    -r–r–r– 1 root root 4096 Apr 19 19:11 w1_master_slave_count
    -r–r–r– 1 root root 4096 Apr 19 19:11 w1_master_slaves
    -r–r–r– 1 root root 4096 Apr 19 19:11 w1_master_timeout

    i don’t see any sign of a folder starting with a number containing a file called w1_slave.

    I’m running the latest Raspbian code – i’ve run rpi-update, apt-get update and apt-get upgrade.
    $ uname -a
    Linux raspberrypi 3.6.11+ #408 PREEMPT Wed Apr 10 20:33:39 BST 2013 armv6l GNU/Linux

    Any idea where to go from here?

    thanks
    Don

  4. 6 Don

    I was in too much of a hurry – realized right after i posted that I’d forgotten the pull up. Added it and it works like a charm now. Now i just need to write a C function to read the device file, pull out the temperature, and convert it to Fahrenheit. Shouldn’t be very hard, though I would image someone’s already done this so I’ll take a look around first.

    Thanks for your help.


  1. 1 Link: How to use the DS18S20 and DS18B20 temperature sensors with Arduino or Raspberry Pi » TechNotes

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.