How-To CODESYS V3.5
Description
The CODESYS programming tool from CODESYS Group is an integrated development environment (IDE) to program and develop PLC applications for and on a wide variety of hardware like computers and embedded devices, like the Raspberry Pi computer.
Some time ago the CODESYS Group made CODESYS available for the Raspberry Pi and since the Pi-Tron CM4 is based on the powerful Compute Module 4, it is possible to install and use CODESYS on the Pi-Tron CM4 and utilize its peripherals from within CODESYS.
This How-To tries to outline the steps needed to get CODESYS up and running on the Pi-Tron CM4.
Requirements
- 24 volts power supply with 2 pin power plug for the Pi-Tron CM4.
- Development computer with network access to the Pi-Tron CM4 or alternatively connect an HDMI monitor, mouse and keyboard.
- To login into the Pi-Tron CM4 via network, it is requires that the SSH server is running.
- The login details are by default: username: pi and password: raspberry
- Note: The latest version of the Raspberry Pi OS requires the user creating the initial SD card or writing the system to eMMC memory to choose a username and password or the information has to be entered during first boot of the system. The above mentioned defaults may not apply to your situation.
- For more information on this topic, have a look at this blog post: An update to Raspberry Pi OS Bullseye
- An active Internet connection to download and install additional software on the development computer.
- CODESYS Development System V3 must be installed on the development computer, available for download on the CODESYS Store.
- The CODESYS Control for Raspberry Pi MC SL package, available from the CODESYS Store.
- The CODESYS Edge Gateway for Linux is also needed, also available from the CODESYS Store.
- The CODESYS Control SL Extension package is currently not available directly from the CODESYS Store, it can only be installed through the CODESYS Installer which requires an active Internet connection.
- An example program for the DIOs can be downloaded as a Zip file from here or a project archive.
How-To
This how-to is divided into 3 parts, first the setup of the development pc, then the setup of the Pi-Tron CM4 and as a conclusion a CODESYS example on how to control the DIOs.
Setup Development Computer
- A Windows PC or computer is required to run CODESYS, either directly or in a virtual computer like VMWare Workstation or VirtualBox.
- CODESYS V3.5 needs to be installed using the CODESYS setup program and administrative rights. The current version of CODESYS V3.5 can be downloaded from the CODESYS Store.
- Since CODESYS V3.5 SP18, CODESYS comes with the CODESYS Installer, which allows the installation of needed packages right from within a central installer application for CODESYS.
- Open the CODESYS Installer from within CODESYS, first entry in the Tools Menu.
- Click on the Browse tab and look for the "CODESYS Control for Raspberry Pi" package.
- Select the package and click on "Install".
- The CODESYS Installer might say that some dependency packages need to be installed as well, click OK to proceed and have everything installed.
- Start CODESYS as a normal user and it should now be possible to create a PLC project with the Raspberry Pi control.
Setup Pi-Tron CM4
Introduction
Pi-Tron CM4 devices are delivered without any OS pre-installed on them. If you have not setup the Pi-Tron CM4 yet, have a look at the Quickstart Guide on how to install the Raspberry Pi OS and take a look below at the Related Documentation section at the end of this How-To.
CODESYS Runtime installation
To get the Raspberry Pi CODESYS runtime installed on the Pi-Tron CM4:
- Power on the Pi-Tron CM4 and launch CODESYS V3.5 on the development computer.
- In CODESYS open the Tools menu and click on the menu entry Update Raspberry Pi
-
Usually on the left side a new window appears, which allows the user to perform maintenance on any connected Raspberry Pi.
- (1) Enter the password for the pi user or the user specified during the installation of the Raspberry Pi OS
- (2) Enter or scan for the IP address of the Pi-Tron CM4
- (3) Choose the desired runtime version, usually the newest available. In this example it is 4.6.0.0
-
(4) Click on the Install button right under the Version list, not the one further down under Additional Packages
-
After the runtime installation, usually a small window pops up and asks which runtime version to use, as there are 2 different versions.
- Standard version (Single Core) CODESYS and all its tasks run on one single CPU core, reducing the overall system load and program complexity.
- Multicore version (Default) CODESYS tasks can be distributed freely across all 4 CPU cores of the Compute Module 4. Keep in mind that some kind of task/thread synchronization is needed as programs now run in parallel to each other. Passing data from one task/thread to another can present additional challenges to the programmer and if not implemented correctly strange and unpredictable behavior can occur.
-
Choose the desired version, click Apply if required and click on OK.
-
After the basic runtime configuration, CODESYS might also ask to install or update the Edge Gateway package, it is recommended to click Yes.
Raspberry Pi OS modifications
To make the most of the Pi-Tron CM4's hardware and interfaces when using CODESYS, it is recommended to enable and activate certain overlays in the config.txt file found in the boot partition.
Recommended Raspberry Pi OS overlays:
-
CAN-Bus and SPI bus, see How-To CAN-Bus
-
Note: The CAN-Bus interface requires a custom start program or script to bring up and configure the interface before the CODESYS runtime starts! A simple shell script could look like the code below and remember to disable CODESYS auto start: sudo systemctl disable codesyscontrol
#!/bin/bash #------------------------------------------------------------------------ # Set CAN Bus device can0 to TX Buffer 1000 with 250k bitrate #------------------------------------------------------------------------ sudo ip link set can0 down sudo ip link set can0 type can bitrate 250000 sudo ifconfig can0 txqueuelen 1000 sudo ip link set can0 up sleep 1 #------------------------------------------------------------------------ # Now start CODESYS #------------------------------------------------------------------------ sudo service codesyscontrol start
Place the script's call at the end of the /etc/rc.local file for example or use cron.
-
-
RTC and I²C bus, see How-To RTC
-
USB Controller, see How-To USB
-
Serial ports should be active automatically, if the USB controller is enabled. For more information see How-To RS232 and RS485
-
Optionally: The BL Pi-Tron CM4 offers the option to attach a Raspberry Pi B model compatible 40 pin header on the back side of the PCB. See How-To GPIO Header
-
Optionally: Enable the serial Debug Console in case there are problems with the Ethernet or WLAN access at some point in the future, see How-To Debug Console
-
Optionally: If the Raspberry Pi OS Desktop version is used, it can be helpful to turn off the screensaver to keep any attached display from going blank or dark every few minutes. See How-To Screensaver
CODESYS Configuration
To access certain hardware or peripherals of the Pi-Tron CM4 from CODESYS, some changes to the CODESYS configuration are needed as well. Changing settings in the OS is not enough.
Serial Ports
When using CODESYS on the Pi-Tron CM4, the serial port names must be in sequence. By default once the USB Controller is enabled, the 2 Pi-Tron CM4 serial ports (RS232 and RS485) can be accessed through the following devices.
- RS232 → ttyUSB0
- RS485 → ttyUSB1
This requires a change in the CODESYSControl_User.cfg file found in the /etc/ folder. To edit this file, root or super user rights are needed.
sudo nano /etc/CODESYSControl_User.cfg
Change the line ;Linux.Devicefile=/dev/ttyS → Linux.Devicefile=/dev/ttyUSB
and make sure the leading semicolon is removed.
CODESYSControl_User.cfg before (left side) and after the change (right side)
Using the sed program, the CODESYS config can be changed like this for example:
sudo sed -i 's+;Linux.Devicefile=/dev/ttyS+Linux.Devicefile=/dev/ttyUSB+g' /etc/CODESYSControl_User.cfg
This results in the following CODESYS COM port assignments:
- COM 1 → ttyUSB0 → RS232
- COM 2 → ttyUSB1 → RS485
CAN-Bus
At the time of writing and CODESYS version 3.5 SP18 Patch 4, the CAN-Bus cannot be initialized by CODESYS as the corresponding Linux script is in the wrong place.
Move the rts_set_baud.sh script to the correct location:
sudo cp /opt/codesys/scripts/rts_set_baud.sh /var/opt/codesys/
Afterwards it might be a good idea to rename the original script:
sudo mv /opt/codesys/scripts/rts_set_baud.sh /opt/codesys/scripts/rts_set_baud.bac
User Management
Note: This change is optional and will reduce the network safety of the device, but can make the development process easier!
Since CODESYS V3.5 SP17 the default setting for the CODESYS User Management system on the Raspberry Pi has been changed from optional to enforced. This means when connecting to the PLC for the first time, the user has to set a system user name (administrator user) and a password.
This username and password combination has to be entered at the start of every CODESYS session when connecting to the PLC for the first time in that session, e.g. after a restart of the development computer or CODESYS was closed and then reopened. This mechanism may become a hinderance during active development of the PLC application. To make the initial development process easier, this enforcement of the User Management system, can be turned off or switched to optional again. A simple change in the CODESYSControl_User.cfg is sufficient.
If a username and password is already set, check the connection options in CODESYS if the User Management system can be reset from there, otherwise in addition to the change shown below, all files in the /var/opt/codesys/ folder starting with a "." (dot) and contain the words User and/or Group and/or Mgr in the filename have to be deleted. Do not delete the file .SoftContainer_CmEmbedded.wbb or the folders .cmact_licenses and .pki, this can render CODESYS unusable. After these changes a reboot is recommended.
sudo nano /etc/CODESYSControl_User.cfg
In the section CmpUserMgr add the following line:
SECURITY.UserMgmtEnforce=NO
CODESYSControl_User.cfg before (left side) and after the change (right side)
CODESYS DIO Example
To test out CODESYS and the Pi-Tron CM4 functionality, an example program is available for download as a Zip file from here or a project archive which demonstrates the use of the DIOs by controlling the MCP23017 I²C GPIO expander from within CODESYS. The library was originally made by CODESYS and can be found on the CODESYS Forum. The library used in this demo has one modification compared to the original, it allows the user to change the MCP23017 I²C GPIO expander configuration during normal operation, by changing the configuration and then setting the variable APPLY to True. Also the library name was changed from I2C_MCP23017.library to I2C_MCP23017v2.library and the version was changed to 1.0.0.0.
Note: The MCP23017 on the Pi-Tron CM4 has the I²C address 0x20 or 16#20
When using the Zip file:
- Download the file and unpack it to a know location.
- Open CODESYS and go to the Tools Menu and select Library Repository...
-
In the Library Repository click on Install and select the I2C_MCP23017v2.library file which came with the Zip file and click on Open to install the library.
- The original library can be found on the CODESYS Forum.
-
Next, open the Tools Menu again and select Device Repository...
- In the Device Repository and click on Install and select the MCP23017v2.devdesc.xml file which also came with the Zip file and install it as well.
- Close and open CODESYS to make sure the device and library database is reloaded.
- Open the demo project Pi-Tron-CM4-DIO-Demo.project
- Compile the project and transfer it to the Pi-Tron CM4.
- If any errors occur, they have to be resolved before the demo application can be loaded on the Pi-Tron CM4.
When using the project archive:
- Download the project archive and open CODESYS.
- Open the project archive via the File menu through either Open Project... or
Project Archive → Extract Archive. - Chose a location for the project file and when asked to overwrite any existing libraries or devices, click on No.
- After the extraction, the project will be loaded automatically and should compile without any errors.
- Transfer the demo project to the PLC and run it.
After logging in:
- Open the DIO_PRG POU and change any variables as need.
- All DIOs of the Pi-Tron CM4 have been configured as outputs during the initialization IF clause at the beginning of the POU.
- Setting xAllDioOn to True will turn on all digital outputs
- Setting xAllDioOff to True will turn off all digital outputs
- Setting xToggleDigOutputs to True will activate a Timer and a sequencer which turns every output on and off in sequence from DIO1 to DIO4 and then starts again from the beginning until xToggleDigOutputs is set to False.
Some screenshots from the CODESYS code in the development windows and when connected to the PLC side by side.
- Initialization of the GPIOs
- Turn all DIOs on
- Letting the DIOs toggle on and off
Restrictions
- On newer Raspberry Pi OS Bullseye releases after Feb. 2023, the CODESYS runtime might not start correctly or terminates itself after a few seconds. If this happens, either go back to Raspberry Pi OS from Feb. 2023 or try and add the line 'arm_64bit=0' to /boot/config.txt.
- There is some information available in the CODESYS forums here.
Related documentation
- Covering all aspects of the Raspberry Pi, the official documentation: https://www.raspberrypi.com/documentation
- Login with the pre-set login credentials, set in the Raspberry Pi Imager or entered during first boot. For more information have a look at this blog post
- CODESYS website: https://www.codesys.com
- CODESYS store: https://store.codesys.com
- CODESYS Development System V3: https://store.codesys.com/en/codesys.html
- CODESYS Control for Raspberry Pi MC package: https://store.codesys.com/en/codesys-control-for-raspberry-pi-mc-sl.html
- CODESYS Edge Gateway for Linux: https://store.codesys.com/en/codesys-edge-gateway-for-linux.html
- CODESYS FAQ: https://faq.codesys.com/display/CDSFAQ/CODESYS+FAQ
- CODESYS Forum: https://forge.codesys.com
- CODESYS Forum, list of available drivers: https://forge.codesys.com/forge/talk/Runtime/thread/8981ce928a/?limit=25#e15f
- CODESYS Examples: https://forge.codesys.com/prj/codesys-example
- CODESYS Raspberry Pi: List of available example applications: https://forge.codesys.com/forge/talk/Runtime/thread/04361f89ec