54. I2C Scanner

In this task, we are implementing the I2C scanner using a microcontroller. It detects all connected I2C slave devices and displays their addresses over UART.

I2C Communication Basics

  • Two-Wire Interface: Uses SDA (Serial Data Line) and SCL (Serial Clock Line).
  • Master-Slave Architecture: The master initiates communication, while slaves respond when addressed.
  • 7-Bit Addressing: Each slave device has a unique 7-bit address (0x08 to 0x77).

Note: When interfacing I²C devices operating at different voltages (e.g., 5 V ↔ 3.3 V), always use a voltage level shifter to ensure safe logic levels and reliable communication.

General Approach for I2C Scanning

  1. Initialize I2C and UART peripherals.
  2. Scan all possible I2C addresses (1 to 127).
  3. Check device presence using an acknowledgment signal.
  4. Report detected devices via UART.

Common Pitfalls & Tips

  • Pull-up resistors: Ensure 4.7kΩ resistors are present on SDA & SCL lines.
  • Clock speed: Ensure the I2C clock speed matches the slave device's specification.

So, by connecting and configuring the slave devices and the I2C communication, we can implement the task.

Below are the solutions to the given task using different microcontrollers

  1. STM32
  2. ESP32
  3. Arduino UNO

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!