ESP32 - 10 Segment LED Bar Graph

1×38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
1×Alternatively, 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Optionally, DC Power Jack
1×10 Segment LED Bar Graph
10×220 Ω Resistor
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables .

This tutorial instructs you how to use ESP32 to control a 10 Segment LED Bar Graph and display a visual bar animation on the display.

Introduction to 10 Segment LED Bar Graph

10 Segment LED Bar Graph
The number of segments 10
The LED color Bright red
The forward voltage ~2V per segment
The forward current 20mA max per segment
The required resistor 220Ω per segment

LED Bar Graph Pinout

10 Segment LED Bar Graph pinout
  • Anode pins (A1–A10): connect these pins to GPIO pins via 220Ω resistors
  • Cathode pins (K1–K10): connect these pins to GND (0V)

We highly recommend connecting current-limiting resistors on every anode to protect the ESP32 GPIO pins.

※ NOTE THAT:

All 10 anode pins are wired down a single side of the ESP32 Dev Board header, in physical pin order, to keep the wiring straightforward. This does mean GPIO5 and GPIO15 are used — both only briefly pulse at boot and are commonly used for onboard LEDs, so they're low-risk. GPIO0, GPIO2, and GPIO12, which can actually interfere with the board's boot or flashing mode, are avoided entirely.

Wiring Diagram between 10 Segment LED Bar Graph and ESP32

ESP32 LED Bar Graph Wiring Diagram - Breadboard

This image is created using Fritzing. Click to enlarge image

LED Bar Graph ESP32 Pin
A1 (Anode 1) GPIO23 (via 220Ω)
A2 (Anode 2) GPIO22 (via 220Ω)
A3 (Anode 3) GPIO21 (via 220Ω)
A4 (Anode 4) GPIO19 (via 220Ω)
A5 (Anode 5) GPIO18 (via 220Ω)
A6 (Anode 6) GPIO5 (via 220Ω)
A7 (Anode 7) GPIO17 (via 220Ω)
A8 (Anode 8) GPIO16 (via 220Ω)
A9 (Anode 9) GPIO4 (via 220Ω)
A10 (Anode 10) GPIO15 (via 220Ω)
K1–K10 (Cathodes) GND

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

How To Program ESP32 for 10 Segment LED Bar Graph

  • Programming the ESP32 for the 10 Segment LED Bar Graph is straightforward. No library installation is required.
  • Define an array of GPIO pins assigned to each segment: const int ledPins[10] = {23, 22, 21, 19, 18, 5, 17, 16, 4, 15};
  • Set each pin as an output inside setup(): pinMode(ledPins[i], OUTPUT);
  • Turn a segment on by writing HIGH to its pin: digitalWrite(ledPins[i], HIGH);
  • Turn a segment off by writing LOW to its pin: digitalWrite(ledPins[i], LOW);

ESP32 Code - 10 Segment LED Bar Graph

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-10-segment-led-bar-graph */ const int NUM_SEGMENTS = 10; const int ledPins[NUM_SEGMENTS] = {23, 22, 21, 19, 18, 5, 17, 16, 4, 15}; void printBar(int litCount) { Serial.print("Bar: ["); for (int i = 0; i < NUM_SEGMENTS; i++) { Serial.print(i < litCount ? "*" : " "); } Serial.println("]"); } void setup() { Serial.begin(115200); for (int i = 0; i < NUM_SEGMENTS; i++) { pinMode(ledPins[i], OUTPUT); digitalWrite(ledPins[i], LOW); } } void loop() { for (int i = 0; i < NUM_SEGMENTS; i++) { digitalWrite(ledPins[i], HIGH); printBar(i + 1); delay(100); } delay(500); for (int i = NUM_SEGMENTS - 1; i >= 0; i--) { digitalWrite(ledPins[i], LOW); printBar(i); delay(100); } delay(500); }

Quick Instructions

If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.

  • Copy the above code and open it in Arduino IDE.
  • Select the ESP-WROOM-32 Dev Module board and the correct COM port.
  • Click the Upload button to compile and upload the code to the ESP32.
  • Open the Serial Monitor, set the baud rate to 115200.
  • Observe the bar animation on the LED Bar Graph and the output printed in the Serial Monitor.

Serial Monitor Output

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Bar: [* ] Bar: [** ] Bar: [*** ] Bar: [**** ] Bar: [***** ] Bar: [****** ] Bar: [******* ] Bar: [******** ] Bar: [********* ] Bar: [**********] Bar: [********* ] Bar: [******** ] Bar: [******* ] Bar: [****** ] Bar: [***** ] Bar: [**** ] Bar: [*** ] Bar: [** ] Bar: [* ] Bar: [ ]
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES