First steps with OpenPLC and Arduino Mega

May 24, 2024 by
Boot & Work Corp. S.L, Arnau Tena

OpenPLC is an open source software that allows us to program hardware platforms like Arduino and Raspberry Pi in more widely used ways, like Ladder Logic or Structured Text. This can be very useful for people with previous experience programming PLCs, but who have never coded in C++ or Python.

Requirements and setup

The OpenPLC sofware consists of two main parts, the OpenPLC Editor and the OpenPLC Runtime. For the simple test we are about to do we ony really need the OpenPLC Editor. You can download it here:

OpenPLC Editor

Test

After installing the OpenPLC Editor, open it. If it's the first time you open it it will take a while to load. With OpenPLC Editor running, we can begin our first test. We will attempt to control the built-in LED of an Arduino Mega board by making it blink at regular intervals.

We will use one of the existing examples as a template. Navigate through the menus to open the Blink example.


This is what you will see after opening the example:

There's not much to see. To actually see the loaded example you have to double click on "Blink" in the "Project" window (upper left).

Now we can see the ladder diagram of the blink example in the middle of the screen. On top of the ladder diagram there's the variables table. If you ever feel like the surrounding windows don't let you see the ladder diagram and the variables, you can press F12 to get a better view. To go back to the default view you can press F12 again.

IEC 61131-3 addressing

The blink example as it is right now will toggle the bool variable named "blink_led" every 500ms. We want to apply that same logic to the built-in LED of the Arduino Mega board, which is wired to the PWM 13 pin, but we can't just assign the pin13 to the "blink_led" variable. To understand why, we first have to understand how OpenPLC maps the board's IOs.
OpenPLC references all pins using the IEC 61131-3 addressing. This means the pins are always named the same, no matter what board you are programming on. In case of the PWM 13 pin of the Arduino Mega, it is referenced as %QW11. The W in %QW11 means it's analogic, which is incompatible with the bool variable "blink_led".


Editing the diagram

What we will do now is add the needed analogic logic on top of the existing digital one. We can make it so that when "blink_led" is activated, we send a high signal to the %QW11 output. First add a new variable to the variable list by pressing the green + sign. Name it "ledMega" by double clicking its cell, make it "UINT" and then assign the location to %QW11. The variables table should look like this:

For the next steps we will be using the tool marked in red:

First select the block tool and click somewhere under the "blink_led". A new window will appear, search for the MOVE block and enable "Execution control" before finally placing it. Then connect the EN pin of the MOVE block to the Q pin of the TOF0 block and connect the ENO pin of the MOVE block to the GND.

Now select the variable tool and click somewhere to the right of the MOVE block. In the new window, select "Output" for the class and "ledMega" for the expression. Then connect it to the OUT pin of the MOVE block. Now add another variable but to the left of the MOVE block, and select "Input" for the class and 65535 for the expression.

When the MOVE block recieves a signal through its EN pin it will then send to the OUT pin the signal it recieves from the IN pin, so "ledMega" will recieve 65535 when "blink_led" is enabled. 65535 represents the maximum possible value for any analogic output in OpenPLC, with 0 being the minimum.

We now need to send a 0 to "ledMega" when "blink_led" is disabled. To do this, just repeat the same structure but before the TON0 block. Then replace the 65535 with a 0. It should look like this:

Before we upload the code to the Arduino Mega, we can simulate it to make sure it works as expected. Click on the blue person to start the PLC simulation:

Then click on the glasses to start debugging the instance:

In the ladder diagam you'll now see the signals in green and the values in purple. You can also debug specific variables by clicking on their glasses icons:

You should see the value of "ledMega" changing every half a second, from 0 to 65535. You can stop the simulation by pressing the red STOP button. If everything works as expected, you can now connect the Arduino Mega to your computer and upload the compiled code. To start this process, click on the orange processor button:

Then for "Board Type" select "Arduino Mega" and select the correct COM port. Now you can click on "Transfer to PLC" and it will compile and upload the code to the board. It may take a while, but when it finishes you should see something like this:

The built-in LED should now be blinking every half a second.


Next, if you want to learn how to use Modbus with OpenPLC, follow this link:

Modbus on Arduino Mega with OpenPLC

​Search in our Blog

Boot & Work Corp. S.L, Arnau Tena May 24, 2024
Share this post
Tags

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

PLC Comparison