In this guide, we'll learn what a relay is, why we might use one, how to choose a relay, and finally how to use one with a Raspberry Pi Pico.


Have you ever wanted to control a higher powered device from a low powered one without frying it? Well, one of the easiest ways is to use a relay. Relays are a type of switch that can be turned on and off electronically. Just like this little demo I have here, the relay is turning on and off a 12-volt fan from a 3.3-volt microcontroller.

Internally, they are made up of two physically and electrically separated circuits. The input circuit, which does the switching, and the control circuit, which is what gets turned on and off. But if the two circuits are isolated from each other, how does one switch on or off the other? Well, it's not done with magic, but rather using either magnetism, in electromechanical relays or light, in solid-state relays.

There are other types of relays such as reed, coaxial, and others, but electromechanical and solid state are by far the most common types that a maker will likely want to use.

Relays can also do more than just connect or disconnect a single path of electricity. They come with different combinations of poles and throws, which enable you to do interesting things when the input circuit is or is not activated. The number of poles a relay has defines how many switches there are in its control circuit. A single pole relay will let you switch only one device on off, but a double pole will let you switch two devices on off simultaneously. Note that poles cannot be controlled independently, they are both controlled by the same input signal.

The number of throws a relay has, or more specifically a pole has, defines how many output contacts each pole in the relay can switch between. A single throw has one output contact per pole. A single throw can only connect or disconnect a single circuit, but a double throw can switch between two different circuits. Double throw relays are useful when you need to switch between two different circuits using only one input signal.

In summary, relays provide a simple and safe way to control high-powered devices with low-powered ones. They are available in different types, such as electromechanical and solid-state relays, each serving different purposes. Furthermore, relays come with varying combinations of poles and throws that give you more control over how circuits are connected and switch a low-powered device, like an LED, it might not be worth the complexity and additional components needed to use a relay.

So, let's dive a bit further into how to use a relay. Each relay will have its own pinout, or pin diagram. It's important to consult the datasheet for the specific relay you are using, as the pins may be labelled differently. In general, though, there will be pins for the control circuit and pins for the output circuit.

Let's talk a bit more about those control pins. As mentioned earlier, there is a normally open (NO) pin, a normally closed (NC) pin, and a common (COM) pin. When the relay is in its resting state, the NO pin and the COM pin will be open, while the NC pin and the COM pin will be closed. When a voltage is applied to the control pins, the switch inside the relay will flip, making the NO pin and the COM pin close, while the NC pin and the COM pin open.

Now, let's talk about hooking up the output circuit. Depending on the relay and the device you are controlling, you may need to use additional components, such as diodes, to protect the circuit. Again, consult the datasheet and/or seek advice from a qualified professional if you're not sure. When hooking up the output circuit, you will need to connect the device you are controlling to either the NO pin and the COM pin or the NC pin and the COM pin, depending on the desired behavior.

In conclusion, relays are a great way to control high-powered devices with a low-powered signal. However, they may not always be the best choice for every situation. When using a relay, it's important to consult the datasheet, understand the pinout, and consider any additional components needed. And, of course, always prioritize safety and seek advice from a qualified professional when it comes to mains power.To switch something on and off, and that thing is the same voltage as your microcontroller, in most cases a transistor will do just fine.

Now that we've decided a relay is the right device for our project, it's time to choose the right one. There are a few factors to consider when choosing a relay.

First, we need to consider what input voltage the relay requires to be activated, and if your circuit can provide the required voltage to it. Next, we need to consider the maximum voltage and current of the control circuit in the relay. This is sometimes referred to as the contact rating, or how much the relay can drive. You need to select a relay that's maximum is higher than your load.

Finally, if you're just starting out with relays, I recommend getting one that comes as a module, such as the one I'm using in this guide. A module will make it easier to connect the relay to your microcontroller, and come with the necessary circuitry required to switch the relay on from your microcontroller.

Let's have a look at how to actually use a relay with a Raspberry Pi Pico. To follow along, all you'll need is a Raspberry Pi Pico, a relay, and a way to connect them together.

I'm going to use a breadboard, some hookup wire, and this single channel relay module. It's linked in the guide if you want to get one yourself. You'll also need an LED and resistor, and if you have one handy, a 12V device and a power source. I'm going to use this 12V fan and a 12V plug pack. Again, check out the guide for links to everything I've used.

If you're using a standalone or non-module relay, go and check out the guide where I've shown what other components you'll need, and how to wire it up instead.

Connecting up a relay requires a few steps. First, connect the input voltage to VCC and the ground to GND on your Raspberry Pi Pico. Next, connect the input control signal to a GPIO pin on the Raspberry Pi Pico. Finally, connect the device that you want to switch on and off to the Normally Open (NO) contact on the relay.

Once everything is connected, write some code to turn on and off the GPIO pin and watch your device switch on and off accordingly. It's really that simple!Relay involves wiring the input circuit to the Pico, and then wiring the output circuit to the device to be controlled. To begin with, we're going to control an LED. If you're using the same relay module as me, you'll notice it has Chinese markings. Check out the guide for a reference picture if you need it.

For the input circuit, connect the three pins of the relay module to the corresponding pins on the Pico. Relay VCC goes to the Pico 3.3V, Ground goes to one of the ground pins, and Relay IN goes to Pico GPIO 16. Note you can use any GPIO pin, I've just chosen 16 for easy breadboard placement.

For the control circuit, connect the LED to the relay by adding the LED to the breadboard. Connect its negative leg to one of the ground pins on the Pico. Add a resistor to the positive leg of the LED, and connect the other side of the resistor to the common terminal on the relay. Finally, connect the normally open terminal to the VBUS pin of the Pico. Note the VBUS pin on the Pico is connected directly to the Pico's USB 5V power. Be careful not to short any wires or you may damage the computer when you plug the USB cable into the Pico.

We're all wired up and we're ready to use the relay to control the LED. Plug your Pico into the computer, head over to the guide and find the section Example 1. Copy paste the example code, head on over to the Thonny, make sure it detects your Pico. Let's create a new file, paste in the code and save it to the Pico. Now let's run this code and see it in action.

Hit run, and as you can see the LED is turning on and off via the relay.

How does this work? Well first we import pin from machine because we're going to use one of the GPIO pins as an output. We setThat pin up, which is pin 16 I've used, set as an output. We're going to assign it to this variable called relay. This is quite simple, in an infinite loop we first turn the relay on, then we sleep for one second, we turn the relay back off, and sleep for another second. That's pretty much it.

So we've controlled an LED, but we didn't really need a relay to do that with a microcontroller. Let's try something we do need a relay for, switching a higher voltage circuit from the lower powered Pico.

Remove the LED and resistor from the previous example, and let's wire up the 12 volt fan and power supply. We're going to connect up the fan so it's switching on the negative side of the circuit.

So let's connect the 12 volt negative to the common terminal of the relay, and connect the normally open terminal to the negative wire of the 12 volt fan. We've now completed the control circuit. Note that I'm using this little barrel jack here, which is breaking out the positive and negative to a connector that I will use for my plug pack. Note this is purely a hardware change.

We're going to reuse the code from example one. So let's connect the 12 volt power, head over to Thonny, and press play. You look at that, it's working just like the LED was, but this time we have a completely isolated circuit that's running 12 volts, and we're controlling it from a 3.3 volt circuit.

Finally, let's take a closer look at the difference between normally open and normally closed. The circuit from the previous example has the 12 volt fan connected to the normally open terminal of the relay, which means by default the fan is turned off. Leave it connected that way for now, so we can see normally open in practice.Let's take a look at an example script that demonstrates the behavior of a relay. Open up the guide and navigate to example three. This script is straightforward - it turns on the relay so that it remains on. You can copy and paste the script to Thonny, overwriting any previous scripts, and then save it.

Now, observe the behavior of normally open by running the script. You'll see that the fan turns on and stays on. But if you disconnect the power source from the Raspberry Pi Pico while leaving the 12-volt power connected, you'll notice that the fan turns itself off. This occurs because the loss of power to the microcontroller has deactivated the relay, causing the normally open control circuit to revert to its default state and switch back to the normally open position.

This feature can be useful in systems that need to be turned off automatically when a fault is detected. For example, you may want to turn off a valve in a high-pressure water system to prevent flooding while you find and fix the fault.

Next, let's examine the normally closed position. To do this, disconnect the Pico entirely, disconnect the 12-volt power source, and move the wire from the normally open terminal of the relay to the normally closed terminal. When you reconnect the 12-volt power source, you'll notice that the fan turns on immediately, even though the Pico is not running.

By default, the normally closed position is always on. To revert the control circuit to the normally open terminal, reconnect the USB cable to the Pico and run the script from example three again. The script will automatically run, and you'll see the fan turn off as the relay activates and moves the control circuit to the normally open terminal, which is no longer connected to anything.Pull the USB cable out from the Pico again, and the fan turns back on. At first, this might seem a little pointless. What is the use of a switch that does the opposite of what you want?

Well, imagine for a second that instead of a fan, we connected an emergency siren, and we added that to the hypothetical high-pressure water system, I mentioned before. Now when there is a fault in the system, not only does the high-pressure valve turn off automatically, but the emergency siren also turns on automatically. Two useful tasks performed automatically at the same time, thanks to the handy relay.

Relays are useful devices that can do a lot more than you might first think. If your next project needs to control something that runs on higher power, or you like the idea of having hardware that can run on higher power, or you like hardware level fail safes, then look no further than the relay.

If you're doing something cool with relays and want to tell us about it, or if you have any questions or need some help, let us know about it over in the forums.

Until next time, happy making.



Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.