Anth's Computer Cave

Build a relay circuit to switch power.

Today we are building a relay circuit to turn power points, lights and other appliances on or off.

A relay circuit built from salvaged components. Picture: Anthony Hartup A relay circuit built from salvaged components. Picture: Anthony Hartup
The relay circuit AAIMI will use to turn on lights and power outlets.

Many of AAIMI's home-automation functions require the ability to switch appliances and lights on or off. This requires two circuits, a relay circuit and a relay-driver circuit.

The relay-driver circuit uses the 3.3V from the Raspberry Pi's GPIO pins (just a few mA) to provide 12V (at around 70mA) for the relay. The relay then uses that 12V to switch the appliance power on or off (up to 10A).

Today we are building the relay circuit. Later in the week we'll build the relay-driver circuit, and I'll provide some Python code to test it.


As usual I am using salvaged parts where possible. You will need the following components.

A relay

A relay salvaged from a CRT TV.

These relays use an electromagnetic coil to trigger an assembly that closes another circuit to activate higher voltage/current appliances.

I get them from old CRT televisions and monitors. They come in either 12V or 5V models. The one pictured above is a 5V unit.

I'm actually using the more common 12V relays. The power supply for my base station already has 5V and 12V power rails, which makes 12V relays a suitable choice for me. If you only have the 5V power you are using to power the Raspberry Pi you may be better off using 5V relays powered from the same power source.

You should choose the same voltage units for all of your relays so they can all be driven from the same relay driver.

A PCB board

The top of a PCB board.

You can buy these for less than a buck online and cut them in half to make several relay boards.

A LED and resistor

You don't have to use an idicator LED on your board but it's handy for testing and troubleshooting without appliances plugged in.

You will need to add a current limiting resistor inline with the LED (around 600 Ohm).

Terminals for AC cables

Cables and terminals to connect the AC power to the relay.

The relay is installed in line with one wire of the light or appliance you are powering. The best option would be to mount screw-terminals directly to the board. In my initial prototype my relays are mounting directly above the power points and light switches so I have attached a pair of short cables with screw terminals on the ends. These tuck into the wall unseen behind the switch or power point to intercept the active AC cable.

Header pins

Header pins to connect the 12V relay power. A cable to connect the 12V relay power.

You could use screw terminals for the 12V input but I chose to use pins for this prototype. I have plenty of two-pin cables from inside TVs that can use these pins to neatly connect the relay board to the relay-driver board. For more distant units I will use screw terminals.

The circuit

Below is the circuit diagram for our relay board.

A circuit diagram for a relay board. Picture created with CircuitDraw by Anthony Hartup.

The 12V power for the lower pins will come from the relay driver we build next week. The upper pins will connect to the appliance.


The relays are easy to figure out. They have four pins underneath, two of them evenly located on one end, and the other two in staggered formation on the other end.

The pin configuration for a relay. Picture: Anthony Hartup
The pinouts for connecting the relay. Picture: Anthony Hartup.

The two evenly alligned pins are the 12V connectors to activate the relay coil. They work regardless of polarity, so connect your positive wire to whichever side you plan to mount your LED, and the GND to the other pin.

The two staggered pins on the other end are for your appliance power. They will intercept inline with the active wire in your appliance's power chord.

To mount the relay, align the pins with the holes in the PCB. The staggered pin doesn't exactly match the corresponding hole in the board, so angle that in first then allign the other three pins and push the relay down. Bend the four pins over to hold the unit in place while you solder wires or terminals on.

Solder one end of the R1 resistor to the 12V positive pin of the relay and route the other end to positive side of your LED. Solder a wire from the GND pin on the LED to the Ground connection on the PCB.

Testing the unit

You can test the relay circuit without anything attached to the appliance connectors.

Testing a relay with a 12V power source. Picture: Anthony Hartup
A simple circuit to test a 12V relay. Picture: Anthony Hartup

Simply attach your 12V connectors to a 12V power supply and turn it on. You should here a click as the relay engages and your LED should light.

You can now connect your appliance and try again.

. A circuit diagram for a relay board. Picture created with CircuitDraw by Anthony Hartup.

This time when you connect the 12V power the relay should click and your appliance should switch on.

Building a box

To safely use 240V power with your relay you need a solid enclosure. The last thing you want is someone grabbing one of those connections.

For my prototype I knocked up a quick box and sprayed it black to match the base-station.

A relay box to turn on 240V appliances from your Raspberry Pi. Picture: Anthony Hartup
A relay box to turn on 240V appliances from a Raspberry Pi or Arduino.

This is a bit bulky for what it does but I will be adding a sensor array on the front later to make it a full AAIMI room controller, so I wanted a bit more room in the front for additional cabling. I chose to use a perspex top so I could see the LED for testing and troubleshooting.

The rear mains plug for the relay box salvaged from an LCD monitor. Picture: Anthony Hartup
The rear mains plug for the relay box salvaged from an LCD monitor.

Using this plug makes things safe and convenient. I always keep these plugs when I find them during my series of teardowns, as they allow you to use any desktop computer cable to connect the box to the wall

The input wiring for the relay box. Picture: Anthony Hartup
The input wiring for the relay box.

The plug has three wires of suitable length for this project. The colors in this case don't match the standard here, but the earth is easily identified by the yellow stripe. From the pinouts I determined the white wire was the live wire and the black was neutral.

The relay box with the relay connected. Picture: Anthony Hartup
The relay box with the relay connected, ready to attach the power point.

The neutral and earth wires attach directly to the power point. The live wire connects to the power point via the relay circuit.

The image above shows the relay in place and the three wires awaiting connection to the power point.

The top view of the relay box with the LED indicator. Picture: Anthony Hartup
The top view of the relay box with the LED indicator.

I cut the see-through cover from a broken plasma TV screen. I think it adds a nice touch. After screwing that down the project is complete.

Where to next?

We now have a functional relay circuit, but we are not ready to connect it to the Raspberry Pi yet, because the Pi's GPIO pins only push 3.3V and our relay circuit requires 12V.

A relay driver circuit. Picture: Anthony Hartup

The next build in this series is a relay driver circuit to take the 3.3V from the Pi and safely route 12V to the relay. For this circuit we will use a ULN2003 transistor array that can drive seven relays.

After that I will show you the Python code to run the system from a Raspberry Pi.



Previous: Use a Movement sensor with your Raspberry Pi

Next: Build a Relay-Driver Circuit



Leave a comment on this article

Leave a comment on this article