I2C Scanner in industrial automation environment

How to know the I2C address you are working with
September 8, 2020 by
I2C Scanner in industrial automation environment
Boot & Work Corp. S.L., Marti Guillem Cura

Introduction

An I2C bus is a widely-used communication system for internal connection between different devices. It is based on the main device working as a master that controls the other devices known as slaves. To send data to a specific slave, it uses the address of its device provided by the manufacturer. In some cases, the provider does not give the address to work, which complicates the I2C communication. In this post, we will see how to know the addresses of the devices that we do not know at first.

Requirements

  • Any of our industrial PLC controllers

  • An I2C device

  • Wires (optional)

2C Scanner in industrial automation environment

How to connect it?

If you are using an Arduino automation MDuino Family PLC, no extra connection other than a PC connection is needed to upload the code, since an RTC (Real Time Connection) with I2C communication is included.

If not, you will have to connect to another device using the SCL and SDA communication pins. Also, it is better to connect the same Ground for a proper connection. Both devices must be properly powered, check the device information provided by the manufacturer.

In the picture on the left, you can see an example where an MDunio 21+ is connected to an Arduino Uno using the I2C pins. In the Arduino Uno you can use the A4 (SCL) and A5 (SDA), it might change in other Arduino devices.

Software interface

We will be programming using Arduino Ide, which is software based on the C language. Industrial Shields provides boards to program the industrial controllers. Basically is not needed to define the pins as inputs or outputs since everything is automatically set up using the boards. 

In order to install Industrial Shields boards, the link below explains everything:

First steps using Arduino based PLCs

After you have set all the board configurations, choose the Board and Model that are used and the Port where de device is connected to the PC in the Tools section of the Arduino Ide. We will use a simple sketch that scans the I2C bus for devices, showing the ones found in the Arduino Serial Monitor on the top right edge of the screen. The code is shown in the section below. Make sure that the Arduino library Wire is previously installed.  In our case, we will be detected, as we have said before, the RTC device shown as the 0x68 address. If another device is connected, it should appear in this scan.

 2C Scanner in industrial automation environment

Code

 

#include <Wire.h>
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknown error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
  delay(5000);
}

Do you want to know more about the I2C communication?

Check out our other posts andy try to implement a little electronic system using this protocol.

Share our posts about industrial arduino based devices in your social networks                


​Search in our Blog

I2C Scanner in industrial automation environment
Boot & Work Corp. S.L., Marti Guillem Cura September 8, 2020

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>