Raspberry Pi Pico Relay Simulator: Wiring, Code and a Live Circuit
This is a single-channel 5 V relay module wired to a Raspberry Pi Pico, clicking on and off once a second live in your browser. The arm on the drawing moves between NC and NO as the board switches it.
VCC to VBUS (5 V from USB), GND to GND, and IN to GP15 (physical pin 20). The pin only gives the signal; the module powers the coil.
- 3.3 V logic
- No library
- IN on GP15 (physical pin 20)
- Coil on VBUS (5 V from USB)
Wiring the Relay to the Raspberry Pi Pico
Every connection in the circuit above, with the board’s own pin names.
| Relay pin | Raspberry Pi Pico pin | Why |
|---|---|---|
| VCC | VBUS (5 V from USB) | The coil is a 5 V coil; it takes about 70 mA, more than any pin can give. |
| GND | GND | Common ground. |
| IN | GP15 (physical pin 20) | A logic signal only: the module’s transistor switches the coil. |
| COM, NO, NC | The load | The switched side: COM is the arm, NO is open until the relay pulls in, NC is closed until then. |
The coil runs from VBUS (5 V from USB) and IN comes from a 3.3 V pin. Most single-channel modules switch fine from 3.3 V; some optocoupler modules with a high-side jumper need closer to 5 V, and then a small transistor or a 3.3 V relay module solves it. The simulated relay is set to trigger on HIGH.
Pins to leave alone on the Raspberry Pi Pico
The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.
On a real bench
Solder headers on the Pico and press it into the breadboard across the center channel; the Relay goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
const int RELAY_PIN = 15; // IN, GP15 (physical pin 20)
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // released at start
}
void loop() {
digitalWrite(RELAY_PIN, HIGH); // pull in: COM to NO
Serial.println("Relay on");
delay(1000);
digitalWrite(RELAY_PIN, LOW); // release: COM back to NC
Serial.println("Relay off");
delay(1000);
}
digitalWrite(RELAY_PIN, HIGH) turns on the module’s transistor, which energizes the coil and pulls the arm from NC over to NO about 10 ms later. LOW lets it fall back.
The relay is off at the start because setup() writes LOW before anything else. Do the same in a real project, so the load does not flick on while the board boots.
Serial.begin(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.
Common mistakes with the Relay on the Raspberry Pi Pico
Active low modules
Many cheap relay boards switch when IN goes LOW, so the sketch seems inverted and the relay is on at boot. Check the board, and in the editor set the relay’s trigger property to low to match.
Driving a coil straight from a pin
A bare relay coil wants 70 mA or more and kicks back a voltage spike when it releases. On a module the transistor and the flyback diode are already there; a bare relay needs both, and never a direct pin.
Switching mains without care
The contacts are rated for mains, but the board and the screw terminals are not a safe enclosure. Learn on a 12 V lamp or a motor, and leave mains to a properly rated, enclosed module.
Questions and answers
Can I simulate a relay with a Raspberry Pi Pico?
Yes. The circuit at the top of this page is a Raspberry Pi Pico with a relay module, running in your browser on a simulated RP2040. The simulated relay has a pull-in and release time, and its contacts really connect COM to NO or NC, so a lamp wired through it switches. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Will a 5 V relay module work with the Raspberry Pi Pico?
Usually, yes: power it from VBUS (5 V from USB) and drive IN from a GPIO. If a module will not switch from 3.3 V, use a 3.3 V relay module or a transistor between them.
What do NO and NC mean?
Normally open and normally closed. With the coil off, COM is connected to NC; with it on, COM is connected to NO. Wire a load to NO to have it off until the relay switches.
How fast can a relay switch?
A few times a second at most. It takes about 10 ms to pull in and its contacts wear with every click, so for anything faster, or for dimming, use a MOSFET.
Build your own Relay project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.