A DS18B20 Temperature Sensor Module for The Raspberry Pi

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area.

The Pi DS18B20 module has a 4.7k resistor and a 2*5 female header, so it is very easy to work with your pi no need to wire up sensors to breadboards. Works perfectly with any Raspberry Pi including the Pi 1, Pi 2, Pi 3, Pi Zero, A+, B+ etc.

Here is a picture of the module

Working voltage:3.3v
±0.5°C Accuracy from -10°C to +85°C
Usable temperature range: -55 to 125°C (-67°F to +257°F)
9 to 12 bit selectable resolution
Uses 1-Wire interface- requires only one digital pin for communication
Unique 64 bit ID burned into chip
Temperature-limit alarm system
Query time is less than 750ms

 

Parts

Under $1.50 for one of these modules

Name Link
Raspberry Pi 4 Raspberry Pi 4 Model B Development Board Kit
DS18b20 Module DS18B20 Temperature Sensor Module for Raspberry Pi

 

Software

The first thing that we need to do is enable the One-Wire interface. Once you’ve connected the DS18B20, power up your Pi and log in, then follow these steps to enable the One-Wire interface:

1. At the command prompt, enter: sudo nano /boot/config.txt, then add this to the bottom of the file:

dtoverlay=w1–gpio

2. Exit Nano, and reboot the Pi. You can do this by typing in sudo reboot on the command line
3. Log in to the Pi again, and at the command prompt enter sudo modprobe w1–gpio
4. Then enter sudo modprobe w1-therm
5. Change directories to the /sys/bus/w1/devices directory by entering: cd /sys/bus/w1/devices
6. Now enter ls to list the devices:

This is my result

pi@raspberrypi:/sys/bus/w1/devices $ ls
28-0300a2797663 w1_bus_master1

So 28-0300a2797663 is my DS18B20 sensor, we will put that in the python code example below

Example:

I start Thonny IDE on my Raspberry PI and typed in the following and ran it

import RPi.GPIO as GPIO
import time
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(18, GPIO.OUT) ## Setup GPIO Pin 18 to OUT
 
while 1:
    tempfile = open("/sys/bus/w1/devices/28-0300a2797663/w1_slave")
    temptext = tempfile.read();
    tempfile.close()
    tempdata = temptext.split("\n")[1].split(" ")[9]
    temperature = float(tempdata[2:])
    temperature = temperature / 1000
    print (temperature)
    time.sleep(1)

 

In the shell part of the IDE you should see something like this when you run the code

SHARE
Previous articleA 128×32 OLED add on board fro a Raspberry Pi