Hooking Up a Thermometer to Raspberry Pi (DS18B20)

This post will be about hooking up a waterproof DS18B20 thermometer to a Raspberry Pi.

What you will need:

1 DS18B20 waterproof thermometer: ~ $2 on eBay

1 4.7 KOhm resistor: Should buy a set of resistors before working on electronics

Breadboard jumper wires: could come with the set of resistors

6 Female to Female Jumper Wires: Bought a 10 pack at a local hobby shop for $2

Electrical Tape: Also bought at hobby shop for $4

Most of this was taken from a great tutorial here: http://www.raspberrypi-spy.co.uk/2013/03/raspberry-pi-1-wire-digital-thermometer-sensor/

That tutorial was for the original Raspberry Pi board and we will introduce the changes we made for taking it off the breadboard. Our pins were also configures for the Pi 2 board.


The schematic of attaching the thermometer using the breadboard from the tutorial

Here is a schematic of how you can take the thermometer off of a board(excuse my lack of artistic talent):


If it is not clear, the box-line-box stands for the female jumper cable, breadboard jumper wires were used to connect the jumper cables, and electrical tape was used to wrap up each connection.


Here is a picture of the finished product and we used color coordinated jumper cables

The tutorial says Pin 1 was connected to P1-06 (Ground), Pin 2 was connected to P1-07 (GPIO4) and Pin 3 was connected to P1-01 (3.3V). We added the ground (black wire) to Pin 20, Pin 2 (yellow wire) to Pin 18 (GPIO 24) and Pin 3 (red wire) to Pin 17 (3.3V) for the Rasperry Pi 2 board.

The tutorial says to add the following dtoverlay=w1-gpio,gpiopin=4 to the bottom of the file that comes up when running the command, in the command line, sudo nano /boot/config.txt ; instead, because we changed the pins around for the Raspberry Pi 2 board, we changed the line to dtoverlay=w1-gpio,gpiopin=24 to suit our the new pin. You can suit this for whatever GPIO pin you choose, just make sure you have the ground and power pins connected to the right pins.

Check out our upcoming post on Raspberry Pi 2 pins! This just reminded me to write it 🙂

After adding that line, as the tutorial says, use sudo reboot in the command line to restart your board.

When you’re back in on the command line, type cd /sys/bus/w1/devices then ls Hopefully, you get something like this:thermo_found_ser

WRITE DOWN THAT 28-00000… CODE. The number is unique to each thermometer, we even attached it to ours using tape: 20150628_144404

The tutorial then gives a great python script, which we will copy and paste here to archive, just in case: (we will also look into writing a post about getting python on your board  you can also just google it like us. 🙂

    mytemp =''
    filename ='w1_slave'
    f =open('/sys/bus/w1/devices/'+id+'/'+filename, 'r')
    line =f.readline() # read 1st line
    crc =line.rsplit(' ',1)
    crc =crc[1].replace('\n', '')
      line =f.readline() # read 2nd line
      mytemp =line.rsplit('t=',1)
      mytemp =99999
if__name__ =='__main__':
  # Script has been called directly
  print"Temp : "+'{:.3f}'.format(gettemp(id)/float(1000))

We copied this code into a new text document and saved it as thermometer_read.py  You need to change the line id = ’28-00000482b243′ to the id you have for your thermometer! cd to the directory you saved the code file in, then type python file_name.py and… Congratulations! You hooked up a thermometer to your Raspberry Pi!


The first reading is if you do not change the id = line correctly


About pythagoraspberry

We are a team working on learning about and documenting our experiences using the Raspberry Pi system. Having had much trouble finding answers to basic problems, we hope that this website will be of use to many people getting started. We look forward to helping and being helped; inspiring and being inspired.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s