18. Bar Graph

We’re going to build a simple LED bar graph using a potentiometer.
As we rotate the potentiometer, the LEDs will light up one by one smoothly. 

Solving Approach:

  • We’ll split the potentiometer’s ADC range into 5 equal parts (since we have 5 LEDs).
  • In each part, the current LED gradually increases its brightness from 0% to 100%.
  • We’ll use PWM to smoothly control LED brightness and map the potentiometer’s ADC value to PWM duty cycle for each LED.
  • All earlier LEDs stay fully ON, and the later ones stay OFF.
  • This gives a nice bar graph effect, like the LEDs are “filling up” smoothly as you rotate the knob.

So we have to interface a potentiometer and five LEDs with a microcontroller.

Potentiometer & LED Interfacing

Potentiometer Interfacing

potentiometer-pinout
  • Connection: Connect the potentiometer terminals 1 and 3 to VCC and GND or vice versa. Terminal 2 (wiper) to the MCU ADC pin.

Note: We can use any of the potentiometers with values between 1kΩ and 10kΩ.

Five LEDs Interfacing

Connect each PWM GPIO pin to an anode of a separate LED, and the cathode of each LED to GND with a current-limiting resistor in series to protect LEDs and GPIO pins.

Calculating the current-limited Resistor Value

Case 1: 5V Supply

  • LED forward voltage (Vf) = 1.8V (from datasheet)
  • Voltage across resistor (VR) = Supply voltage – Vf = 5V – 1.8V = 3.2V
  • Resistor value (R) = VR / I = 3.2V / 10 mA = 320 Ω

Standard resistor values near 320 Ω: 330 Ω or 300 Ω (whichever is available).

Similarly, Case 2: 3.3V Supply

  • Voltage across resistor (VR) = 3.3V – 1.8V = 1.5V
  • Resistor value (R) = 1.5V / 10 mA = 150 Ω

Standard resistor value: 150 Ω.

So, by selecting a proper resistor, LED, and potentiometer connections, we can implement the task.

Below are the solutions to the given task using different microcontrollers

  1. STM32
  2. ESP32
  3. Arduino UNO