CONTROLLINO Library

repository·master·Indexed 41 days ago

https://github.com/controllino-plc/controllino_library

An open-source, Arduino-compatible PLC hardware platform library providing hardware definitions (BSP) and software abstractions for RTC, RS485, and pin aliases. Includes documentation for flashing customized bootloaders for MINI, MEGA, and MAXI variants using ISP programmers and Atmel Studio, as well as examples for I2C synchronization between devices.

Tokens
11.4K
Snippets
21
Records
45
Agent score
48%

What's inside CONTROLLINO

  1. Hardware-specific Pin Mapping for Modbus Synchronization

    master

    When synchronizing Modbus data with physical Controllino pins, the pin assignments depend on the hardware model being used. Ensure both Server and Client use the same definitions to maintain synchronization.

    Controllino MAXI

    • Digital Coils/Inputs: Uses NUMBER_INPUTS_COILS (5).
    • Analog Registers: Uses NUMBER_IN_HOLD_REGISTERS (5).
    • Analog Start Pin: CONTROLLINO_A5.

    Controllino MEGA

    • Digital Coils/Inputs: Uses NUMBER_INPUTS_COILS (11).
    • Analog Registers: Uses NUMBER_IN_HOLD_REGISTERS (5).
    • Analog Start Pin: CONTROLLINO_A11.

    Pin Initialization Pattern

    To map Modbus data to pins, iterate through the configured counts and use pinMode or analogWrite to initialize the hardware state.

    // Example for MAXI digital pin setup
    for (int i = 0; i < NUMBER_INPUTS_COILS; i++) {
      pinMode(CONTROLLINO_A0 + i, INPUT);
      pinMode(CONTROLLINO_R0 + i, OUTPUT);
      digitalWrite(CONTROLLINO_R0 + i, LOW);
    }
    
    // Example for Analog pin setup
    for (int i = 0; i < NUMBER_IN_HOLD_REGISTERS; i++) {
      pinMode(FIRST_ANALOG_INPUT_PIN + i, INPUT);
      pinMode(CONTROLLINO_D0 + i, OUTPUT);
      analogWrite(CONTROLLINO_D0 + i, 0);
    }
  2. How Modbus TCP synchronization works between Controllino devices

    master

    In a Modbus TCP setup, two Controllino devices act as a Server and a Client to synchronize physical I/O points over an Ethernet network.

    The Server (Data Holder)

    • Manages physical inputs and outputs.
    • Listens on a specific IP address and port (typically 502) for incoming requests.
    • Provides the current state of its inputs and executes commands to change its outputs.

    The Client (Controller)

    • Connects to the Server's IP address.
    • Retrieves the current state of the Server's inputs.
    • Sends commands to the Server to update the Server's outputs based on its own control logic.

    Synchronization Logic

    • Digital I/O: The Client reads Discrete Inputs from the Server (to monitor sensors/switches) and writes to the Server's Coils (to control relays/LEDs).
    • Analog I/O: The Client reads Input Registers from the Server (to monitor sensors like temperature) and writes to the Server's Holding Registers (to control analog outputs like motor speeds).
  3. Understand analog input voltage limits

    master
    While CONTROLLINO is often used with 24V systems, the analog input A0 (connected to the D0 pin) will not return a full 1023 value at exactly 24V. The maximum value of 1023 is reached when the power supply is closer to 30V. If you are seeing values like 806 at 24V, this is expected behavior.
  4. Use CONTROLLINO pin aliases

    master

    While you can use standard Arduino aliases (like A0 or 2), the CONTROLLINO library provides hardware-specific aliases that match the screw terminal labels on the device. These aliases follow the pattern CONTROLLINO_<DESCRIPTION>.

    Example (CONTROLLINO MINI):

    • Digital Pins: CONTROLLINO_D0 through CONTROLLINO_D7
    • Analog Pins: CONTROLLINO_A0 through CONTROLLINO_A5
    • Inputs: CONTROLLINO_IN0, CONTROLLINO_IN1

    Always include the library at the top of your sketch to use these aliases:

    #include <Controllino.h>
    #include <Controllino.h>
    
    void setup() {
      pinMode(CONTROLLINO_D0, OUTPUT);
    }
  5. How Controllino integrates with ArduinoModbus

    master

    The Controllino Board Support Package (BSP) for MAXI and MEGA includes pre-defined pin mappings in pins_arduino.h that allow for seamless integration with the ArduinoRS485 library.

    Because the BSP defines the following constants:

    • RS485_SERIAL_PORT (Serial3)
    • RS485_DEFAULT_TX_PIN (14)
    • CUSTOM_RS485_DEFAULT_DE_PIN (75)
    • CUSTOM_RS485_DEFAULT_RE_PIN (76)

    You do not need to manually instantiate an RS485Class or pass it to Modbus. You can use the default instances provided by the ArduinoModbus library directly, such as ModbusRTUServer and ModbusRTUClient.

  6. Read analog values on CONTROLLINO MINI A6 and A7 pins

    master
    On the CONTROLLINO MINI, pins A6 and A7 are analog-only. They cannot be used with digitalRead() to get a digital status. To detect a high/low state on these pins, you must read their analog value and compare it against a specific voltage threshold in your code.
  7. Understand modifications to the CONTROLLINO MAXI bootloader

    master
    The CONTROLLINO MAXI uses a modified version of the original Arduino Mega2560 bootloader. The only functional change made by CONTROLLINO is that the LED blinking behavior has been disabled. This ensures the bootloader process does not interfere with specific hardware signaling or user expectations regarding LED status during startup.
  8. Implement a Modbus RTU Master with Controllino

    master

    To implement a Modbus RTU Master using a Controllino Mega, you must use the ArduinoModbus and ArduinoRS485 libraries. The master device acts as a client that reads from and writes to a slave device's registers and coils.

    Key synchronization logic in this pattern:

    1. Digital Synchronization: Read discrete inputs from the slave and write them to the master's digital outputs. Simultaneously, read the master's digital inputs and write them to the slave's coils.
    2. Analog Synchronization: Read input registers from the slave and write them to the master's analog outputs. Simultaneously, read the master's analog inputs and write them to the slave's holding registers.

    Required libraries:

    • Controllino.h
    • ArduinoRS485.h
    • ArduinoModbus.h
    #include <Controllino.h>  
    #include <ArduinoRS485.h>  
    #include <ArduinoModbus.h>  
    
    #define CONTROLLINO_MEGA_SLAVE  1  
    #define NUMBER_INPUTS_COILS  11    
    #define NUMBER_IN_HOLD_REGISTERS 5 
    
    void setup() {
      Serial.begin(9600);
      // Initialize Modbus RTU Client
      if (!ModbusRTUClient.begin(9600)) {
        while (1); // Halt on error
      }
    }
    
    void loop() {
      // Example: Reading discrete inputs and writing coils
      for (int i = 0; i < NUMBER_INPUTS_COILS; i++) {
        int val = ModbusRTUClient.discreteInputRead(CONTROLLINO_MEGA_SLAVE, i);
        if (val >= 0) {
          digitalWrite(CONTROLLINO_R0 + i, val);
        }
        ModbusRTUClient.coilWrite(CONTROLLINO_MEGA_SLAVE, i, digitalRead(CONTROLLINO_A0 + i));
      }
    }
  9. Upgrade to CONTROLLINO BSP version 3.1.0

    master

    Version 3.1.0 introduces a new MINI bootloader and allows burning the bootloader directly from the Arduino IDE using an Arduino UNO.

    Troubleshooting Compilation Errors: If you encounter compilation errors after upgrading, you may have obsolete versions in your local Arduino packages folder. Navigate to the following directory and remove all previous versions (e.g., 2.0.0, 3.0.0, etc.): c:\Users\UserName\AppData\Local\Arduino15\packages\CONTROLLINO_Boards\hardware\avr\

  10. Burn the CONTROLLINO MAXI Bootloader

    master

    To install the bootloader on a CONTROLLINO MAXI, use an ISP programmer (such as an Atmel-ICE) connected to the X1 pin header. Follow these steps:

    1. Hardware Connection: Connect the ISP programmer to the CONTROLLINO X1 header using the following pin mapping:
      • TCK $\rightarrow$ SCK
      • GND $\rightarrow$ GND
      • TDO $\rightarrow$ MISO
      • VTref $\rightarrow$ VCC (5V!)
      • nSRST $\rightarrow$ Reset
      • TDI $\rightarrow$ MOSI
    2. PC Connection: Connect the ISP programmer to your computer.
    3. Power: Provide power to the CONTROLLINO via USB or an external power supply.
    4. Software Setup: Open Atmel Studio and navigate to Tools $\rightarrow$ Device programming.
    5. Device Configuration: Set the Device to ATmega2560 and the Interface to ISP. Verify the signature and voltage.
    6. Flash Bootloader: Navigate to Tools $\rightarrow$ Device programming $\rightarrow$ Memories. In the Flash section, select the path to the Bootloader_CONTROLLINO_MAXI.hex file (downloaded from GitHub) and click Program.

    Note: The current bootloader version (1.0) is a modified ATmega2560 bootloader that removes the requirement to use LEDONBOARD while uploading new sketches.

    ### Pin Mapping for X1 Header
    - TCK -> SCK
    - GND -> GND
    - TDO -> MISO
    - VTref -> VCC (5V!)
    - nSRST -> Reset
    - TDI -> MOSI
  11. Synchronize two Controllino devices via I2C

    master

    This project demonstrates how to achieve distributed control by synchronizing two Controllino devices over an I2C bus. One device is configured as the Master and the other as the Slave.

    Communication Workflow

    1. Master Controllino:

      • Reads its own analog and digital inputs.
      • Sends these values to the Slave via I2C.
      • Requests and receives updated output states from the Slave.
      • Updates its own local outputs (PWM and Digital) based on the Slave's data.
    2. Slave Controllino:

      • Receives input values from the Master.
      • Reads its own analog and digital inputs.
      • Sends its input values back to the Master when requested.
      • Updates its own outputs based on the data received from the Master.

    This pattern is useful for building coordinated distributed control systems where multiple controllers must act in harmony.