Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteris

I2C Scanner in industrial automation environment

How to know the I2C address you are working with

Introduction

An I2C bus is a widely used communication system for internal connection between different devices. It is based on a 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 a RTC (Real Time Connection) with I2C communication is included.

If not, you will have to connect 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 a software based on the C language. Industrial Shields provides boards to program the industrial controllers. Basically is not needed to define the pins and if they are 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 configuration, choose the Board and Model that are using 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 detecting, 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

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.

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);
}

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



Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send