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

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi Pico W

BUY

Temperature Sensor Module (DS18B20)

-

Breadboard

BUY

Wiring

../_images/Lesson_18_DS18B20_bb.png

Code

Note

  • Open the 18_ds18b20_module.py file under the path of universal-maker-sensor-kit-main/pico/Lesson_18_DS18B20_Module or 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

  1. Importing Libraries

    The code begins by importing necessary libraries. machine is used for controlling GPIO pins, onewire for the OneWire communication protocol, ds18x20 for the specific temperature sensor, and time for delays.

    Regarding OneWire in MicroPython, you can refer to OneWire driver.

    from machine import Pin
    import onewire
    import time, ds18x20
    
  2. 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))
    
  3. 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)
    
  4. 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)
    
  5. 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)