Arduino as a programmable logic controller (PLC) - Open Electronics - Open Electronics
Arduino as a programmable logic controller (PLC) - Open Electronics - Open Electronics
Home > Open Source Projects > Arduino as a programmable logic controller (PLC)
POPULAR LATEST COMMENTS
from sensors and to actuators, reducing the EMI which may damage the microcontroller) and an Posted 14 years ago
218
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
Localizer With SIM908
studying this fascinating world without spending a bunch of money on materials and training. Module
The device is based on a GSM/
To turn Arduino into a Programmable Logic Controller, there are two approaches. The first one is to GPRS module with...
write our program using KOP language (ladder). To do that, we must use two more applications in Posted 12 years ago
187
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 GSM GPS Shield For
guide we’ll consider only the DIGITAL I / O with no special features. The second method is to use Arduino
plcLIB (a library we suitably modified to take advantage of the IO shield coupled with Arduino UNO) Shield for Arduino designed
so that you can edit our project code with a language similar to AWL (instructions: IF, AND, OR, …) and based on the module...
having the control on timers and other functions; Here, too, our attention will be focused exclusively Posted 12 years ago
181
on using digital I / O without dealing with specific functions.
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
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
EN Ok Privacy - Terms
1 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
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:
– OUTPUT:
ROLLER: YAVVOLG
UNROLLER: YSVOLG
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.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
EN Ok
2 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
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.
The whole procedure shall be repeated for all the remaining contacts and equations.
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”.
3 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
“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:
• Filename: ladder.h
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
Then we can modify the “ladder.h” library with this pinout setting, as shown in “Code listing 1” (the
code lines highlighted in yellow are the ones to be modified, without affecting the remaining parts)
#ifndef LADDER_H
#define LADDER_H
#define EXTERN_EVERYTHING
#define NO_PROTOTYPES
void PlcCycle(void);
4 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
#endif
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Once youEN
changed the library as just shown, we save the changes without
Ok modifying neither the
name nor the file extension, keeping it always on your “Desktop”.
5 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
At this time we need to create the file “pinmap.ini”. In this file, we have to declare the names of our
variables and the respective pins. This step may also be skipped but we do not guarantee proper
operation of the program without.
The changes to this file are closely related to our project and must be consistent with the ladder.h
code (Listing 2).
; This file contains mapping between variable name in the LD and actual
; pin number of the Arduino.
; SEN_VENTO on pin 2
SEN_VENTO = 2
; FINE_COR_A on pin 3
FINE_COR_A = 3
; FINE_COR_S on pin 4
FINE_COR_S = 4
; IN_CREP on pin 5
IN_CREP = 5
; AVVOLG on pin 8
AVVOLG = 8
; L_AVV on pin 9
L_AVV = 9
; SVOLG on pin 10
SVOLG = 10
; L_SVO on pin 11
L_SVO = 11
; L_RIP on pin 12
L_RIP = 12
Now we can move our four files 1.”ladder.ld” 2.”ladder.cpp” 3.”ladder.h” 4.”pinmap.ini” to a single
folder called “ladder”. Then move this folder to (W7) C: \ programmiX86 \ Arduino \ libraries (or for
older editions of Windows (XP) to C: \ Program Files \ Arduino \ libraries).
Open or restart the Arduino IDE and write the following code (NOTE: it is not intended for using of
timers or other special functions).
From the menu “SKETCH”, we select “import library” and choose “ladder”. Now, type the following
code, which is valid for all projects similar to this:
#include <ladder.h>
void setup()
PlcSetup();
void loop()
PlcCycle();
delay(10);
We use
Now, cookies
please clicktoon
ensure that we give check”
the “verification you theof
best
ourexperience
code and on ouruntil
wait website. If you continue
the sketch has beento use this site we will assume that you are happy with it.
compiled.EN
We should get a positive result, if not please follow again the Ok instructions above
systematically.
6 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
After the compilation is finished you can click on the icon on the side, to transfer our program to
Arduino; wait until it’s loaded and … Congratulations: you’ve managed to turn your Arduino into a
controller very similar to its rival PLC. Now you just have to interface your system, respecting the
logic that we have established for inputs and outputs.
Attention: When you create a new project, before importing the new folder (whose name must
always be “ladder” and it shall contain the files with the same name as before), you must remove
from the directory the previous project folder. In case you want to keep it, just rename it and save it
in another directory. If you want to restore it, simply call it “ladder” and repeat the process in reverse.
For more information and further clarifications, please connect to the site http://
electronics4dogs.blogspot.it/2011/07/ldmicro-and-arduino.html
Methodology 2: plcLIB
To follow this approach you must download the suitably modified library from our website.
The plcLIB library that we have modified to optimally interface the IO shield with Arduino UNO, is
dedicated to those who want to start studying the world of automation without having to “hack” the
code too.
Simply download from our website the library plcLIB and once unzipped, paste the folder in “C:
\programmiX86\arduino\libraries” for Windows 7 or to “C:\ program files\arduino\libraries” for older
Windows versions (XP).
Even in this case, our discussion will be mainly focused on digital logic without using special
functions even if, respect to LDmicro example above, in this case we could easily take advantage of
certain features such as timers.
Once you have imported our library as described above we will not have to change it, regardless of
the project we are going to realize. Just edit our code taking into consideration some factors:
By reusing the Boolean equations listed above, we write our code in the Arduino IDE: in this way,
i.e. considering them as real equations respecting the order of operators (calculating parenthesis
first and then the rest).
#include <plcLib.h>
void setup()
{
setupPLC();
}
void loop()
{
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
inNot(X1);
EN
orNotBit(X4);
Ok
andBit(X2);
7 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
andNotBit(Y4);
out(Y1);
in(Y1);
out(Y2);
in(X4);
andBit(X3);
andNotBit(Y2);
andBit(X1);
out(Y3);
in(Y3);
out(Y4);
inNot(Y2);
andNotBit(Y4);
out(Y5);
Once you have edited this code, simply click on Verify and wait for the IDE to compile the sketch:
we should get a positive result, otherwise revise the instructions step-by-step. Once the compilation
is complete click on “Upload” to program the Arduino. Wait until the loading is completed and
CONGRATULATIONS! You have turned your Arduino to PLC.
Warning: This library has been modified and tuned by us to take advantage of the Arduino UNO
digital I/O. In addition, for a new project, you should only change the code enclosed in the void loop
() {…}, maintaining unchanged the previous part. You can save your sketch without changing the
library: an operation very advantageous from a practical and timesaving point of view respect to the
LDmicro example. With this method, you cannot use the GUI “LADDER” but you can fully exploit the
logic of plc, with a considerable saving of time and reducing the risk of compilation errors or code
mismatches without the need to switch to other software. In fact, a good programmer is someone
who is able to create a good product regardless of the SDK or GUI he is using: the important thing is
that the algorithm logic is always the same.
More information: please note that the pins mapping of our library has been modified to interface
with the IO shield and Arduino UNO. The notions taken from this site are valid only if you used the
original library found at the link below so that all special functions are available and explained. Our
library has been adapted for the IO shield, so some features that you might find at the link below
might not work properly due to the adaptation done, and then we do not guarantee the proper
working of all the functions mentioned by “electronics”. However, we guarantee that the timerOn
function works fine (we tested it successfully). If you wish to try all the other functions, you may
download the original library and follow the step-by-step instructions from the linked website.
void setup() {
setupPLC();
}
void loop() {
in(X1);
timerOn(TIMER0, 2000);
out(Y1);
}
void setup() {
setupPLC();
void loop() {
in(X1);
timerOn(TIMER0, 2000);
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
out(Y1); EN Ok
8 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
Learn more and download the original library plcLIB without our changes (here is their link):
– www.electronics-micros.com/software-hardware/plclib-arduino/
– www.electronics-micros.com/resources/arduino/plclib/plcLib.zip.
In figure, you see the wiring diagram of sensors and the motor of the sunshade (220V single-phase
motor with two-way rotation versus). NOTE: Since the wiring diagram have been used for potentially
hazardous voltages, we take no responsibility in case of accident, injury to persons or property
resulting from a misinterpretation of the wiring diagram or by any inopportune change from your
side.
In this section, we deviate a little from the world of electrical engineering and automation PLCs and
take a step towards electronics. We propose a second plcLIB library we suitably modified to take
advantage of our IO shield so to have three digital inputs, three PWM outputs, six inputs and six
analog outputs.
Unzip the file PlcLIB.zip and copy the folder to the directory “….\arduino\libraries”
Attention: If you installed the library indicated in the previous path, you have to cut and paste it into
another directory to avoid conflicts with this (keep it if you want to create a purely digital project, you
shall use the library specified in the methodology 2). In case you want to build a prototype that
integrates both digital and analog/PWM controls, use the library indicated in this paragraph.
Table shows the mapping of inputs and outputs adapted to our IO board.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
EN Ok
9 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
We remind you that on analog inputs, the maximum voltage and current allowed are 5V 100mA.
Here is an example application; we want to dim a led (L1; AY1) according to the value assumed by
potentiometer (R1; AX1) in real time.
The Arduino solution, easily understandable after studying previous paragraph, is shown on listing:
#include <plcLib.h>
void setup()
setupPLC();
void loop()
inAnalog(AX1);
outPWM(AY1);
After verifying the code, we can upload to Arduino and…. Good job! You succeeded in interfacing
the analog world with Arduino-based PLC!!
From Store
Arduino UNO
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
EN Ok
10 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
About
RELATED POSTS
17 COMMENTS
LEAVE A REPLY
Your email address will not be published. Required �elds are marked
Comment
Name
Website
Post Comment
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
EN Ok
11 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
EN Ok
12 of 13 3/12/2024, 12:45 am
Arduino as a programmable logic controller (PLC) - Open Electronics... https://www.open-electronics.org/arduino-as-a-programmable-logic-c...
we share exciting open projects and create amazing using the Contact Form source code are instead provided according to
products! a Creative Commons Attribution-ShareAlike 4.0
is not just a container of ideas: Unported License.
it is also a web site lead by a team of engineers and
geeks who will take part in the discussions and give
support.
is to become a reference Open Source
hacking site with ideas and feedback aimed to enrich
the community.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
EN Ok
13 of 13 3/12/2024, 12:45 am