DesignSpark Electrical Logolinkedin
Menu Search
Ask a Question

Controlling the LED on a raspberry pi through CoDeSys

The standardization of control logic programming has the advantages of interoperability within products from different vendors and in the process saves a lot of time for the engineers. IEC 61131-3 has greater efficiency through reduced development and training costs, enabling flexibility and providing the user the option to select the device based on their needs and not depending on the supplier. Based on IEC 61131-3, Codesys, a comprehensive open software tool for industrial automation, is designed which allows to create controller applications without hardware and programming as a common platform

It is one of the most powerful IEC 61131-3 programming tool for controllers, supporting all five PLC programming languages (Ladder Diagram, Structured Text, Instruction List, Function Block Diagram and Sequential Function Chart). Moreover, the combination of advanced programming languages, such as C or Pascal makes the handling and operational functions of PLC programming systems much easier.

Here, in this blog, I have shared step-by-step how to switch on the LED connected with one raspberry pi from operating the switch on another raspberry pi connected on the same network through CoDeSys. I will explain every step in details and hope that people starting with codesys in raspberry pi will get a nice insight how the things work.

Raspberry Pi 2

I have used two Raspberry Pi 2 devices in this project. The Raspberry-Pi-2 is a single board computer, developed in UK, by the Raspberry Pi foundation. This is a credit card sized computer, which can perform multiple functions that a normal PC does, such as playing videos, running software, processing word and spreadsheets. The raspberry-Pi-2 contains 4 USB ports, 40 GPIO pins, HDMI port, Ethernet port, 3.5 mm audio jack, Camera interface (CSI), Display interface (DSI), Micro SD card slot and Video Core IV 3D graphics core. The developments from the previous Raspberry-Pi-1 model are the inclusion of a 1 GB RAM and a 900 MHz quad-core ARM Cortex-A7 CPU. Below you will find an image of the raspberry pi devices used.


As you can see, it is an integrated setup, mounted with piface (, a LED and a separate switch which is connected to one of the output pins of the piface. The first step is to setup our raspberry pi. The first steps of setting up the raspberry pi are explained properly here. After we have successfully configured our raspbian, we can now start using CoDeSys.

Downloading CoDeSys and Setting up the Package Manager

We should first download the environment and set it up. To download Codesys, we should go here, create an account and download the .exe or the .zip file of the latest release (V3.5 Patch 3 in this case). Next, we should run the exe file and set up the IDE. It will look like this:


The next step is to install the package for the raspberry pi control in the IDE. To do this, we need to navigate to Tools and then click on Package Manager. There, on the right hand side, we will find an option to get redirected to ‘Codesys Store’. Now, we need to create an account and install the package ‘Codesys Control For Raspberry Pi’. The download is free and runs for 2 hours at a stretch. Now, you can check whether your package is installed from this screen:


After this, the next important step is to setup the runtime environment in our raspberry pi. In the latest releases of Codesys, this process has been made very simple and takes only a couple of minutes. We should click on ‘Tools’ and click on ‘Update Raspberry Pi’. Here, we should put in the username and password of our raspberry pi and give the correct IP address of our Pi. It is always possible to find out the IP address by using ‘Scan’ option which is present there. This will scan our network and give us all the IP addresses of the connected raspberry pi devices. Now, once we click ‘ok’, Codesys runtime would be set up in the pi devices. We should not forget to repeat this procedure for our other raspberry pi also.

Setting up Our Project- Sender

Now, after the package has been successfully installed, it is time to start creating our projects. We can reach our goal by controlling the two devices from one single project, but I have found creating two different projects much faster and simpler. First, we should create a new project. To do this, we should move to ‘File’, click on ‘New Project’ and choose a ‘Standard project’. We can give this project the name ‘Sender’ because; we will be sending our signal from this device. Next, we should select our package ‘Codesys Control For Raspberry Pi’ in the device, and for the choice of programming language, I have used ‘Function block Diagram (FBD)’, but of course you can use any of the available languages. It is now time to make few adjustments before we can actually use the switch on our piface board. The first and foremost thing is to setup our device and we should click on our ‘Device’ and put in the IP of one of the raspberry pi devices and press enter. After this is done, we should right-click on ‘SPI’, which we will find in the ‘Devices’ tab. Here, we should ‘Add Device’ and add ‘SPI Master’. Then, we should right-click on ‘SPI Master’ and ‘Add Device’ and add ‘Piface’. Then, we should add a network variable in our application, so that we can send this variable over the network to control the receiving end. To do this, we should right-click on ‘Application’, which is also in the Devices tab. We should then ‘Add Object’ and select ‘Network Variable List (Sender)’. Now, we should create the variable that we will want to send to the other project via the network. I have used the name ‘Status’ for this variable.

After we have set all this up, we are now ready to set up our POU. To do this, we should move to ‘PLC_PRG (PRG)’. From the ‘toolbox’, which is found on the right hand side of the screen, we should expand the ‘Function Blocks’ tab and select ‘TON’ block. We should provide a name to this block (optional) and setup a variable for this block, which is of type ‘TON’. We can now observe that the TON block has got two inputs and two outputs and we have got to assign variables in each of them. The ‘IN’ (input) should come from the switch on our piface board. To do this, we should type ‘piface.byIn.pinnumber’. I have used the pin number 0, so my input will be ‘piface.byIn.0’. Next comes the ‘PT’, which is the time that should pass before the output is set. We can set this to 50ms. To do this, we have to set the value of PT to‘t#50ms’. ‘Q’ should be assigned with the network variable that we had created. We will use the input assistant (can be found on the clicking the Q) to assign this. It should look like this now:


‘EN’ denotes the time elapsed since rising edge at input. We can check this once we run our project. Before running our project, the final step is to set the properties of the Network Variable List (NVL). To do this, we should right-click on NVL, go to properties and then navigate to ‘Link To File’, and check ‘Export before compile’ and create a file in our desired location. Now, we are all set to run our project. To run this, we should first compile our project by pressing F11. If there are no errors (and there should not be) we will go to ‘Online’ and click on ‘Login’. At last, we need to press F5 to start. Our project should look like this now when we press the switch on the piface.


We should check whether the .gvl file that was supposed to be generated when we linked our network variable, has been generated or not. If it has been generated, we are all good to start configuring the receiving end, but if it has not been generated, we should check what we did wrong and follow the steps from the beginning.

Setting Up Our Project- Receiver

Once we have created the ‘Sender’ project successfully, creating the ‘Receiver’ project is very simple. We would open a new Codesys environment (we will keep the other window opened as well) and like before we should create a standard project and I gave it the name ‘Receiver’. We should follow the similar steps as we did while creating the ‘Sender’ project. But, quite obviously we will not use the ‘Network Variable List (Sender)’ over here. Instead, we will use the ‘Network Variable List (Receiver) and will import our network variable from the file that we had generated. Like before, we will use the TON block, but this time, the input should be the ‘Status’ variable that we have imported. The time is kept the same and the output should be ‘piface.byOut.pinnumber’. I had used the LED numbered 1 and thus used the pin number 1 in my project. Now, similarly we would login and start our project. Before doing this, we must never forget to check whether our second raspberry pi is connected and is active in the project or not. If it is active, we should be able to login and run our project successfully. Our ‘Receiver’ project should look like this now:



Now, that both of our projects are running successfully, we should be able to control the LED of one raspberry pi with one of the switches of the other. Here is the image that I have clicked of my result:


I hope that this little example can be a platform for people to understand how Codesys works and this is also a small example how Codesys is used as a ‘Distributed Control System’ technology.

Have a nice day!

Aranya1991 has not written a bio yet…

27 Jun 2016, 9:23


February 6, 2018 09:34

nice to read from other Codesys Users! I work on Codesys V3 all the day at my job and at home I'm "playing" around sometimes with Codesys at the raspberry pi, too. Interesting may be, that you are able to create a NVL in the "Reciever"-Pi and import it in the "Sender-Pi" programm. Now you have the possibility of bidirectional communication!


January 22, 2018 11:58

Great project, thank you so much for posting!

0 Votes