Building a Temperature-Based Control System with Raspberry Pi Pico and DS18B20/HW-506 Sensor

In this tutorial, you will learn how to use a Raspberry Pi Pico to create a simple temperature control system that automatically switches a relay based on temperature readings from a DS18B20/HW-506 temperature sensor. This project is perfect for beginners who want to understand how to interface with temperature sensors and control devices such as fans or heaters via relays.

Components Needed:

  1. Raspberry Pi Pico.
  2. DS18B20/HW-506 temperature sensor.
  3. Relay module (to control an output device like a fan, heater, or any other electrical appliance).
  4. Jumper wires and a breadboard.
  5. A computer with Thonny IDE for coding in MicroPython.

How the System Works:

  • The DS18B20 sensor continuously measures the room temperature.
  • The Raspberry Pi Pico monitors the temperature and turns on a relay when the temperature rises above 20°C.
  • The relay can be connected to any output device that you want to control based on temperature, such as a cooling fan or a heater.

Step-by-Step Guide

Step 1: Connecting the Components

  1. DS18B20 Sensor Connections:

    • VCC (3.3V) → Pico 3.3V pin.
    • GND → Pico GND pin.
    • Data Pin → Pico GPIO16 (pin 21).
  1. Relay Module Connections:

    • VCC → Pico 3.3V pin.
    • GND → Pico GND pin.
    • IN → Pico GPIO15 (pin 20).

Step 2: Installing MicroPython Libraries

Before writing the code, make sure your Raspberry Pi Pico has MicroPython installed. You will also need the ds18x20 and onewire libraries, which come pre-installed in MicroPython for the Pico.

Step 3: Writing the Code

Below is the code that will monitor the temperature and control the relay based on whether the temperature exceeds 20°C.

Code:

import machine
import onewire
import ds18x20
import time

# Setup the DS18B20 sensor on GPIO16
ds_pin = machine.Pin(16)
sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))

# Setup the relay on GPIO15
relay_pin = machine.Pin(17, machine.Pin.OUT)

# Scan for DS18B20 devices (there could be more than one)
roms = sensor.scan()
print("Found DS18B20 devices:", roms)

# Initialize relay state (off by default)
relay_pin.off()

while True:
    try:
        # Start a temperature conversion
        sensor.convert_temp()
        time.sleep(1)  # Wait for the conversion to complete
        
        # Read the temperature from all connected sensors
        for rom in roms:
            temperature = sensor.read_temp(rom)
            print("Temperature: {:.2f}°C".format(temperature))
            
            # Trigger relay if temperature is above 20°C
            if temperature > 20:
                relay_pin.on()  # Turn on relay
                print("Temperature above 20°C - Relay ON")
            else:
                relay_pin.off()  # Turn off relay
                print("Temperature below or equal to 20°C - Relay OFF")

    except Exception as e:
        print("Error reading sensor:", e)

    time.sleep(15)  # Read temperature every 2 seconds




Code Breakdown:

  1. DS18B20 Setup:

    • The DS18B20or HW-506 is connected to GPIO16, and we initialize the sensor using the onewire and ds18x20 libraries.
    • The scan() method detects all DS18B20 sensors connected to the data line (if there are multiple sensors).
  2. Relay Setup:

    • The relay is connected to GPIO15. The relay is initially turned off.
  3. Temperature Monitoring:

    • In the main loop, we periodically start a temperature conversion with convert_temp() and then read the temperature from each connected sensor.
    • If the temperature exceeds 20°C, the relay is turned on, and a message is printed. If the temperature is 20°C or below, the relay is turned off.
  4. Error Handling:

    • If there's an error (e.g., the sensor is disconnected), the code will catch the error and print a message.

Step 4: Running the Code

  1. Open the Thonny IDE on your computer.
  2. Connect your Raspberry Pi Pico via USB.
  3. Copy and paste the above code into the Thonny editor.
  4. Press the Run button to start the program. The relay will now turn on or off depending on the temperature measured by the DS18B20 sensor.

Step 5: Testing the System

  • Once the code is running, the sensor will start reading the temperature every 2 seconds.
  • If the temperature rises above 20°C, the relay will turn on, allowing you to control an external device like a fan or heater.
  • If the temperature drops below 20°C, the relay will turn off.

Real-World Applications

This simple temperature control system can be adapted for various real-world applications, such as:

  • Automatic Fan Control: Turn on a fan when the room temperature gets too hot.
  • Heater Control: Activate a heater if the room becomes too cold.
  • Greenhouse Monitoring: Maintain ideal temperature levels for plant growth by controlling heating or ventilation.

Conclusion

This project demonstrates how easy it is to interface a DS18B20/HW-506 temperature sensor with the Raspberry Pi Pico to build a functional temperature control system. The ability to trigger a relay based on temperature readings opens up a wide range of automation possibilities, from home climate control to industrial applications.

With a few tweaks, you can customize this system to suit your needs, adding more sensors or adjusting the threshold temperatures. We hope this tutorial helps you understand the basics of sensor interfacing and relay control using MicroPython and the Raspberry Pi Pico.

Feel free to explore more with this setup—whether you're automating a room or learning the fundamentals of electronics, the possibilities are endless!

Comments

Popular posts from this blog

Tutorial: Using Raspberry Pi as an OpenVPN Client Router with Ethernet Sharing

How to Use a Raspberry Pi 4 as a Firewall to Block Adult Content and Protect Your Family

Tutorial: Using Raspberry Pi Pico and HW-488 Infrared Obstacle Avoidance Sensor to Play/Pause a Video on Your PC via USB