Raspberry Pi Pico - Relay
This tutorial instructs you how to use a Raspberry Pi Pico and a relay. In detail, we will learn:
- How a relay works.
- How to connect a relay to a high-voltage device.
- How to connect the Raspberry Pi Pico to the relay.
- How to program the Raspberry Pi Pico to control the relay for switching high-voltage devices on and off.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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
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.
- 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.
- LOW level trigger mode
- HIGH level trigger mode
- Open by default mode
- Closed by default mode.
- 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.
- 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.
- 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.
- 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.
We typically use only two of the pins in the high voltage group, not all of them.
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 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:
The output mode: There are two output modes, and they make the relay function differently:
The word "normally" means when the "IN pin" is connected to "LOW (0V)". Here is some basic information:
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.
HIGH Level Trigger - Normally Closed Mode
To use this mode, connect the high voltage device to the COM pin and the NC pin.
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:
Wiring Diagram
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.
- Turn on relay.
- Turn off relay.
Raspberry Pi Pico Code
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.