Raspberry Pi Pico - Relay

This tutorial instructs you how to use a Raspberry Pi Pico and a relay. In detail, we will learn:

Raspberry Pi Pico and relay

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Relay
1×LED Strip
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Relay

A relay is a programmable switch operated by devices like the Raspberry Pi Pico or other micro-controllers. It allows for the automatic control of devices, especially those requiring high voltage or high current, to switch them on or off.

The relay acts as a connector between the Raspberry Pi Pico and devices that use high voltage.

WARNING

When working on projects that involve main electricity, it's very important to know what you're doing to prevent electric shocks. Safety is key. If you're unsure about how to proceed, do not try to work on it. Ask for help from someone with experience instead.

We suggest using a DC device (up to 24V) for testing, although some relays can operate with both DC and AC devices.

Relay Pinout

Relay Pinout

A relay has two groups of pins: the input pins that work with low voltage, and the output pins that work with high voltage.

  • Connect the pins in the input group to the Raspberry Pi Pico. There are three pins:
    • DC- pin: connect to GND (0V).
    • DC+ pin: connect to VCC (5V).
    • IN pin: connect this pin to receive the control signal from the Raspberry Pi Pico.
  • Connect the pins in the output group to the high-voltage device. There are also three pins, typically found in a screw terminal:
    • COM pin: this is the common connection used in both the normally open and normally closed modes.
    • NO pin: this is the normally open pin. Use it in the normally open setting.
    • NC pin: this is the normally closed pin. Use it in the normally closed mode.

    We typically use only two of the pins in the high voltage group, not all of them.

    • In normally open mode, connect just the COM pin and the NO pin.
    • In normally closed mode, connect just the COM pin and the NC pin.

    Additionally, if the relay supports both LOW and HIGH level triggers, there's usually a jumper to select either the LOW level trigger or the HIGH level trigger.

    ※ NOTE THAT:

    Different manufacturers might place the pins on the relay module in different ways. Always check and follow the labels on the relay for correct connection. Be sure to check closely!

    How to Connect the High Voltage Device to Relay

    How to connect relay

    How It Works

    A relay can work differently based on the manufacturer and the installation method used by the user.

    The input mode: There are two input modes that make the relay work in opposite ways:

    • LOW level trigger mode
    • HIGH level trigger mode

    The output mode: There are two output modes, and they make the relay function differently:

    • Open by default mode
    • Closed by default mode.

    The word "normally" means when the "IN pin" is connected to "LOW (0V)". Here is some basic information:

    • The "normally open" mode and "normally closed" mode function differently.
    • Many relay modules can use both "normally open" and "normally closed" modes.
    • The LOW level trigger mode and "HIGH level level trigger" mode function differently.
    • Not all relay modules can use both LOW level trigger and HIGH level trigger modes.
    • A relay module can only work in one mode at a time, either LOW level trigger or HIGH level trigger.

    The mix of input modes and output modes creates several use cases. For beginners, we recommend using HIGH level trigger mode and normally open mode.

    The LOW level trigger and HIGH level trigger modes work differently. Next, we will explain the HIGH level trigger mode in detail. The LOW level trigger works in the opposite way.

    HIGH Level Trigger - Normally Open Mode

    To activate this mode, connect the high voltage device to both the COM pin and the NO pin.

    • If the IN pin is connected to LOW (0V), the switch stays open, and the device is turned OFF.
    • If the IN pin is connected to HIGH (5V), the switch is closed, and the device is turned ON.
    How Relay Works - Normally Open

    HIGH Level Trigger - Normally Closed Mode

    To use this mode, connect the high voltage device to the COM pin and the NC pin.

    • When the IN pin is connected to LOW (0V), the switch is closed. This means the device is turned on.
    • When the IN pin is connected to HIGH (5V), the switch is open. This means the device is turned off.
    How Relay Works - Normally Closed

    Summary

    Input modes Output modes IN pin (programmable) Output pins Relay state Device state
    HIGH Trigger Normally Open LOW COM and NO pin ⇒ open OFF
    HIGH Trigger Normally Open HIGH COM and NO pin ⇒ closed ON
    HIGH Trigger Normally Closed LOW COM and NC pin ⇒ closed ON
    HIGH Trigger Normally Closed HIGH COM and NC pin ⇒ open OFF
    LOW Trigger Normally Open LOW COM and NO pin ⇒ closed ON
    LOW Trigger Normally Open HIGH COM and NO pin ⇒ open OFF
    LOW Trigger Normally Closed LOW COM and NC pin ⇒ open OFF
    LOW Trigger Normally Closed HIGH COM and NC pin ⇒ closed ON

    There can be up to 8 use cases. This may look too much. But if you are starting out, just concentrate on the first two cases. These include the HIGH level trigger and the normally open settings. We will mostly discuss these two cases in this tutorial.

    Raspberry Pi Pico - Relay

    The Raspberry Pi Pico uses a relay to manage a device that works with high voltage.

    Controlling a relay is simple. We only need:

    • Connect the pin on the Raspberry Pi Pico to the IN pin on the relay.
    • Set the pin to LOW or HIGH to control the relay.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Relay

This image is created using Fritzing. Click to enlarge image

How To Program For Relay

  • To configure a Raspberry Pi Pico pin as a digital output. For example, to set up pin 3, use this function.
relay = Pin(RELAY_PIN, Pin.OUT)
  • Turn on relay.
relay.value(1) # Set relay to HIGH (relay ON)
  • Turn off relay.
relay.value(0) # Set relay to LOW (relay OFF)

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-relay """ from machine import Pin import time RELAY_PIN = 1 # The Raspberry Pi Pico pin connected to the relay pin (GP1) # Create a relay object using the Pin class relay = Pin(RELAY_PIN, Pin.OUT) # Main loop to toggle the relay while True: relay.value(1) # Set relay to HIGH (relay ON) time.sleep(2) # Delay for 2 seconds relay.value(0) # Set relay to LOW (relay OFF) time.sleep(2) # Delay for 2 seconds

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the relay according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the relay state.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!