A Remotely Programable Relay Controller (Christmas Lights or Home Automation Controller)
What was that? A Whats-a-Maja-Thimagig?
Yep – One of those! This Instructable extends my Arduino Ethernet controller to control a set of up to 6 relays, but that’s not the neat bit. The neat bit is that there is a web based state machine in the AtMega chip so that the relays will operate in whatever sequence with whatever timing you want – all by themselves.
This allows you to have a set of relays connected to the end of a piece of Ethernet cable that can be accessed from a web browser anywhere from your house.
I needed a system to sequence a set of relays to control some Christmas lights around my house this Christmas, as well as to turn on and off a water fountain, so I thought that a simple controller for it would be an AtMega328 chip. Then I thought about how cool it would be to allow these things to be controlled from a web interface. A little bit of tinkering later, and this project was born. I really only needed 4 relays, but as I was doing the layout, I decided that here was enough space for 6!
This project is an autonomous controller, operating a set of 6 relays according to a pre-programmed pattern, over and over again. If you are impatient, jump to the section on programing the board to see how it is programmed.
In my case, I wanted to control a fountain and some Christmas lights, so my pre-programmed pattern is as follows:
Relay 1 – Front fairy lights
Relay 2 – Fountain Blue Light
Relay 3 – Fountain Green Light
Relay 4 – Fountain Pump
On startup, do the following forever until it is switched off:
Turn on the Fairy lights, the Blue light and run the pump. After 3 minutes, turn off the blue light, and turn on the Green light. After 3 more minutes, turn off the fountain and the fountain lights.
This would be defined in the controller as:-
State0; Relay 1, Relay 2, Relay 4 – time 3 minutes – jump to state 1
State1; Relay 1, Relay 3, Relay 4 – time 3 minutes – jump to state 2
State2; Relay 1 – time 3 minutes – jump to state 0
Relay 1 would be connected to the Fairy lights.
Relay 2 would be connected to the Blue light
Relay 3 would be connected to the Green light
Relay 4 would be connected to the pump.
You can have a completely different program, say for running a gardening facility, where the relays drive various pumps and lights for watering and heating. Whatever you like! The uses are limitless! And the hacking potential is limited only by your imagination.
Step 1: How does the programming work – or What is a state machine?
I am glad that you asked that!
The controller implements a wonderful tool called a state machine. A state machine is one of the simplest controllers that just knows two things; What the current state is, and what will the next state be?
State machines are driven by a table called a state table. Essentially what the controller does, is cycle through the state table one state at a time. Our state table documents the current output settings, and what the next state will be. In our case, it also specifies the amount of time we wait in the current state. Here is an example of a state table that toggles the first 4 relay outputs in sequence, waiting for 3 minutes between cycles;
State 0: 1 on, 2 off, 3 off, 4 off, 5 off, 6 off, Wait 3 minutes – goto State 1
State 1: 1 off, 2 on, 3 off, 4 off, 5 off, 6 off, Wait 3 minutes – goto State 2
State 2: 1 off, 2 off, 3 on, 4 off, 5 off, 6 off, Wait 3 minutes – goto State 3
State 3: 1 off, 2 off, 3 off, 4 on, 5 off, 6 off, Wait 3 minutes – goto State 0
The controller runs in an infinite loop, where it knows what the current state is, waits the required time, and then jumps to the next state as dictated by the table.
You will find state machines in things ranging from traffic lights, through washing machines, through to Industrial controllers.
Step 2: The Hardware
The basis for my project was my Arduino Ethernet controller board – I simply connected 6 switched relays of some spare I/O lines, and designed a new PCB layout because I enjoy the quiet Zen state when I am laying out a new PCB.
There is nothing stopping you from putting a set of relays onto a piece of stripboard, and running them from an Arduino board with an Ethernet shield. The only caveat is that the Ethernet shield has to be based on the ENC28J60 chipset. That is what I support in my code.
Here is the circuit diagram of one of the relay outputs. You can see that each relay is driven using a BC547 transistor. There is nothing fancy about the BC547 (except that they cost about $0.03 in quantity) you can use whatever type of NPN switching transistor you like. There is a current limiting resistor between the base of the switching transistor and the Arduino output to protect the transistor from damage. There is also a spike protection diode across the transistor to protect it from the large electrical spike that occurs when the relay is switched off. There is also a LED installed so that you can visually see the state of the outputs.
You will also find the full schematic diagram for the project here as a PDF. For more information, have a look at my Ethernet Arduino Instructable.
Update – It was suggested that the protection diodes should be placed straight across the relay – so I have amended the pcb layout, as well as fixed the schematic. Thanks for that!
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
JLCPCB Prototype: Only $2 for 10pcs 10×10cm, 24 hours Quick Turn, DHL Delivery in 3 days