Followers 0

# Combine Arduino and programmable logic controller (PLC)

## 1 post in this topic

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 TMD27713(datasheet:) , 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.

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:

– INPUT:

WIND SENSOR: XSEN_VENTO

LIMIT SWITCH ROLL: XFINE_COR_A

LIMIT SWITCH UNROLL: XFINE_COR_S

TWILIGHT SWITCH: XIN_CREP

– OUTPUT:

ROLLER: YAVVOLG

LED ROLLER: YL_AVV

UNROLLER: YSVOLG

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.

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”.

We choose to save the file on “Desktop” with these settings:

• Save as: All Files

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

In this exercise, we have chosen to set the pin configuration as follows:

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)

```/* This is autogenerated code. */

/* (generated Wed, 13 Nov 2013 14:06:53 +0100 by ladder-gen v 1.0) */

#if ARDUINO >= 100

#include "Arduino.h"

#else

#include "WProgram.h"

#endif

#define BOOL boolean

#define SWORD int

#define EXTERN_EVERYTHING

#define NO_PROTOTYPES

void PlcCycle(void);

/* Configure digital I/O according to LD (call this in setup()). */

inline void PlcSetup()

{

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);

}

/* Individual pins (this code is used in ladder.cpp) */

{

}

{

}

{

}

{

}

inline void Write_U_b_YL_SVO(BOOL v)

{

digitalWrite(11, v); //TODO

}

{

}

inline void Write_U_b_YAVVOLG(BOOL v)

{

digitalWrite(8, v); //TODO

}

{

}

inline void Write_U_b_YL_AVV(BOOL v)

{

digitalWrite(9, v); //TODO

}

{

}

{

}

inline void Write_U_b_YSVOLG(BOOL v)

{

digitalWrite(10, v); //TODO

}

{

}

inline void Write_U_b_YL_RIP(BOOL v)

{

digitalWrite(12, v); //TODO

}

#endif```

Once you changed the library as just shown, we save the changes without modifying neither the name nor the file extension, keeping it always on your “Desktop”.

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 &lt;ladder.h&gt;

void setup()

{

PlcSetup();

}

void loop()

{

PlcCycle();

delay(10);

}```

Now, please click on the “verification check” of our code and wait until the sketch has been compiled. We should get a positive result, if not please follow again the instructions above systematically.

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.