
CODESYS V3.5 Programming Tutorial for Independent PLCs
Tutorial for CODESYS V3.5 development environment covering application structure, visualization, OPC UA server, and multi-vendor deployment.
Published on March 30, 2025
CODESYS V3.5 Programming Tutorial for Independent PLCs
This expanded tutorial presents a practical, standards-based approach to developing IEC 61131-3 applications in CODESYS V3.5 for independent PLCs and virtual controllers. It covers application structure, data modeling, function block usage, visualization and OPC UA server configuration, debugging and deployment strategies, and version/compatibility considerations for multi-vendor and embedded targets. The content synthesizes official CODESYS release documentation and hands-on engineering practices to help automation engineers design reliable, maintainable, and scalable control systems.
Key Concepts
Understanding the fundamentals of the CODESYS V3.5 environment and the governing standards (IEC 61131-3, IEC 62541 for OPC UA) is critical for portable, maintainable PLC projects. This section details the language and architectural primitives, data organization patterns, and the runtime concepts that underpin robust implementations.
Standards and Languages
CODESYS V3.5 implements the IEC 61131-3 languages: Structured Text (ST), Ladder Diagram (LD), Function Block Diagram (FBD), Instruction List (IL), and Sequential Function Chart (SFC). According to IEC 61131-3, programs must be organized into Tasks, Programs, and Resources; CODESYS provides Program Organization Units (POUs) such as the default PLC_PRG POU for the main cyclic logic (IEC 61131-3 compliance documented in product literature) [3][5].
Data Types and Structures
CODESYS supports both primitive types (BOOL, INT, REAL, etc.) and composite types via Data Unit Types (DUTs). Define reusable equipment blueprints using STRUCT ... END_STRUCT in a DUT. When you instantiate multiple instances (for example motor1, motor2), the IDE will propagate structural changes—such as adding a new field like fault—to all instances automatically, reducing manual edits and ensuring consistent memory layout [2].
Function Blocks and Parameter Passing
Function blocks (FBs) encapsulate state and methods. For scalable solutions, pass DUT instances by reference using INOUT parameters. This passes a reference rather than a copy, reducing memory usage and avoiding duplicate state for systems that manage many devices (e.g., a PressureManager FB handling multiple pressure sensors) [4]. Use typed FB instances for deterministic memory and execution behavior across targets.
Runtime Concepts
CODESYS distinguishes between the Development System (IDE) and the Runtime (PLC runtime on target hardware). The runtime executes tasks and POUs, handles cyclic/aperiodic scheduling, and manages hardware I/O and communication stacks (EtherCAT, PROFINET, OPC UA). CODESYS Virtual Control (Virtual PLC) allows multiple independent PLCs on commodity hardware for lab and virtualization use cases, reducing cost and easing CI/CD integration for control software [5].
Implementation Guide
Deploying a CODESYS V3.5 project successfully requires a repeatable project structure, disciplined data modeling, and a verified deployment pipeline. The steps below reflect best practices proven in field deployments and official CODESYS guidance.
Project Setup
- Start with a Standard Project and add a device that matches your runtime (simulator, Raspberry Pi, vendor controller, or Virtual Control SL).
- Create the main POU named PLC_PRG to host the main cyclic logic; map tasks and priorities according to process determinism requirements (IEC 61131-3 task configuration) [3][5].
- Enable IDE conveniences such as Tools > Options > SmartCoding to accelerate Structured Text variable management and maintain consistent naming conventions [1].
Data Modeling and Modularization
Use DUTs to define device blueprints. Example fields for a motor DUT might include:
- RUN : BOOL
- FAULT : BOOL
- SPEED_RPM : INT
- TEMP_C : REAL
- ALARM_CODE : UINT
Instantiate motors as motor1, motor2, etc. Call a single FB (e.g., MotorCtrl) with an INOUT reference to the motor instance to modularize I/O processing and control logic. According to practical examples and tutorials, this pattern prevents redundant code and simplifies change management when the DUT evolves [2][4].
Programming Patterns
- Centralize global signals (E-stop, system mode, network status) in a dedicated system DUT to avoid scattered global variables.
- Group alarms and diagnostics in structured arrays to support scalable HMI listing and OPC UA exposure.
- Implement error counters and watchdog timers at the FB or task level for robust fault detection; map critical faults to high-priority tasks.
Example Project Walkthrough (High-Level)
- Create Standard project > select target (e.g., CODESYS Control for Raspberry Pi SL or device-specific runtime) [5].
- Define DUTs for devices (motor, valve, sensor) and instantiate them in a global VAR block.
- Implement FBs with INOUT parameters to operate on DUT instances; instantiate FBs in a PROGRAM POU called PLC_PRG and call FBs cyclically.
- Configure tasks: assign PLC_PRG to the cyclic task with required cycle time (e.g., 10 ms for motion control, 100 ms for monitoring).
- Enable and configure OPC UA server and HMI visualization per project requirements (see OPC UA section below) [5].
- Build, login to the target (simulator or hardware), switch to RUN, and validate I/O, using forcing and online monitoring tools as needed (Ctrl+F7 to force values in ST/IL) [3].
Best Practices
These practices reflect decades of field experience and align with CODESYS recommendations and release notes for V3.5. They help achieve maintainable, high-performance automation solutions.
Design and Code Management
- Organize code into small, testable FBs and POUs. Favor composition over monolithic programs.
- Use structured DUTs for device data and instantiate standardized arrays for repetitive hardware elements, enabling bulk operations and easier HMI binding.
- Keep safety-critical logic in dedicated objects and consider platform safety certifications (e.g., AURIX TC39x support for SIL2) when requirements demand [6].
- Apply source control to the .project and associated files; use build automation for repeatable deployments to Virtual Control or hardware targets.
Memory and Performance
- Pass large structures using INOUT to avoid copies and reduce stack usage. This is especially important on constrained embedded targets (ARM/MIPS) [4].
- Profile task cycle times and reduce processing in high-frequency tasks. Move non-time-critical calculations to lower-priority tasks.
- Watch for dynamic allocations in IDE extensions or visualization scripts; prefer static arrays where determinism is required.
Testing and Debugging
- Use the CODESYS simulator or a device-specific simulator to validate logic before hardware tests. Login to the runtime and verify the module in RUN mode to confirm live behavior [3].
- Force variables using Ctrl+F7 or via the Watch window; observe power flow indicators in ST/IL to trace data flow and detect logic blocks carrying runtime power [3].
- Implement automated unit tests where possible; run tests in Virtual Control to accelerate iteration without hardware dependencies [5].
Deployment and Lifecycle
- Use CODESYS Automation Server for centralized app management and remote lifecycle control (start/stop/reset) via browser without needing the full development system on-site. SP21 expands browser-based control and Automation Server integration [5].
- Prefer Virtual Control SL for development, staging, and load testing. Virtual PLCs enable multiple independent runtime instances on commodity servers and improve orchestration in IT/OT integrated environments [5].
- Plan licensing: CODESYS uses application-based and host-bound licenses (CODESYS Store and license binding). Ensure license compatibility for target hardware and virtualization scenarios [5].
OPC UA and Visualization
Modern IIoT systems require standardized interfaces for data exchange. CODESYS integrates an OPC UA server and supports a browser-based HMI runtime to expose variables and enable remote operation.
OPC UA Server
Configure the built-in OPC UA server to expose variables and structured objects. CODESYS follows the OPC UA model (IEC 62541-compliant concepts) and supports cyclic and acyclic synchronization. SP21 introduced redundancy and improvements to OPC UA handling and namespace management suitable for multi-vendor architectures [5]. Use structured DUTs and arrays to create logical, discoverable nodes for clients (SCADA, MES).
Visualization and Automation Server
CODESYS provides runtime visualization that can be deployed on HMI panels or the Automation Server for browser-based access. The Automation Server allows remote application control—start/stop/reset—and browser-driven HMI operation without the Development System installed on the client machine, simplifying field service and remote diagnostics [5]. For advanced dynamic interfaces, integrate scripting (e.g., Python) to generate or manipulate visualization elements during deployment [5].
Product Versions, Compatibility and Specifications
Choose runtime and development versions based on target hardware constraints and required features. Below is a specification comparison for common runtime/development components and notable compatibility notes from the V3.5 SP19/SP21 release materials.
| Feature | Version / Notes | Compatibility & Key Points |
|---|---|---|
| Runtime (Control Linux SL) | V3.5 SP21 | Supports Raspberry Pi 5/4, ARM64 Windows, EtherCAT/PROFINET on x86; improved OPC UA and Automation Server features [5] |
| Virtual Control SL | V3.5 SP21 | Multiple independent virtual PLCs on standard hardware; suitable for IT orchestration and cost-effective lab setups [5] |
| Safety (AURIX TC39x) | V3.5 SP19+ | Pre-certified SIL2 support, hardware ECC protection and TriCore integration for safety applications [6] |
| Development System | V3.5.7.0+ (IDE) | Multi-vendor support (WAGO, Beijer, others), improved visualization scripting and expanded device repository; use SmartCoding and editor features for efficiency [5][10] |
When selecting a target, consult the vendor device page (e.g., WAGO CODESYS V3.5 support) and the CODESYS Store for runtime license models; some hardware binds licenses to host IDs and others use application-bound licensing [9][5].
Testing, Debugging and Field Commissioning
Field commissioning requires methodical testing and reliable debugging tools. CODESYS provides both IDE-level and runtime-level tools to validate behavior before and after installation.
Simulator and Online Login
Use the CODESYS simulator for early functional validation and the target-specific runtime simulator for realistic I/O emulation. Login to an actual runtime or to a virtual PLC to perform final integration tests, then switch to RUN mode to observe live operation. The IDE shows power flow in program editor windows to identify active logic segments [3].
Forcing and Monitoring
Force digital and analog variables from the IDE to simulate sensor and actuator states during commissioning. In Structured Text and IL editors, use Ctrl+F7 or the context menu to force variables; monitor power flow indicators and online variable watches to confirm signal propagation and FB behavior [3].
Fault Handling and Diagnostics
- Log important events and counters to a non-volatile diagnostics area or to an OPC UA history server depending on retention needs.
- Map diagnostic variables to visualization elements and remote-access dashboards via Automation Server for rapid troubleshooting.
- Implement health monitoring FBs that report CPU load, task jitter, and communication statuses to a system diagnostics structure.
Summary
CODESYS V3.5 provides a mature, standards-compliant environment for building portable IEC 61131-3 control applications across multiple hardware vendors and virtualization platforms. By following structured data modeling with DUTs, using INOUT references for large structures, and leveraging CODESYS runtime and Automation Server features (OPC UA, virtual PLCs), engineers can create scalable, maintainable automation systems. SP19 and SP21 releases extended platform support (AURIX, Raspberry Pi 5), OPC UA and virtualization capabilities—making CODESYS V3.5 a strong choice for modern IIoT and embedded control deployments [5][6].
For hands-on assistance with architecture, implementation, or commissioning of your CODESYS projects, contact our engineering team to discuss custom solutions and deployment strategies tailored to your hardware and industry standards.