Build Your Own Programmable Logic Controller

[Q] is an Electrical Engineer who works in an industrial setting. He frequently uses Programmable Logic Controllers at work but had never built one himself. He decided to undertake the project at home and managed to build a PLC that outputs 120V AC or 12 V DC and has optoisolated inputs.

On the circuit board you’ll find an ATmega8 and an EEPROM for extra data storage. Six outputs are controlled by relays since they are able to output either alternating or direct current. There are eight inputs which use optical isolators as buffers to protect the microcontroller.

So what did he end up using this for? It was part of his Christmas light setup last year. The image above shows the PLC in a water-tight electrical box with extension cords running to each of the devices he wishes to control. The example code is what he used on the X-mas setup, but it should be enough of a guide to program this to work with just about any application.

28 thoughts on “Build Your Own Programmable Logic Controller

  1. So…basically he wired some relays and extension cords to an ATMEGA chip?

    A device without an intended use is just pointless. Even if you can occasionally plug Christmas lights into it for fun.

  2. And where in his AVR code is the “Logic” from PLC?
    His site states: “best of all they use LADDER LOGIC. I love them. So one day I decided to build one.”
    But he doesn’t, really.

  3. Is the optical isolation done the right way in this project? Both the transistor and the diode have common ground, which means it isn’t really isolated.

  4. @INquiRY

    Though I also did not see reference to ladder logic being used in his code, I have noted a free ladder logic logic compiler for avr & pic:
    http://cq.cx/ladder.pl

    That said, ladder logic is simply a visual perspective and any code if/then logic will accomplish the same tasks. In fact, most commercial ladder logic based plc’s do in fact run their instructions sequentially from the top of the ladder downward.

  5. @McMonster: At best, it will limit an over-voltage on the input reaching the AVR but yep, it’s not really isolated. He may as well have just put a cheaper zenner or transorb.

  6. Hi guys

    I was planning on developing ladder logic programming for this PLC but that proved to be above my computer programming skill level, so I moved onto other things.

    As for the Opti-coupler:

    If you read the writeup on the input circuit:
    “The grounds do not have to be tied together, However, I always like to tie all my power supply grounds together so I decided to do this on the board.”

    When you work with Industrial controllers, you tie the neutrals of all your power supplies together (floating grounds are unsafe) and, then you tie one of those neutrals to earth ground. Because my PLC was mainly designed to mess around with small 5V inputs I decided to simplify things and just make the connections on the board that way they get tied to earth via the power plug.

    In the end the input side of the Opti-couplers do get tied to the power supply in some way (external to the board or right on it).

    “I also agree the common grounding issue is a major design flaw, and somewhat defeats the purpose of the optical isolation.”

    The optical isolation will still provide protection to the rest of the board. Surges or improper voltages on the input will only blow the OK itself but, will not jump to the output side and blow the micro controller or other components.

    Now if I had designed the PLC for industrial use I would have definitely not tied to high and low side of the OK together, as well as protected the power supply by using a good insulation transformer

    Good catch, I like to explain as much as I can, and from the commends it seems that I need to make the OK write-up more clear.

    Thanks for the input, guys I always love talking design, it’s a great way for me to learn.

    Cheers
    Q

  7. @macw

    A PLC is a controller that is purpose built to function in adverse conditons. Extremes in temperatures, humidity, vibration, dust, etc are normal operating conditions for a PLC. Inputs and outputs can be nearly anything you want (DC, AC, or analog, from 5VDC, to 440VAC 3 phase). You don’t buy a real PLC unless you have an actual use for it (learning to use one is a valid use), simply because they are expensive. Also, you can add an arbitrary number of inputs and outputs (up to the limits of the PLC controller), and you can chain a number of them together.

    You CAN do anything with a *duino that you can do with a PLC, but it is all a matter of what you want to do with it. If you need dozens of IO, and need to handle line level voltages, in extreme environments, you will use a PLC. If you need to control highly complex, industrial systems, you will use PLCs.

    *duinos (or other forms of microcontrollers) are better suited to smaller systems. I am sure that others will disagree with me, but if you are designing one machine, use an Arduino. If you are designing the entire plant, use a PLC.

  8. Not sure I agree with your statement “If you need to control highly complex, industrial systems, you will use PLCs.”

    Conventional logic says this is correct. But Arduinos have plenty of power – on par with many common PLCs. They are far less expensive and without too much work are able to be extended out such that they can talk with each other and perform computer based monitoring. What they can’t do is drive a large touch screen or be programmed with ladder logic (sort of anyway).

    Arduinos generally lack *any* 120v outputs but with some solid state or mechanical optically isolated relays, you can control up to 480 volt systems without terrible difficulty – assuming you know what you are doing. The SSR modules are fairly inexpensive and because the arduinos are so much easier to use, easier to program and cheaper, we have embedded them in all sorts of industrial controls. We built a roughly $100,000 anodizing line using 11 of them (mostly Megas) and saved quite a bit of hassle, time and frustration – plus they all talk to each other, control displays, accept inputs and control outputs and perform error detection and process control. In short, pretty much everything we needed. I consider Arduinos to be PLCs, but better in nearly every regard. And I say that as someone who has dealt far more than they want to with Siemens and Telemecanique and other “true” PLCs.

  9. @macw

    You’re right, functionally a PLC is nothing more than a hardened microcontroller.

    But, when you buy a PLC it’s already built and tested. Also, they are quite flexible (you can expand them adding modules). you get a development environment with libraries and testing facilities. They’re ready to talk to SCADA systems by standard protocols. You get vendor support and spares…

    All very valuable things that save time and headaches when doing complex (and often dangerous) stuff.

  10. @macw

    Actually, that was off the top of my head. And you are absolutely correct. I should also mention that I’ve only had one class in PLC’s, and that over a decade ago.

    And I do agree with Hackerspacer. When I said “highly complex, industrial systems”, I was thinking more about the ‘industrial’ than the ‘complex’. PLCs are (mostly) designed for nasty, miserable environments that have neither air conditioning or filtration. And they are installed and maintained by electricians. Most electricians are pretty smart, but they tend to work on “bigger” equipment.

    In my mind, I roughly divide them this way:
    If the connecting wires are 14 gauge and larger, and carry either 48VDC or some type of line voltage; OR are very electrically noisy, you should probably use a PLC. If the conecting wires are 18 gauge and smaller, and won’t handleing more than ± 12VDC, go with a microcontroller.

    Or even more simply put, if your signal wires carry enough current to kill you outright, use a PLC. Otherwise, use a microcontroller.

  11. PLC is just a term that has stuck for many kinds of modular, rail-mount, easily programmable controllers with specific IO needs. PLCs were the next step from relay control blocks.
    So basically, all those discrete blocks (relays, timers, logic) that had to be wired, it could be done with programming just one block via a simple graphical interface.
    If you build a few, simple machines professionally that need some control – use a PLC. For larger scale installations, the availably systems (building blocks) are not easily categorized, but everything goes: PLC with motion control and addional IO modules connected via some field bus, up to embedded real time OSs with graphical process control GUI etc..
    It’s all about standardization – of course, if you are very quick, you can build your own specialized hardware to do the job. But only few people will be able to design, build, program and test a system within the time they can setup a PLC. In addition, a proprietary solution is not easily replaced and maintained while pretty much anyone could replace and reprogram a PLC.

    @Agent420
    I know – there is also a (discontinued?) project called MikroSPS, by the guys who now mainly do the MikroKopter. I think it is able to generate code from a logic diagram made with EAGLE.

  12. Who cares if its overkill or technically not this or that? I think it’s a great project, particularly if you someday get some kids involved in understanding how it all works. Better than playing video games or watching TV IMO, and he probably had the best light display on the block. Beat that Mr and Mrs Joneses!

  13. Microcontroller also take the place of PLC
    Its up to Hardware design we all know PLC is able to work in Extremes in temperatures, humidity, vibration, dust, etc are normal operating conditions.
    but if your Electronic Hardware designer then
    # you can choose a appropriate components for wide rang of operating temperature
    # A good PCB design by considering all EMC standard will allow you to work in Vibration
    and Noisy Environment.
    # For Dust Protection you can use thin coating layer paste which will help you for Dust Protection.
    at last Software Consideration
    # PLC use Ladder ,FDB etc
    # Microcontrollers use Embedded C etc.

    What is Main Difference b/w PLC and MCU based PLC.

    # Ladder logic is mainly design for Electrical Engg.’s who only know NO,NC of relay.

    # Software of PLC are Available Worldwide and any little skilled person who deals with Relay are able to program PLC But not able to program Microcontroller based PLC.

    # PLC hardware consist semiconductor base and MIcroprocessor/microcontroller is RUN inside PLC but User Interface programing is Ladder (for Electrical Engg’s).

    # Microcontroller PLC is also Beat with Commercial PLC like Schneider etc. but only skilled person can program Microcontroller PLC not a person who deals with NO NC

    for more detail :amardeep.singh190@gmail.com

  14. I do PLC development daily for a living. I have worked on power plant systems with 100’s of I/O points and 1000’s of communicated points. The more I learn about arduinos, and other controllers the more I am convinced that the multi $100K systems i work on are not much more powerful (in cases less) than these controllers.I believe most of what you are paying for is the decades of development these companies have invested (MODICON, Allen Bradley, GE, Siemens). M$ windows would be much more expensive if billions of people weren’t buying copies. As for flexibility, I would argue my hands are tied very often because this controller doesn’t work with that protocol. The OS of the PLC is what it is. With C or Linux you will never be told that cannot be done, just that it may take alot of work. There is also very often a patchwork of protocol bridges and media converters to get certain controllers to work with older hardware. There is a much easier entry to control something with an off the shelf PLC (especially if you are replacing relay logic). People who know nothing about CPUs, electronics, or low level programming can be controlling something pretty quickly.

  15. I have a PLC+HMI which is called EasyPLC micro programmed using text language which I call CLARITY which is more like BASIC and can also be called PLCBASIC. If any body is interested please drop me a mail and I can forward you the manual and then you can think of buying it. Thanks

  16. You don’t really need to use a *seperate* ground for the isolator to work as intended . Ground is simply a reference for voltage; if the circuits aren’t connected by anything other than ground it’s fine.

    I’m a bit perplexed as to why he is using an isolator for a 5v DI though.. – It would make complete sense for a 12, 24 or higher voltage DC.

    With a couple modifications it could be used for AC also.

  17. I was reading the project documentation and there is a part were you explain “..LED1 is in series with the coil..” Could you please confirm how is that? By looking at it I thought the coil was in parallel with LED1 and R13.

    Thanks for sharing your project with the community, people like you make the internet a better place.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.