Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 18: Temperature Sensor Module (DS18B20)
In this lesson, you will learn how to use a Raspberry Pi to read temperature data from a DS18B20 temperature sensor. You will understand how to locate the sensor’s device file, read and parse its raw data, and convert this data into Celsius and Fahrenheit readings.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi 5 |
|
- |
|
Wiring
Code
Note
The DS18B20 module communicates with the Raspberry Pi using the onewire protocol. Before running the code, you need to enable the onewire function of the Raspberry Pi. You can refer to this tutorial: 1-Wire Configuration.
import glob
import time
# Path to the directory containing device files for 1-wire devices
base_dir = "/sys/bus/w1/devices/"
# Finds the first device folder that starts with "28", specific to DS18B20
device_folder = glob.glob(base_dir + "28*")[0]
# Device file containing the temperature data
device_file = device_folder + "/w1_slave"
def read_temp_raw():
# Reads raw temperature data from the sensor
f = open(device_file, "r")
lines = f.readlines()
f.close()
return lines
def read_temp():
# Parses the raw temperature data and converts it to Celsius and Fahrenheit
lines = read_temp_raw()
# Waits for a valid temperature reading
while lines[0].strip()[-3:] != "YES":
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find("t=")
if equals_pos != -1:
temp_string = lines[1][equals_pos + 2 :]
temp_c = float(temp_string) / 1000.0 # Convert to Celsius
temp_f = temp_c * 9.0 / 5.0 + 32.0 # Convert to Fahrenheit
return temp_c, temp_f
try:
# Main loop to continuously read and print temperature
while True:
temp_c, temp_f = read_temp()
formatted_output = f"Temperature: {temp_c:.2f}°C / {temp_f:.2f}°F"
print(formatted_output)
time.sleep(1) # Wait for 1 second between readings
except KeyboardInterrupt:
# Gracefully exit the program on CTRL+C
print("Exit")
Code Analysis
Importing Necessary Libraries
The
globlibrary is used to search for the temperature sensor’s device folder. Thetimelibrary is used for implementing delays in the program.import glob import time
Locating the Temperature Sensor Device File
The code searches for the directory of the DS18B20 sensor by looking for a folder name starting with “28”. The device file
w1_slavecontains the temperature data.base_dir = "/sys/bus/w1/devices/" device_folder = glob.glob(base_dir + "28*")[0] device_file = device_folder + "/w1_slave"
Reading Raw Temperature Data
This function opens the device file and reads its content. It returns the raw temperature data as a list of strings.
def read_temp_raw(): f = open(device_file, "r") lines = f.readlines() f.close() return lines
Parsing and Converting Temperature Data
The
read_tempfunction callsread_temp_rawto get the raw data. It waits for a valid temperature reading and then extracts, parses, and converts the temperature to Celsius and Fahrenheit.def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != "YES": time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find("t=") if equals_pos != -1: temp_string = lines[1][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
Main Program Loop and Graceful Exit
The
tryblock contains an infinite loop to continuously read and display the temperature. Theexceptblock catches a KeyboardInterrupt to exit the program gracefully.try: while True: temp_c, temp_f = read_temp() formatted_output = f"Temperature: {temp_c:.2f}°C / {temp_f:.2f}°F" print(formatted_output) time.sleep(1) except KeyboardInterrupt: print("Exit")