Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 13 of 47 · Home automation · Beginner

Wall switch with a relay

A Pi pin cannot power a lamp. A relay board is an electric switch that the Pi pin controls. Press the button to switch a lamp on and off, like a smart wall switch.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Wall switch with a relay: the board, its power supply and the parts wired to the 40-pin header
CAUTION: 230 V single-phase mains. The relay in this project switches a mains load (Live and Neutral, 230 V AC, 50 Hz in India). Mains can kill. Practise with the simulator or a low-voltage lamp. Build the mains side only with a qualified electrician, in a closed insulated box, behind a fuse or MCB and a 30 mA RCCB. Never touch the relay screw terminals while anything is connected.

Parts in this circuit

PartType
Wall buttonPush button
Relay and lampRelay module

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1ColouredWall button - SignalPin 13 (GPIO27)
2BlackWall button - GNDPin 6 (GND)
3ColouredRelay and lamp - INPin 11 (GPIO17)
4BlackRelay and lamp - GNDPin 9 (GND)
5RedRelay and lamp - VCCPin 2 (5V)

Python code (gpiozero)

from gpiozero import Button, OutputDevice
from signal import pause

# Most relay boards switch ON when the pin goes LOW.
# active_high=False hides this: relay.on() really switches the lamp on.
relay = OutputDevice(17, active_high=False)
button = Button(27)

def flip():
    relay.toggle()
    print("Lamp ON" if relay.value else "Lamp OFF")

button.when_pressed = flip
print("Press the wall button to switch the lamp")
pause()

How the code works

You will learn

  • Why a relay is needed for lamps, fans and pumps
  • Active-low relay boards and active_high=False
  • toggle() and button events

Try this

  • Press the button several times and watch the red relay LED.
  • Change active_high=False to True and run again: the lamp now behaves the opposite way, just like a real board with the wrong setting.
  • Print how many times the lamp has been switched on.

On a real Pi

  • The mains side (230 V or 120 V) can kill. Use a relay module that is rated for your voltage, a proper enclosure, and ask a qualified electrician to wire it. Practise with a 5 V or 12 V lamp first.
  • Power the relay coil from the 5 V pin (or an external supply for several relays). Share GND with the Pi.
  • A relay clicks and wears out: do not switch it faster than once every few seconds.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
4Tactile push button (12 mm, with cap)1Robu.in Amazon.in ThinkRobotics
51-channel 5 V relay module with optocoupler
10 A 250 V AC contacts
1Robu.in Amazon.in ThinkRobotics
6230 V LED bulb (9 W) with holder and wire1Robu.in Amazon.in ThinkRobotics
7Closed ABS enclosure (IP54) for the mains side1Robu.in Amazon.in ThinkRobotics
83-way screw terminal block (rated 10 A)1Robu.in Amazon.in ThinkRobotics
9MCB 6 A single pole and RCCB 30 mA (by an electrician)
Mains protection
1Robu.in Amazon.in ThinkRobotics
10Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
11Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 12. Reaction time gameAll projects14. Automatic porch light →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.