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. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems.
- Unique 1-Wire® Interface Requires Only One Port Pin for Communication
- Reduce Component Count with Integrated Temperature Sensor and EEPROM
1. Measures Temperatures from -55°C to +125°C (-67°F to +257°F)
2. ±0.5°C Accuracy from -10°C to +85°C
3.Programmable Resolution from 9 Bits to 12 Bits
4.No External Components Required
- Parasitic Power Mode Requires Only 2 Pins for Operation (DQ and GND)
- Simplifies Distributed Temperature-Sensing Applications with Multidrop Capability
1.Each Device Has a Unique 64-Bit Serial Code Stored in On-Board ROM
- Flexible User-Definable Nonvolatile (NV) Alarm Settings with Alarm Search Command Identifies Devices with Temperatures Outside Programmed Limits
- Available in 8-Pin SO (150 mils), 8-Pin µSOP, and 3-Pin TO-92 Packages.
- Consumer Products
- Industrial Systems
- Thermally Sensitive Systems
- Thermostatic Controls
Raspberry Pi DS18B20 Temperature Sensor Tutorial
The DS18B20 temperature sensor is perfect for projects like weather stations and home automation systems. Few sensors are this easy to set up on the Raspberry Pi. They’re the same size as a transistor and use only one wire for the data signal. They’re also extremely accurate and take measurements quickly. The only other component you need is a 4.7K Ohm or 10K Ohm resistor.
In this tutorial, we’ll show you how to connect the DS18B20 to your Raspberry Pi and display the temperature readings on the SSH terminal or an LCD display.
Digital Temperature Sensors Vs.Analog Temperature Sensor
Digital temperature sensors like the DS18B20 differ from analog thermistors in several important ways. In thermistors, changes in temperature cause changes in the resistance of a ceramic or polymer semiconducting material. Usually, the thermistor is set up in a voltage divider, and the voltage is measured between the thermistor and a known resistor. The voltage measurement is converted to resistance and then converted to a temperature value by the microcontroller.
Digital temperature sensors are typically silicon based integrated circuits. Most contain the temperature sensor, an analog to digital converter (ADC), memory to temporarily store the temperature readings, and an interface that allows communication between the sensor and a microcontroller. Unlike analog temperature sensors, calculations are performed by the sensor, and the output is an actual temperature value.
ABOUT THE DS18B20
The DS18B20 communicates with the “One-Wire” communication protocol, a proprietary serial communication protocol that uses only one wire to transmit the temperature readings to the microcontroller.
The DS18B20 can be operated in what is known as parasite power mode. Normally the DS18B20 needs three wires for operation: the Vcc, ground, and data wires. In parasite mode, only the ground and data lines are used, and power is supplied through the data line.
The DS18B20 also has an alarm function that can be configured to output a signal when the temperature crosses a high or low threshold that’s set by the user.
A 64 bit ROM stores the device’s unique serial code. This 64 bit address allows a microcontroller to receive temperature data from a virtually unlimited number of sensors at the same pin. The address tells the microcontroller which sensor a particular temperature value is coming from.
- -55°C to 125°C range
- 3.0V to 5.0V operating voltage
- 750 ms sampling
- 0.5°C (9 bit); 0.25°C (10 bit); 0.125°C (11 bit); 0.0625°C (12 bit) resolution.
- 64 bit unique address
- One-Wire communication protocol
For more details on timing, configuring parasite power, and setting the alarm, see the datasheet:
Connect The DS18B20 To The Raspberry Pi
The DS18B20 has three separate pins for ground, data, and Vcc:
Wiring For SSH Terminal Output
Follow this wiring diagram to output the temperature to an SSH terminal:
R1: 4.7K Ohm or 10K Ohm resistor
Wiring For LCD Output
Follow this diagram to output the temperature readings to an LCD:
R1: 4.7K Ohm or 10K Ohm resistor
Enable The One-Wire Interface
We’ll need to enable the One-Wire interface before the Pi can receive data from the sensor. Once you’ve connected the DS18B20, power up your Pi and log in, then follow these steps to enable the One-Wire interface:
- At the command prompt, enter: sudo nano /boot/config.txt, then add this to the bottom of the file:
Exit Nano, and reboot the Pi (sudo reboot)
Log in to the Pi again, and at the command prompt enter sudo modprobe w1– gpio
Then enter sudo modprobe w1-therm
Change directories to the /sys/bus/w1/devices directory by entering: cd /sys/bus/w1/devices
Now enter ls to list the devices:
28-000006637696 w1_bus_master1 is displayed in my case.
- Now enter cd 28-XXXXXXXXXXXX (change the X’s to your own address)
For example, in my case I would enter: cd 28-000006637696
- Enter cat w1_slave which will show the raw temperature reading output by the sensor:
Here the temperature reading is t=28625, which means a temperature of 28.625 degrees Celsius.
- Enter cd to return to the root directory
That’s all that’s required to set up the one wire interface. Now you can run one of the programs below to output the temperature to an SSH terminal or to an LCD…
Programming The Temperature Sensor
- Temperature Output To SSH Terminal
This is a basic Python program that will output the temperature readings in Fahrenheit and Celsius to your SSH terminal:
import os import glob import time os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*') device_file = device_folder + '/w1_slave' def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines def read_temp(): lines = read_temp_raw() while lines.strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines.find('t=') if equals_pos != -1: temp_string = lines[equals_pos+2:] temp_c = float(temp_string) / 1000.0 temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c, temp_f while True: print(read_temp()) time.sleep(1)
Temperature Output To An Lcd
We’ll be using a Python library called RPLCD to drive the LCD. The RPLCD library can be installed from the Python Package Index, or PIP. PIP might already be installed on your Pi, but if not, enter this at the command prompt to install it:
sudo apt-get install python-pip
After you get PIP installed, install the RPLCD library by entering:
sudo pip install RPLCD
Once you have the library installed, you can run this program to output the temperature to an LCD display:
import os import glob import time from RPLCD import CharLCD lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23]) os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*') device_file = device_folder + '/w1_slave' def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines #CELSIUS CALCULATION def read_temp_c(): lines = read_temp_raw() while lines.strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines.find('t=') if equals_pos != -1: temp_string = lines[equals_pos+2:] temp_c = int(temp_string) / 1000.0 # TEMP_STRING IS THE SENSOR OUTPUT, MAKE SURE IT'S AN INTEGER TO DO THE MATH temp_c = str(round(temp_c, 1)) # ROUND THE RESULT TO 1 PLACE AFTER THE DECIMAL, THEN CONVERT IT TO A STRING return temp_c #FAHRENHEIT CALCULATION def read_temp_f(): lines = read_temp_raw() while lines.strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines.find('t=') if equals_pos != -1: temp_string = lines[equals_pos+2:] temp_f = (int(temp_string) / 1000.0) * 9.0 / 5.0 + 32.0 # TEMP_STRING IS THE SENSOR OUTPUT, MAKE SURE IT'S AN INTEGER TO DO THE MATH temp_f = str(round(temp_f, 1)) # ROUND THE RESULT TO 1 PLACE AFTER THE DECIMAL, THEN CONVERT IT TO A STRING return temp_f while True: lcd.cursor_pos = (0, 0) lcd.write_string("Temp: " + read_temp_c() + unichr(223) + "C") lcd.cursor_pos = (1, 0) lcd.write_string("Temp: " + read_temp_f() + unichr(223) + "F")