55. LED Toggling Using I2C

In this task, we are implementing that when a push-button connected to the Master microcontroller is pressed, the Slave microcontroller toggles an LED connected to it using I2C communication.

For this, we have to establish I2C communication between the master and the slave.

Understanding I²C Protocol

I2C-Communication

I²C (Inter-Integrated Circuit) is a serial communication protocol that uses only two lines

  • SDA (Serial Data) – Transfers data between devices.
  • SCL (Serial Clock) – Provides clock signal (controlled by the Master).

Key characteristics:

  • Master-Slave Architecture: The Master initiates and controls communication; Slaves respond.
  • Open-Drain Configuration: Devices can only pull the line LOW, so pull-up resistors are mandatory on both SDA and SCL.
  • Addressing:
    • Each Slave has a unique 7-bit address (0x08 to 0x77 for general usage).
    • In this task, we will use a 0x08 slave address.
  • Speed: Standard mode is 100 kHz, which is suitable for this task as LED toggling is not time-critical.

Important Design Considerations

  1. Pull-Up Resistors:
    • Required because of the open-drain nature of I²C lines.
    • Typical value: 4.7 kΩ for 100 kHz communication (can range from 4.7 kΩ to 10 kΩ depending on bus capacitance and speed).
  2. Pull-Up Voltage:
    • Use 5V if both microcontrollers operate at 5V.
    • Use 3.3V if one or both microcontrollers operate at 3.3V.
  3. Common Ground: Both boards must share a common GND for proper communication.
  4. Debouncing: Push buttons exhibit noise; implement software debounce logic to avoid multiple triggers.

Hardware Setup

  • Master Connection:
    • Connect the push button to any Digital GPIO pin (configured with internal or external pull-up).
  • Slave Connection
    • Connect the LED to any Digital GPIO pin through a current-limiting resistor.
  • I²C Connections:
    • Master SDA → Slave SDA
    • Master SCL → Slave SCL
    • 4.7 kΩ external pull-up resistors between VCC and SDA/SCL.
    • Common GND between Master and Slave.

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.

So, by connecting and configuring the master and 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!