I spent years in industrial plants working with PLCs. They are great, off the shelf, upgradeable and best of all they use LADDER LOGIC. I love them. So one day I decided to build one.
I originally planned to write ladder logic PC software for this unit, but that proved to be beyond my programming capability so this is ended up being just a hardware project. The code example is very simple and basically initiates the hardware. Now if I ever buy my own place (instead of renting) I will probably write more advance software for the PLC.
So one day I realized that I used PLCs all the time at work BUT I never actually looked at the inner guts of the things.
So I decided to sit down and build a little PLC of my own. At the time I had very little experience building PCBs (since industrial Electrical Engineers tend not to do circuit board design). So I started designing the board in my good old trusty AutoCAD. This proved to be a HUGE pain the <<insert whatever word you want here>>. So I hit the net did a bit of research and discovered EagleCAD. After fighting with Eagle for a few weeks I eventually got use to it and I designed a decent little board. So now I encountered my next problem.
My Electrical Engineering program had an Automated Systems Technologies (ie. Industrial Automation) specialty and like all Industrial Electrical Engineers I had never build my own circuit board. So once again Google to the rescue. I did some digging and found out about the different methods of building PCBs and eventually found the Toner Transfer Method (my tutorial coming ... well eventually). I did manage to find a PCB etching kit at my local Radio Shack however, it did not have enough material in order to create the board I needed and, the FeCL proved to be a pain in the ass. So I started looking for alternatives and I found CCiAHAS. Dispute being honorables at chemistry making some CCiAHAS up was really easy. I was able to get all the materials (pool acid and Hydrogen Peroxide) and managed to whip the stuff up at home. WOOT. And it worked great.
I got some drill bits from Drill Bit City and started drilling....drilling.... more drilling. And a little over a hundred holes later I started soldering. So now I know why people use surface mounted components over through hole components.
Once all of my drilling was done, I taped up the headers and terminal blocks and sprayed the top and the bottom of the board with clear top coat to keep things from oxidizing. The pros use "Conformal Coating" but the stuff proved to be much more expansive.
Originally I wanted to build an 8 input 4 output PLC. However, I ended up with a bit of extra space on the board so I tacked on 2 more outputs. And since the AVR has a ADC and a PWM I decided to add one of each. I also added an ISP (I2C) EEPROM to extend the memory (because I had never used ISP before).
I wanted to be able to be able to switch both AC and DC so I chose to use Relays. In the PLC world the rule is Transistors for switching DC loads, Triacs for switching AC Loads and Relays for general purpose (AC/DC).
All the PLCs that I had ever purchased had Optically Coupled inputs. So my PLC was going to have it as well. Optical Coupling allows signals to be passed by using light. This is important because when using PLCs we often mix multiple power supplies. So our inputs could come from one (or more) power supplies, the PLC has its own power supply. And finally our outputs might be powered by a few other power supplies. While this might sound a bit silly, you have to remember that a single PLC might control several robots (or machines). Having 1 power supply that powers all the robots would be very expansive (2 50 amp power supplies are cheaper then 1 100 amp supply) and if it blows it would take out a lot of hardware with it.
So here is my final SPECS:
|PLC Voltage Input||7V||12V|
|PLC Current Draw (based on all devices activated at once)||400mA||500mA|
|Vin (Voltage of Inputs)||4V||6V|
|Vout (Voltage of Outputs)|| || 120VAC|
|Iout (Current of Output)|| 0.5A @ 120VAC|
1A @ 24VDC
|Pout (Max Output Power)||30W|
Download Project PDFs (Schematic, Toner Transfer Layer and Overlay)
Picture 3: PLC Bottom - POVray Rendering
Picture 4: Real Board - Top
As you could see I even played with POVray on this project. Not that I could remember how I did it now but, I did at one point. As you could see I never did get the relays to appear on the board.
Figure 1: Inputs
So, Remember my Opti-coupler rant ? Well this is how it works. Pin 1 and 2 OKx (opti-coupler) is basically an LED, 1 is the Anode and 2 is the Cathode, R1 limits the current to the LED. LED10 is put in series with the LED in the opti-coupler as a troubleshooting tool. When the LED comes on you know that the input came on. This is a common feature on all PLCs. Pins 4, 5 and 6 is a simple transistor. The Gate however, has a light detecting diode hooked up to it. So when the LED on pin 1 and 2 is activated it causes the current to flow from the Collector (pin 5) and the Emitter (pin 4). INPUT1 is tied to an AVR pin. So the AVR pin will see Vcc (HIGH) signal when the LED is not OFF. And a GND (LOW) signal when the LED is ON. If you want your outputs to run on a different voltage level all you need to do is change the value of R11, my are set for 5V input level so I used a 330ohm resistor.
Now a few people have asked about the ground between the input and the output side of the opti-coupler (between pin 2 and pin4). The 2 sides of the opti-couplers are not usually tied together. In an industrial PLC the inputs power supply would be bonded to the output power supply (their neutrals tied together). The PLC would have its own power supply that would have a good quality isolation transformer for protection. This would basically cause the logic part of the PLC to be totally isolated. My PLC's on the other hand uses a simple 9V switching power supply thus, no isolation. Therefore when I tie my power supplies together (input and PLC) it will cause both sides to ground together anyways. Now some people said that this defeats the point of the opti-coupler. This is incorrect, the opti-coupler still provides us with protection from voltage spikes, shorts or incorrect voltage levels on the input side. Sure the opti-coupler will blow but its better then blowing the entire board. Hope that clears things up, and if you still don't agree with my design please send me a message (email@example.com), I'm not perfect and I love to learn. So in short, if you want total isolation don't tie pin2 and pin4, but if you just want to protect your input from voltage spikes my method works.
The output looks crazy but its actually fairly simple. Lets work backwards on this one. K1 is the coil of the Relay, when it is de-energized the contacts of the relay will will conduct between X13-2 and X13-3. When the coil is energized it will conduct between X13-2 and X13-1. LED1 is in series with the coil, so it will come on when the coil is energized (again a common troubleshooting feature of a PLC). R13 exists in order to limit current to the LED.
So whats with the transistor? Well the coil requires 30-35mA to operate, the AVR pins are limited to 20mA. So by using a transistor I could limit the draw on the AVR output pins to 1mA!!!
This leaves us with D1. Lets keep it simple shall we? Magnetic coils are basically inductors. When the power is cut to an inductor its magnetic field starts to collapse. The collapse induces a voltage and causes some havoc with the current which could damage the transistor. So the diode in reverse polarity in parallel with the coil will give the induced voltage somewhere to go and, drops it over time (remember diodes suck up 0.7V).
I am not going to cover the I2C EEPROM yet. I plan on explaining this in a its own tutorial some time.
REMOTE X-MASS LIGHTS:
Last year, I wired my X-mass lights to the PLC. Extension cords were on sale so I purchased 7 of them, and wired them into a waterproof electrical enclosure. I then built a small adapter board that let me communicate with the PLC via Xbee. (no going outside to turn on X-MASS lights for me).
I then wrote a small program that would read the 1-6 key on my keyboard. Every time I hit the key it would switch the output state. This was great till I started playing a video game. Which caused my lights go crazy.
Since I was going away for a few weeks over the holidays I hacked an Xbee into my old Linksys router and then was able to communicate with the PLC via internet ... which I rarely ended up using because being back home meant drinking with old friends, Hence forgetting to turn on the lights.
Picture 6: X-mass lights Example - Enclosure 1
Picture 7: X-mass lights Example - Enclosure 2
The bad news that I have used this device for several things. So there is no specific software for this project. I can however, include the code for my x-mass lights project, it is very simple and includes all the basics with very little extras.
DOWNLOADS 8x6 PLC Xmass Lights Example Code, AVR Studio 4 Code.
This page was edited while playing Portal 2.