I2C

This article introduces I²C communication on the Edge101 board and explains how to use its I²C ports in master or slave mode. It also demonstrates scanning I²C devices with an ESP32 and handling communication.

I2C

The Edge101 board has two I2C control ports that handle communication on the two I2C buses. Each controller can be set as a master or slave. The board expansion interface uses GPIO18 as I2C SDA and GPIO23 as I2C SCL, and it uses a library compatible with the Arduino Wire library.

Example: Scanning Devices on the I2C Bus

The following code scans for devices on the I2C bus every 5 seconds and prints the slave device addresses found to the serial monitor.

Hardware Preparation:

Sample Code:

#include "Wire.h"  // Include I2C communication library

void setup() {
  Serial.begin(115200);  // Initialize serial communication (baud rate 115200)
  Wire.begin();          // Initialize I2C bus (default uses GPIO18-SDA/GPIO23-SCL)
}

void loop() {
  byte error, address;   // error: communication status code, address: scanned address
  int nDevices = 0;      // Device found counter

  delay(5000);           // Scan the bus every 5 seconds

  Serial.println("Scanning for I2C devices ...");
  
  // Scan the standard I2C address range (0x01~0x7F)
  for(address = 0x01; address < 0x7f; address++) {
    Wire.beginTransmission(address);  // Try to communicate with the target address
    error = Wire.endTransmission();   // Get communication status
    
    if (error == 0) {                 // Status code 0 means device responded
      Serial.printf("I2C device found at address 0x%02X\n", address);
      nDevices++;
    } 
    else if(error != 2) {             // Status code 2 is normal, indicating no device is connected, other errors need to be reported
      Serial.printf("Error %d at address 0x%02X\n", address, error);
    }
  }

  if (nDevices == 0) {                // No devices found
    Serial.println("No I2C devices detected");
  }
}

Result:

The serial monitor will print the scanned devices. The address 0x51 corresponds to the I2C address of the RTC chip on the board.

Was this article helpful?

TOP