ESP32 MicroPython 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×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 provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

This tutorial instructs you how to use the 10 Segment LED Bar Graph with the ESP32 and MicroPython. In detail, we will learn:

ESP32 MicroPython and 10 Segment LED Bar Graph

Overview of 10 Segment LED Bar Graph

10 Segment LED Bar Graph
Number of Segments 10
LED Color Bright red
Forward Voltage ~2V per segment
Forward Current 20mA max per segment
Resistor Required 220Ω per segment

Pinout

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

Wiring Diagram between 10 Segment LED Bar Graph and ESP32

The wiring diagram between ESP32 MicroPython and 10 Segment LED Bar Graph  - breadboard

This image is created using Fritzing. Click to enlarge image

※ 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.

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

ESP32 MicroPython Code - 10 Segment LED Bar Graph

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-10-segment-led-bar-graph """ from machine import Pin import time NUM_SEGMENTS = 10 LED_PIN_NUMS = [23, 22, 21, 19, 18, 5, 17, 16, 4, 15] leds = [Pin(p, Pin.OUT) for p in LED_PIN_NUMS] def print_bar(lit_count): bar = '*' * lit_count + ' ' * (NUM_SEGMENTS - lit_count) print('Bar: [' + bar + ']') for led in leds: led.value(0) while True: for i in range(NUM_SEGMENTS): leds[i].value(1) print_bar(i + 1) time.sleep(0.1) time.sleep(0.5) for i in range(NUM_SEGMENTS - 1, -1, -1): leds[i].value(0) print_bar(i) time.sleep(0.1) time.sleep(0.5)

Here's instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time, refer to the ESP32 MicroPython Getting Started guide.
  • Connect the 10 Segment LED Bar Graph to the ESP32 according to the provided diagram.
  • Connect the ESP32 to your computer with a USB cable.
  • Open Thonny IDE.
  • In Thonny, go to Tools → Options, Interpreter tab, and select MicroPython (ESP32) from the dropdown.
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by: choosing File → Save as..., selecting MicroPython device, and naming the file main.py.
  • Click the green Run button (or press F5) to run the script.
  • Watch the LED bar animate on your breadboard.
  • Check the Shell at the bottom of Thonny for the printed bar output.

Thonny Console

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Bar: [* ] Bar: [** ] Bar: [*** ] Bar: [**** ] Bar: [***** ] Bar: [****** ] Bar: [******* ] Bar: [******** ] Bar: [********* ] Bar: [**********] Bar: [********* ] Bar: [******** ] Bar: [******* ] Bar: [****** ] Bar: [***** ] Bar: [**** ] Bar: [*** ] Bar: [** ] Bar: [* ] Bar: [ ]
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

If you name your script main.py and save it to the MicroPython device, it will run automatically every time the ESP32 is powered on.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!