DS18B20 is a commonly used temperature temperature sensor providing 9 bit to 12 bit digital Celsius temperature measurements. The sensor communicates over one wire bus. Each sensor has a 64 bit serial code enabling multiple sensors to be connected to the same one wire bus.
The Raspberry Pi has drivers for one wired devices to be connected to GPIO pin-4 by default. 1-wire is a device communication data bus system developed by Dallas semiconductor providing low speed data, signalling and power over a single signal wire. One wire is similar to I2C with longer range and low data rates. It can be used to communicate with inexpensive devices like thermometers, humidity sensors and other one wire sensors over a long range. The main advantage is since each device has a unique address, any number of devices can be connected on a single wire limited by the drive capacity of Raspberry Pi’s GPIO and the total capacitance seen on the line.
The one wire bus requires a weak pull up resistor as all devices linked to this bus via a tri-state or open drain output. Here a 4.7KΩ resistor is used as pull up.
Sensor not detecting after OS update
Users having Rasbian with updated Linux kernel 3.18 need to edit boot config file to work with one wire sensors.
- In terminal edit config file,
sudo nano /boot/config.txt
- And add,
dtoverlay=w1-gpio,gpiopin=4
- And save (Ctrl+X)
Reading temperature using Terminal
The one wire communication device kernel modules can be loaded by typing,
sudo modprobe w1-gpio
sudo modprobe w1-therm
Point to the address of the temperature sensor,
cd /sys/bus/w1/devices/28*
Note: If more than one device is connected use cd /sys/bus/w1/devices/
and ls
to list all the devices and cd address
where address
is the unique address of the device required. For eg.cd 28-00042d8165ff
Display temperature,
cat w1_slave
For more detail: Interfacing DS18B20 Temperature sensor with Raspberry Pi