Skip to Content

I²C (Inter-Integrated Circuit) is a widely-used, synchronous communication protocol designed for short-distance communication between integrated circuits and micropocessors/controllers. It uses a master-slave architecture with a two-wire interface: SCL (serial clock line) and SDA (serial data line). 

In the realm of Programmable Logic Controllers (PLCs), I²C is particularly valuable for extending the functionality of the system without the need for complex wiring. By using I²C, PLCs can easily connect to a variety of sensors, displays, memory modules, and other peripheral devices, facilitating real-time data acquisition, monitoring, and control.

Hardware

Arduino based PLCs feature one external I2C port which works at the voltage levels of the internal microprocessor: 5V. In order to use I2C communication with an external device the SDA and SCL lines, as well as a common ground, need to be connected:

Do not connect voltages higher than 5V to the I2C pins, as this could damage the  PLC.


Depending on the PLC, the steps needed to configure the  are different. Follow the procedure according to your PLC model:

Remember to properly power the PLC with a 12-24V power supply.


Software

The I2C can be used with the library "Wire". Check the following example which implements an I2C scanner that searches the bus for any connected devices. When a device is detected, it will be reported to the serial monitor. Note that if you connect the I2C pins (SDA and SCL) to an other device, it should appear in the scan.

#include "Wire.h"

void setup() {
  Serial.begin(115200);
  Wire.begin();
}

void loop() {
  byte error, address;
  int nDevices = 0;

  delay(5000);

  Serial.println("Scanning for I2C devices ...");
  for(address = 0x01; address < 0x7f; address++){
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0){
      Serial.printf("I2C device found at address 0x%02X\n", address);
      nDevices++;
    } else if(error != 2){
      Serial.printf("Error %d at address 0x%02X\n", error, address);
    }
  }
  if (nDevices == 0){
    Serial.println("No I2C devices found");
  }
}
Rating
0 0

There are no comments for now.

to be the first to leave a comment.