SunFounder DS18B20 Temperature Module for Raspberry Pi – Part 1

I have been using the SunFounder DS18b20 attached to various Raspberry Pi GPIO in my home automation setup.  They are really easy to add to your board and start pulling temperature data in a matter of minutes.  Each room that has a Raspberry has one attached and then using Homebridge the current temperature can be display as a sensor.

You can buy the SunFounder DS18B20 Temperature Sensor Module for Arduino and Raspberry Pi off Amazon for about $8 USD. I am sure you can find them cheaper but I am a sucker for 2 day shipping.

SunFounder DS18b20
SunFounder DS18b20

Here is my routine for setting these up on a Raspberry Pi. Note, I connect them direction to the GPOIO pins and don’t use a breadboard of any kind. I need to tuck my Raspberry Pis away in various areas of the house and they generally just hang off the Raspberry.

You will be connection a ground, signal and power using a female to female jumper wire.

Connect your jumper wires to the module then to the Raspberry Pi GPIO pins. I’ll assume what show what pin is which on your RPi.

GPIO7 to Sig
5V to ACC
GRD to GRD

Upgrade your kernel

sudo apt-get update
sudo apt-get upgrade

Edit that the following config file with nano or your preferred editor.

sudo nano /boot/config.txt

Then scroll to the bottom of the final and add the following. This might already exist but commented out. Uncomment that line by removing the # in front of it if that is the case.

dtoverlay=w1-gpio

Then reboot.

sudo reboot

Mount the device drivers and confirm whether the device is effective or not.

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices/
ls

The result should be something similar to this.

root@rasberrypi:/sys/bus/w1/devices# ls
28-051684d013ff w1_bus_master1

28-051684d013ff is an external temperature sensor device, but it may vary with every client. This is the serial number of your ds18b20.

Check the current temperature

cd 28-051684d013ff
ls

The result should be similar to the following.

root@rasberrypi:/sys/bus/w1/devices/28-051684d013ff# ls
driver id name power subsystem uevent w1_slave

Now enter the following to get the temperature.

cat w1_slave

The result is as follows.
root@raspberrypi:/sys/bus/w1_slave/28-051684d013ff# cat w1_slave
53 01 4b 46 7f ff 0c 10 2d : crc=2d YES
53 01 4b 46 7f ff 0c 10 2d t=21187

The second line t=21187 is current temperature value. If you want to convert it to degree Celsius, you can divide by 1000, that is, the current temperature is 21187/1000=21.187.

If you want to convert that to Fahrenheit remember that 0 °C = 32 °F. This is probably a bit more helpful. multiply by 9, then divide by 5, then add 32.

21.187 * 9 = 190.683
190.683 / 5 = 38.1366
38.1366 + 32 = 70.1366

21.187 C or 70.1366 F

In Part 2 of this tutorial let’s look at using Python to display the temperature on the screen.

Leave a Reply

Your email address will not be published. Required fields are marked *