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âll learn how to integrate and read temperature data from DS18B20 sensors using the Raspberry Pi Pico W. Youâll begin by setting up a OneWire bus on the GPIO pin and scanning for DS18X20 devices. The main focus of the lesson is continuously reading and displaying temperature measurements from these sensors.
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 Pico W |
|
- |
|
Wiringď
Codeď
Note
Open the
18_ds18b20_module.pyfile under the path ofuniversal-maker-sensor-kit-main/pico/Lesson_18_DS18B20_Moduleor copy this code into Thonny, then click âRun Current Scriptâ or simply press F5 to run it. For detailed tutorials, please refer to Open and Run Code Directly.Donât forget to click on the âMicroPython (Raspberry Pi Pico)â interpreter in the bottom right corner.
from machine import Pin
import onewire
import time, ds18x20
# Initialize the OneWire bus on GPIO pin 12
ow = onewire.OneWire(Pin(12))
# Create a DS18X20 instance using the OneWire bus
ds = ds18x20.DS18X20(ow)
# Scan for DS18X20 devices on the bus and print their addresses
roms = ds.scan()
print('found devices:', roms)
# Continuously read and print temperature data from the sensors
while True:
# Start the temperature conversion process
ds.convert_temp()
# Wait for the conversion to complete (750 ms for DS18X20)
time.sleep_ms(750)
# Read and print the temperature from each sensor found on the bus
for rom in roms:
print(ds.read_temp(rom))
# Wait for a short period before the next reading (1000 ms)
time.sleep_ms(1000)
Code Analysisď
Importing Libraries
The code begins by importing necessary libraries.
machineis used for controlling GPIO pins,onewirefor the OneWire communication protocol,ds18x20for the specific temperature sensor, andtimefor delays.Regarding OneWire in MicroPython, you can refer to OneWire driver.
from machine import Pin import onewire import time, ds18x20
Initializing OneWire Bus
A OneWire bus is initialized on GPIO pin 12. This sets up the communication between the Raspberry Pi Pico W and the DS18B20 sensor.
ow = onewire.OneWire(Pin(12))
Creating DS18X20 Instance
A DS18X20 instance is created using the OneWire bus. This instance is used to interact with the temperature sensor.
ds = ds18x20.DS18X20(ow)
Scanning for Devices
The code scans for DS18X20 devices on the OneWire bus and prints their addresses. This is important for identifying the connected sensors.
roms = ds.scan() print('found devices:', roms)
Reading Temperature Data
The main loop of the program continuously reads temperature data from the sensor.
It starts the temperature conversion process and waits for it to complete, which takes about 750 milliseconds.
It then reads and prints the temperature from each sensor found on the bus.
The loop pauses for 1000 milliseconds before repeating.
while True: ds.convert_temp() time.sleep_ms(750) for rom in roms: print(ds.read_temp(rom)) time.sleep_ms(1000)