56. LED Brightness Control Using I2C

In this task, we are implementing a system where a potentiometer connected to the Master microcontroller is rotated, and the Slave microcontroller controls the brightness of 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-master-slave-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 brightness control 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.

Hardware Setup

  • Master Connection:
    • Connect the potentiometer’s VCC to 3.3V/5V (as per MCU logic level), GND to ground, and the wiper to any analog input GPIO pin on the Master microcontroller.
  • Slave Connection
    • Connect the LED’s anode to a PWM-capable GPIO pin on the Slave microcontroller through a suitable current-limiting resistor
    • Connect the LED’s cathode to ground.
  • 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!