Skip to content

Project 1.4.3: Relay Timer Switch


Overview

Build a relay timer switch with a button and an LED indicator.

This project demonstrates timed automation and safe relay control.

The final result should turn the relay and indicator LED on for a set time and then turn them off automatically.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
1-channel relay module
1-channel relay module
Push button
Push button
LED
LED
220 Ohm resistor
220 Ohm resistor
Breadboard
Breadboard
Jumper wires
Jumper wires

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
Relay VCC 5V / VSYS Physical pin 40 Module power
Relay GND GND Physical pin 38
Relay IN GPIO 0 GPIO 0 / physical pin 1 Usually active-low
Button leg 1 GPIO 1 GPIO 1 / physical pin 2 Pull-up in code
Button opposite leg GND Physical pin 38
LED anode (+) 220 Ohm resistor then GPIO 2 GPIO 2 / physical pin 4 Status indicator
LED cathode (-) GND Physical pin 38

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2 W

Place the Raspberry Pi Pico 2 W on the breadboard so it sits across the center gap.

Step 1


Step 2: Place the Relay Module and Push Button

Place the relay module where its pins are easy to reach and place the push button across the breadboard center gap.

Step 2


Step 3: Connect Relay Power

Connect relay VCC to 5V / VSYS and relay GND to GND.

Step 3


Step 4: Connect the Relay IN Pin

Connect relay IN to GPIO 0.

Step 4


Step 5: Connect the Push Button

Connect one button leg to GPIO 1 and the opposite button leg to GND.

Step 5


Step 6: Place the Status LED

Place the LED on the breadboard with its legs in different rows.

Step 6


Step 7: Connect the LED Through a Resistor

Connect the LED long leg through a 220 Ohm resistor to GPIO 2, and connect the LED short leg to GND.

Step 7


Wiring Check

  • Relay VCC connects to 5V / VSYS.
  • Relay GND connects to GND.
  • Relay IN connects to GPIO 0.
  • Button connects between GPIO 1 and GND.
  • LED long leg connects through a resistor to GPIO 2.
  • LED short leg connects to GND.

Testing Individual Components

LED Test

from machine import Pin
import time

led = Pin(2, Pin.OUT)
led.on()
time.sleep(1)
led.off()

Button Test

from machine import Pin
import time

button = Pin(1, Pin.IN, Pin.PULL_UP)

while True:
    print('Pressed' if button.value() == 0 else 'Released')
    time.sleep(0.2)

Relay Click Test

from machine import Pin
import time

relay = Pin(0, Pin.OUT)
relay.value(1)
time.sleep(1)
relay.value(0)
time.sleep(1)
relay.value(1)

Full Project Code

from machine import Pin
import time

relay = Pin(0, Pin.OUT)
button = Pin(1, Pin.IN, Pin.PULL_UP)
led = Pin(2, Pin.OUT)
RUN_TIME = 10

relay.value(1)
led.off()

print('Relay timer switch ready')
print('Press the button to start the timer')

while True:
    if button.value() == 0:
        print('Timer started')
        relay.value(0)
        led.on()

        for remaining in range(RUN_TIME, 0, -1):
            print('Time remaining:', remaining, 's')
            time.sleep(1)

        relay.value(1)
        led.off()
        print('Timer complete')
        time.sleep(0.3)

    time.sleep(0.02)

How the Code Works

Code Section What It Does Why It Matters
RUN_TIME Stores how long the relay stays active Timer can be changed easily
Button input Starts the timer when pressed Gives manual control
Relay and LED on Turns on relay and indicator LED Shows when timer is active
Countdown loop Prints remaining seconds and turns outputs off Creates timed switch behavior

Expected Result

Pressing the button turns on the relay and LED for about 10 seconds. After the countdown finishes, both turn off automatically.


Troubleshooting

Problem Possible Cause Solution
Relay does not click Wrong relay power or logic Check VCC, GND, and active-low behavior
LED stays off LED reversed or resistor misplaced Reverse LED and check resistor path
Timer restarts unexpectedly Button bouncing or held down Release button after one press