The Power of I2C Bus: Applications of Digital Sensors in Microcontrollers and PLCs

The Power of I2C Bus: Applications of Digital Sensors in Microcontrollers and PLCs

Dear friends, I am Dayi Zong, and today I bring you a practical technology article about the I2C bus. I2C is a simple and widely used serial communication protocol, which can be seen in both microcontroller and PLC fields. So what makes it unique? Let’s start with practical application cases.

I2C Bus Introduction

I2C stands for Inter-Integrated Circuit, which is called “Internal Integrated Circuit” bus in Chinese. As the name suggests, its original purpose was to allow chips on the board to transmit data to each other. It enables half-duplex communication between a host and multiple slaves using only two wires (SDA/SCL).

I2C is like a two-way street, where SDA is the bidirectional data lane and SCL is the clock lane. Any device can act as a sender or a receiver. The communication follows a strict “send-acknowledge” pattern, ensuring the reliability of data transmission.

Connecting Digital Sensors

The advantage of the I2C protocol lies in its convenience for communication with various peripherals. For example, common digital sensors such as temperature and humidity, barometric pressure, and electronic compass sensors usually come with dedicated I2C drivers provided by manufacturers.

Taking the SHT30 temperature and humidity sensor as an example, you only need to correctly connect its VCC, GND, SDA, and SCL pins to the I2C interface of the microcontroller or PLC to read accurate environmental parameters. The wiring is simple, and the sensor device address is fixed, requiring minimal configuration.

For beginners, it is much simpler compared to analog input. More importantly, both microcontrollers and PLCs support I2C, allowing you to write code once and use it universally.

Microcontroller I2C Programming

Let’s take the 51 microcontroller as an example to see how to read data from the SHT30 using I2C. The hardware resources consist of two IO pins from port P0, plus a 10K pull-up resistor.

#include <reg51.h>
#define SDA P0^0  // Data line
#define SCL P0^1  // Clock line

// Start I2C bus
void Start_I2C() {
  SDA = 1; // Send start signal
  SCL = 1;
  SDA = 0;
  SCL = 0;
}

// End communication
void Stop_I2C() {
  SDA = 0;
  SCL = 1;
  SDA = 1;
}

// Host sends a byte
void Send_Byte(unsigned char dat) {
  unsigned char i;
  for (i = 0; i < 8; i++) {
    SDA = (dat & 0x80) >> 7; // Transmit from high bit
    SCL = 1; // Give clock pulse to slave
    SCL = 0;
    dat <<= 1; // Shift pointer left by 1 bit
  }
}

unsigned int Read_Temp() {
  unsigned int temp;
  Start_I2C(); // Start bus
  Send_Byte(0x88); // Send device address + write data
  Send_Byte(0x03); // Send register address
  ...  // Execute specific read/write operation
  Stop_I2C(); // End communication
  return temp;
}

void main() {
  unsigned int humidity;
  humidity = Read_Temp();
  // Data processing
}

It may seem a bit complicated, but once you understand the basic communication principles of I2C, programming becomes quite easy to master. The code above is just a minimal example; in practical projects, it needs to be improved based on specific situations, such as adding a retransmission mechanism, error handling, etc.

PLC I2C Programming

For PLCs with modular programming capabilities, calling the I2C slave function block is also quite simple. Taking the Siemens S7-300 PLC as an example:

  1. Hardware wiring: Connect the I2C slave module IM365 to the PLC.
  2. Write the program: Insert the slave module communication instruction MB_MASTER in the main program OB1.
  3. Specify baud rate, slave address, read/write operations, and data area.
  4. Create a data block DB to store device parameters and read data.

To read the SHT30 temperature and humidity sensor, you only need to select the appropriate read command in the MB_MASTER module, set the target onboard slave address, and specify the register address for reading and writing.

If using ladder diagram programming, calling the function block MB_MASTER is also very intuitive. Although there are many steps, as long as you understand the working principle of I2C, writing the communication program is not too difficult.

Common Issues and Debugging Tips

Finally, I would like to share some common issues when applying I2C:

  1. Hardware wiring may have reverse connections or loose connections. You can use a multimeter to measure the pull-up resistor.
  2. Failure to comply with the start and stop conditions of I2C. Please check the start and stop timing.
  3. Data read/write errors. Verify if the address is correct and if the register number is correct.
  4. Too high a rate causing communication failure. It is recommended to use a baud rate below 100KHz.
  5. Environments with significant interference may cause data errors. You can increase data checks or increase the pull-up resistor value.

During programming, encountering bugs is inevitable. Maintain patience and look for reasons from both hardware wiring and software programs to make adjustments. I recommend that everyone frequently consult device manuals and application notes, and take notes in practical scenarios to accumulate experience.

Well, if you have any questions, feel free to consult “Dayi Zong”. I2C is just a small mark in communication technology, and I hope this opportunity can inspire you to explore the broader applications of microcontrollers and PLCs. The learning process may not always be smooth, but it will ultimately elevate your skills and value! Keep up the effort!

Leave a Comment