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!
Step 3: The Board
Here is the PDF that I used for Toner Transfer PCB manufacturing - As long as you print the sheet out in full size, then you won't have any problems with size.
Also included is the parts overlay, for the top and bottom of the PCB. As was suggested, I re-wired the protection diode across the coils - so the PCB changed, but not enough to warrant replacing the parts overlays!
There is a small number of surface mount components on the board, but, like the Arduino Ethernet controller, their use is minimal.
I have also loaded up a PNG of the board from KiCad. It is sooo pretty!
Step 4: The Software
Here is the .pde sketch file.
Just download this sketch and load it into your AtMega328 chip and you are off and running!
The code has almost exhausted the RAM capacity of the ATMega328 - if you decide to make any changes, you will need to be very careful not to let the buffers corrupt the stack space.
All of the code is commented - and should be able to be followed.
An update from 4 years after the project was made:
Ahh - It looks like the original site that held the ethershield library has dissipated into the ether, and the arduino IDE has changed a lot. (I wish that they would not change the IDE that much to break the older code)..
There are two alternatives:
https://github.com/jcw/ethercard - it looks like it is supported for Arduino 1.0
https://github.com/thiseldo/EtherShield - it looks unsupported, but at least its there :-)
I can not comment right now on either of these libraries as I have not used them.
Step 5: Construction
Start assembling the board by installing the components that are mounted on the back of the PCB - The 74HC08, 4 x 18pF Caps, 4 x 10uF caps, 3 x 0.1uF caps. These are all surface mount, and are surprisingly simple to solder. It just takes patience.
Next, install the wire jumpers - there are a total of 6 jumpers to install. I can only make single sided PCBs at home, so this is a small price to pay for convenience.
After that, install the connectors, and IC sockets. Followed by the crystals, voltage regulators, diodes, and transistors.
Next, get a short length of cut off resistor lead, and install the ferrite bead, and the MagJack. Note that this is a socket with magnetics installed. Not just an RJ45 socket.
Finish by installing the LEDs, and relays.
There you have it - a competed controller board.
Once you have finished building the board, load the sketch up using the Arduino IDE, and a FTDI USB-232 cable from Sparkfun. The FTDI cable is connected to the 6 pin header on the right side of the PCB.
Step 6: Parts List
The following parts are needed to construct this project;
Surface Mount parts:
1 x 74HC08 SO16 package
4 x 18pf Caps 1206 package
4 x 10uf/16V caps 1206 package
3 x 0.1uF caps 1206 package
Through lead parts
6 x 470R resistors
11 x 270R resistors
4 x 50R resistors (51 ohm is fine)
3 x 10K resistors
1 x 2k7 resistor
6 x BC547 transistors
1 x 78l05 Voltage regulator
1 x 78l33 Voltage Regulator
1 x 16Mhz HC18 crystal
1 x 25Mhz HC18 crystal
7 x 1N4004 diodes
2 x 3mm blue LEDs
6 x 5mm Yellow LEDs
1 x 5mm red LED
1 FX18 Ferrite bead
1 x Microchip 28J60 Ethernet controller (DIP)
1 AtMega 328 microcontroller (DIP)
6 x 3 pin 5mm spacing screw connectors
1 x 2 pin 5mm spacing screw connector
1 x 6 pin header 0.1 inch
1 x 4 pin header 0.1 inch
1 RJ-45 Mag Jack (From Sparkfun)
1 x 4 way dip switch (optional)
1 x small pushbutton
2 x 28 pin IC sockets
6 x Jaycar FRM1-1C1 SPST 12V DC relays
Ethernet Relay Controller PCB
Step 7: Using the Controller Manually
OK - Now that you have the controller built, and the software installed, just turn it on.
The default program will run. It just cycles through all of the outputs, alternately turning them on and off. It does this once per second.
Plug the board into your computer, make sure that your computer is on the 192.168.1 network, and assuming that you didn't change the sketch to supply a new address, connect your web browser to http://192.168.1.2. I use firefox - I have not tested it with Opera, Chrome, or IE yet.
The screen shown as the first screen in this step will appear.
First things first, there is a timer running in the controller all of the time - every second, it refreshes the relay to whatever the program has. to use the relay board in manual mode, you will have to turn off the timer - just click on the Timer Off link - and it will stop. The fact that the timer is stopped will be written to the EEPROM, so when the board is turned on next, the timer will be off. You will need to explicitly turn on the timer again to get the board to run automatically.
Next, you can toggle the state of the relays, simply by clicking on the corresponding toggle command. Operation is instantaneous, so the output will change as soon as you select it!
Have a play with the options, until you are comfortable with how it works.
Step 8: Setting Up a Program
Now - we can set up a program in the controller.
Before we do that, lets turn on the timer again, so that the automatic functions can operate.
Then, select "Program 0-6" at the top of the screen to view the current program states 0 through 6. The screen below will be displayed.
The program shown uses 2 states;
State 0 which turns on relays 1 3 & 5 for 1 second then jumps to state 1
State 1 which turns on relays 2 4 & 6 for 1 second then jumps to state 0
Round and round it goes....... alternating every second.
Lets modify it so that state 0 turns on relays 1 3 & 5 for 10 seconds, then goes to state 1.
Just type 0 in the state selection box at the bottom and push 'Modify". This will cause the modify screen to be shown. It is also shown below.
In the modify screen you are shown the state (or step) that you are changing, and you can alter the wait time (between 1 and 65000 seconds), the step to jump to (between step 0 and step 19, and what outputs to turn on for this step.
Just make your changes by entering 10 for the wait time, 1 for the next state, and select relays 1, 3 & 5. Then push the Modify button - Your step will then be saved, and you can re-select the "Program 0-6 tab" to see the whole program.
The steps will automatically be saved into EEPROM on the micro, so that your program will run automatically by itself as soon as the board is powered up.
Step 9: Conclusion and Next Steps
There you have it - an extremely useful little board.
With 20 program steps available and delay timers of up to 65000 seconds (a little over 18 hours), there are almost limitless possibilities to what the board can do...
And - finally, with great power comes great responsibility (tm) - You will have noticed that there was no login option - there is no security - don't connect this to the big bad Internet and expect people to not fiddle. The board does not understand routing anyway, so you would need to NAT any connection from the board.
Also - it is possible to setup state 0 - turning off all relays - and jumping to state 0 - Guess what that does??? Yep - nothing - again and again and again - but that was what you typed in :-) But - remember , you can always type in a new program.
Where to next? Well - I am glad that you asked that - I have a couple of other jobs to do around the house (an my mother in laws house as well) - but when they are finished, I will be providing an input module - that should extend the project to provide Programmable Logic Controller functionality - such as - If Input 3 is on, and 4 seconds have passed, dont go to step 5, go to step 9 instead. There is a 4 pin expansion connector in the middle of the board fo that capability to be just plugged in.... Given time we shall see.
I hope you enjoy building and using it as much as I enjoyed designing it.
Finally, if you really want a kit for the project - or completed boards, send me a message - I will see what level of popularity the project has and will provide kits if there is sufficient interest.
Participated in the