Raspberry Pi Pico with Relay Module (MicroPython)

Learn to control a relay module using the Raspberry Pi Pico programmed with MicroPython. In terms of code, controlling a relay module with the Raspberry Pi Pico is the same as controlling a simple LED via ON and OFF commands. Relays serve as electronic switches, allowing the Raspberry Pi Pico to control high-power devices like lights, motors, and an array of other electrical appliances.

Raspberry Pi Pico with Relay Module MicroPython

New to the Raspberry Pi Pico? Check out our eBook: Learn Raspberry Pi Pico/Pico W with MicroPython.

Table of Contents:

In this tutorial, we’ll cover the following subjects:

Prerequisites – MicroPython Firmware

To follow this tutorial, you need MicroPython firmware installed on your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.

The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.

If you’re still getting started with the Raspberry Pi Pico, follow one of these getting-started guides:

Where to Buy Raspberry Pi Pico 2?

The Raspberry Pi Pico 2 W board is widely available on many different stores. Check the following link to compare its price on different stores:

Introducing Relays

A relay is an electrically operated switch, and like any other switch, it can be turned on or off, letting the current go through or not. It can be controlled with low voltages, like the 3.3V provided by the Raspberry Pi Pico GPIOs, and allows us to control high voltages like 12V, 24V, or mains voltage (230V in Europe and 120V in the US).

Different relay modules compatible with microcontrollers: one, two, and eight channels.

Usually, the best way to control a relay using a microcontroller, like our Raspberry Pi Pico, is using a relay module that already comes with all the circuitry to safely control a relay, and an on-board LED for visual feedback.

There are different relay modules with a different number of channels. You can find relay modules with one, two, four, eight, and even sixteen channels. The number of channels determines the number of outputs you can control. Additionally, most of them come with a built-in optocoupler that adds an extra “layer” of protection, optically isolating the Raspberry Pi Pico from the relay circuit.

Relay Pinout

For explanation purposes, let’s take a look at the pinout of a 2-channel relay module. Using a relay module with a different number of channels is similar.

Relay Module Pinout

On the left side, there are two sets of three sockets to connect high voltages, and the pins on the right side (low-voltage) connect to the pins of a microcontroller, in our case, the Raspberry Pi Pico.

High Voltage Connections

Relay Module - high voltage connections

The relay module shown in the previous picture has two connectors, each with three sockets: Normally Open (NO), common (COM), and Normally Closed (NC).

  • COM: connect the current you want to control (mains voltage)—the socket in the middle.
  • NC (Normally Closed): the normally closed configuration is used when you want the relay to be closed by default. The NC and COM pins are connected, meaning the current is flowing unless you send a signal from the Pi Pico to the relay module to open the circuit and stop the current flow. This corresponds to the socket at the right.
  • NO (Normally Open): the normally open configuration works the other way around: there is no connection between the NO and COM pins, so the circuit is broken (open) unless you send a signal from the Pico to close the circuit. This corresponds to the socket at the left.

Control Pins

Relay module - top view to see the control pins

The low-voltage side has a set of four pins and a set of three pins. The first set consists of VCC and GND to power up the module, and input 1 (IN1) and input 2 (IN2) to control the bottom and top relays, respectively.

If your relay module only has one channel, you’ll have just one IN pin. If you have four channels, you’ll have four IN pins, and so on.

The signal you send to the IN pins determines whether the relay is active or not. The relay is triggered when the input goes below about 2V. This means that you’ll have the following scenarios:

  • Normally Closed configuration (NC):
    • HIGH signal – current is flowing
    • LOW signal – current is not flowing
  • Normally Open configuration (NO):
    • HIGH signal – current is not flowing
    • LOW signal – current in flowing

In this tutorial, we’ll control a 12V (DC) lamp. Because we want the lamp to be powered off when the Raspberry Pi Pico is not sending any signal, we’ll use the normally closed configuration (LOW signal >> current is not flowing).

Power Supply Selection

Relay Module - power supply selection

The second set of pins consists of GNDVCC, and JD-VCC pins. The JD-VCC pin powers the electromagnet of the relay. Notice that the module has a jumper cap connecting the VCC and JD-VCC pins; the one shown here is yellow, but yours may be a different color.

With the jumper cap on, the VCC and JD-VCC pins are connected. That means the relay electromagnet is directly powered from the Pico power pin, so the relay module and the Pico circuits are not physically isolated from each other.

Without the jumper cap, you need to provide an independent power source to power up the relay’s electromagnet through the JD-VCC pin. That configuration physically isolates the relays from the Raspberry Pi Pico with the module’s built-in optocoupler, which prevents damage to the Pico in case of electrical spikes. However, some relay modules are simpler and you power them directly from the VCC pin.

Parts Required

We’ll now create a simple project example that controls a relay.

For this example, you’ll need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Wiring a Relay Module to the Raspberry Pi Pico

For demonstration purposes, we’ll control a 12V LED lamp. You can control any other electronic appliances, like 12V pumps or even devices powered by mains voltage.

Raspberry Pi Pico controlling a 12V Relay Lamp

Warning: dealing with mains voltage is dangerous. Misuse can result in serious injuries. If you’re not familiar with mains voltage, ask someone who is to help you out. While programming the Raspberry Pi Pico or wiring your circuit, make sure everything is disconnected from mains voltage.

If you’re not familiar with dealing with mains voltage, you can use a 12V power source to control 12V appliances, like LED lamps, as we’ll do in this example.

Connect the relay module to the Raspberry Pi Pico as shown in the following diagram, or continue reading for a more detailed explanation of the connections. The diagram shows wiring for a 2-channel relay module; wiring a different number of channels is similar.

Raspberry Pi Pico - Wiring a Relay Module

High Voltage Side

Connect the high voltage side of the relay as follows:

  • Connect one of the lamp holder terminals to the (-) sign terminal on the DC barrel power jack (or to the () terminal of whatever power source you’re using).
  • Connect the DC barrel power jack (+) sign terminal to the relay COM sockets;
  • Connect the lamp’s other terminal to the relay NO socket.

Low Voltage Side

Wire the low-voltage side of the relay to the Raspberry Pi Pico as follows:

  • Connect the relay IN1 pin to GPIO 0;
  • Connect the relay GND pin to GND;
  • Connect the relay VCC pin to the Pico VBUS pin (some relay modules only require 3V3 for power, in that case, wire to the 3V3(OUT) pin.

Recommended reading:

Code: Pico Controlling a Relay Module with MicroPython

As we’ve mentioned previously, controlling a relay module is as easy as controlling an LED. You just need to send LOW (0) and HIGH (1) signals. The following code turns the relay on for 10 seconds, and then off for 10 seconds, then on for 10 seconds, and so on.

# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-relay-micropython/

from machine import Pin
from time import sleep

relay = Pin(0, Pin.OUT)

while True:
    # RELAY ON
    relay.value(0)
    sleep(10)   
    # RELAY OFF
    relay.value(1)
    sleep(10)

View raw code

As you can see, the code is very simple. You control the relay as you would control an LED or any other output.

Recommended reading: Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython).

This example might not have a practical application, but it shows how relays work. Then, you can apply the concepts learned to make practical applications. For example: control the relay based on environmental conditions, control the relay remotely using a web server, etc. Additionally, you can control any electronic device, whether it’s a pump, a lamp, or a toaster.

Testing the Code

After uploading the previous code to the Raspberry Pi Pico, the lamp will turn on for 10 seconds. Then, it will turn off for 10 more seconds. This cycle will repeat until you stop the program.

RPi Pico connected to a relay - off state

Notice that the LED on the relay module board will turn on when the relay is triggered and will remain off when it’s not (some relay modules might not have an indicator LED).

Raspberry Pi Pico connected to a relay - on state

Wrapping Up

In this guide, you learned how to interface a relay module with the Raspberry Pi Pico. This allows you to control 12V or mains voltage devices with a 3.3V signal from the Raspberry Pi Pico.

Controlling the relay module is as easy as sending HIGH and LOW signals to the module. It’s as simple as controlling a simple output like an LED.

We have more projects and tutorials for the Raspberry Pi Pico with MicroPython, with other popular sensors that you may like:

You can check all our Raspberry Pi Pico projects and tutorials here.

If you want to learn more about the Raspberry Pi Pico and MicroPython, check out our eBook: Learn Raspberry Pi Pico/Pico W with MicroPython (compatible with RPi Pico version 1 and 2).



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.