Arduino as a programmable logic controller (PLC)
Today we’ll explain how to exploit the potential of Arduino as a programmable logic controller, connecting it to appropriate interfaces for I/O.
The PLC (Programmable Logic Controller) has been and still is the basic component of the industrial automation world. The Industrial application made the PLC systems being very expensive, both to buy and repair, and also because of the highly specific skills requested to software designers to extract the maximum potentials from controllers. Arduino is a kind of universal programmable controller, although it is only the “core” and in any case it has been built for general applications; with a little of external hardware (essentially interfaces capable of transferring signals from sensors and to actuators, reducing the EMI which may damage the microcontroller) and an appropriate software may, however, become something very similar to a PLC.
In this tutorial we will explain how to “convert” our Arduino board in a PLC-like controller, programmable through the PLC proprietary language and logic, helping those who wish to start studying this fascinating world without spending a bunch of money on materials and training.
To turn Arduino into a Programmable Logic Controller, there are two approaches. The first one is to write our program using KOP language (ladder). To do that, we must use two more applications in addition to Arduino IDE: the first is LDmicro that is the editor and compiler of our ladder code (it can be downloaded from http://cq.cx/dl); the second consists of a web page that will help us creating the code for the ladder.h library (http://adam.horcica.cz/tools/ladder-gen); for simplicity’s sake, in this guide we’ll consider only the DIGITAL I / O with no special features. The second method is to use plcLIB (a library we suitably modified to take advantage of the IO shield coupled with Arduino UNO) so that you can edit our project code with a language similar to AWL (instructions: IF, AND, OR, …) having the control on timers and other functions; Here, too, our attention will be focused exclusively on using digital I / O without dealing with specific functions.
A practical example
As an example to understand how to take full advantage of the two methods described above, we will try to solve a problem of home automation found on the web: automate electric sunshades. Then we will proceed with a step-by-step explanation of the two methods described above. Briefly, we want to control the sunshades so that in case of a strong wind they must be automatically retracted, while unrolled only after the wind has calmed down. The behavior should be similar for the different lighting situations: roll them during the night and unroll on daylight, but obviously the wind conditions should prevail over the lighting.
As a possible solution we could use a real PLC, but given the simplicity of the algorithm and the high cost of that PLC, we will use the hardware shown above.
We will use two sensors, twilight NO (normally open) and wind NO that will be connected to the IO shield. In addition, we will have to adapt and change the power scheme of the sunshades engines so that Arduino could manage them.
Method 1: LDmicro
Before writing the ladder code (contact diagram), similar to that in figure, we need to download the LDmicro executable from the link http://cq.cx/ladder.pl. Once downloaded and saved to your desktop, just double click on the LDmicro icon. Now, before proceeding with the application of ladder diagram, we have to write a draft of the program that we want to create.
Since the program is very simple and we use few variables only, we can use the most basic and immediate programming approach for the PLC: the wired logic. This methodology consists of simple Boolean equations that will always return as output (after the equal sign) a value that can be 0 or 1. The only operators used will be the AND (series), the OR (parallel) and NOT (negation).
Input and Output variables in our program shall be the following:
WIND SENSOR: XSEN_VENTO
LIMIT SWITCH ROLL: XFINE_COR_A
LIMIT SWITCH UNROLL: XFINE_COR_S
TWILIGHT SWITCH: XIN_CREP
LED ROLLER: YL_AVV
LED UNROLLER: YL_SVO
IDLE STATE: YL_RIP
Note that the Arduino output pins have not yet been declared yet, while the “x” and “y” placed at the beginning of each variable represent respectively the input and the output. The software adds this letter automatically during the coding.
Once declared the variables, we can proceed on writing the logic equations, taking into account that Arduino with our shield reads all inputs as HIGH (1) when the contact is open and LOW (0) when the contact is closed: therefore we must negate the logic of all the inputs to ensure that they are properly executed.
Now that we have our Boolean equations, we can back to LDmicro. By pressing the “C” key or clicking on the “instruction->include contact” menu, we get the inclusion of an open contact. Now, without changing anything, by pressing “L” on keyboard (after placing the cursor after Xnew, on the right side), we should obtain a segment identical to that of figure.
Moving the cursor below Xnew (by using the arrow keys on keyboard) and pressing “C”, we will get a new segment, always named Xnew but in parallel with the previous one; now, we must click on Ynew and putting the cursor vertically on the left (before) of the output, we have to press “C” twice. By doing this we have added two more contacts, in series with the two (parallel) previous ones. To finish up, we just need to click on Ynew (but this time positioning the cursor below the output symbol) and then press “L”: so we have added another output in parallel to the existing Ynew.
Now we need to modify all contact names accordingly to those used in the equations. To do this we simply double click on the first Xnew contact we have created, then we set the name and contact type in the pop-up shell. We will assign the name “SEN_VENTO” and select “INPUT PIN” and “|/| NORMALMENTE CHIUSO” (NC). Remember that the software automatically assign the initial letter “X” or “Y” whether the variable stands for INPUT or OUTPUT, respectively.
To insert a new segment, click on “Edit->Insert segment behind”. After having completed the configuration, from the menu “settings” we shall select “Microcontroller->ANSI C Code”, then we can compile choosing from menu “Compile->Compile as”.
We choose to save the file on “Desktop” with these settings:
- Save as: All Files
- Filename: ladder.cpp
After saving the file, a popup windows will warn us to configure the I/O addresses map: we click on “OK”, save our LDmicro project (menu File->save as) always on Desktop naming it as “ladder.ld”.
Then we need one-step further. Open the ladder.cpp file with notepad, select all the text and copy paste it on the website http://adam.horcica.cz/tools/ladder-gen. After clicking on “Generate”, we will get a new code. Copy and paste this code to a new notepad file, saving it with the following parameters:
- Save as: All Files
- Filename: ladder.h
For more detail: Arduino as a programmable logic controller (PLC)
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w
This Post / Project can also be found using search terms:
- arduino programmable logic controller irvine
- Arduino as PLC
- arduino logic controller
- programble logic cells in Arduino