Arduino Mega - 10 Segment LED Bar Graph

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×10 Segment LED Bar Graph
10×220 Ω Resistor
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

Or you can buy the following kits:

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 Arduino Mega 2560 to control the 10 Segment LED Bar Graph display. In detail, we will learn:

Arduino Mega 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

The 10 Segment LED Bar Graph Pinout

10 Segment LED Bar Graph pinout

The 10 Segment LED Bar Graph has 20 pins in total — 10 anode pins and 10 cathode pins:

  • Anode pins (A1–A10): must be connected to GPIO pins via 220Ω resistors
  • Cathode pins (K1–K10): must be connected to GND

※ NOTE THAT:

The pin arrangement on your LED bar graph may differ from one manufacturer to another. Always refer to the datasheet or labels on your component. Take a close look!

Wiring Diagram

The wiring connects each anode pin of the LED bar graph to a dedicated Arduino Mega GPIO pin through a 220Ω current-limiting resistor.

The wiring diagram between Arduino Mega and 10 Segment LED Bar Graph

This image is created using Fritzing. Click to enlarge image

LED Bar Graph Arduino Mega Pin
A1 (Anode 1) Pin 2 (via 220Ω)
A2 (Anode 2) Pin 3 (via 220Ω)
A3 (Anode 3) Pin 4 (via 220Ω)
A4 (Anode 4) Pin 5 (via 220Ω)
A5 (Anode 5) Pin 6 (via 220Ω)
A6 (Anode 6) Pin 7 (via 220Ω)
A7 (Anode 7) Pin 8 (via 220Ω)
A8 (Anode 8) Pin 9 (via 220Ω)
A9 (Anode 9) Pin 10 (via 220Ω)
A10 (Anode 10) Pin 11 (via 220Ω)
K1–K10 (Cathodes) GND

How To Program Arduino Mega 2560 for 10 Segment LED Bar Graph

The first step is to define the array of pins connected to the LED bar graph anodes:

const int NUM_SEGMENTS = 10; const int ledPins[NUM_SEGMENTS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

Each pin must be configured as an output using pinMode() inside the setup() function:

pinMode(ledPins[i], OUTPUT);

To turn on a segment, write a HIGH signal to the corresponding pin:

digitalWrite(ledPins[i], HIGH);

To turn off a segment, write a LOW signal to the corresponding pin:

digitalWrite(ledPins[i], LOW);

No library is needed — the LED Bar Graph is driven entirely by direct GPIO control.

Arduino Mega 2560 Code for 10 Segment LED Bar Graph

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-10-segment-led-bar-graph */ const int NUM_SEGMENTS = 10; const int ledPins[NUM_SEGMENTS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; 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(9600); 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); }

Detailed Instructions

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

  1. Wire the components as shown in the diagram.
  2. Connect the Arduino Mega board to your computer using a USB cable.
  3. Open Arduino IDE on your computer.
  4. Select the correct board: go to ToolsBoardArduino AVR BoardsArduino Mega or Mega 2560.
  5. Select the correct COM port: go to ToolsPort and choose the port that corresponds to your Arduino Mega.
  6. Copy the code above and paste it into the Arduino IDE editor.
  7. Click the Upload button to compile and upload the code to the Arduino Mega board.
  8. Open the Serial Monitor (ToolsSerial Monitor) and set the baud rate to 9600.
  9. Observe the LED bar graph filling up segment by segment, then emptying in reverse, repeating continuously.

Serial Monitor Output

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' 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
Arduino Mega Or... on COM15
2

Additional Knowledge

When driving a 10 Segment LED Bar Graph, there are two common approaches:

Direct GPIO Control (used in this tutorial): Each anode pin is connected directly to a dedicated GPIO pin on the Arduino Mega. This approach is straightforward — no additional hardware or library is required — but it consumes 10 GPIO pins, which may be a concern on boards with fewer available pins.

Shift Register Approach (e.g., 74HC595): A shift register allows the same 10 LED segments to be controlled using only 3 GPIO pins (data, clock, and latch). This conserves pins significantly, which is useful when many peripherals are connected simultaneously. However, the code is more complex, as data must be shifted out serially to the register before the LEDs respond.

For projects where GPIO availability is not a constraint, direct control is the simpler and more readable solution. For more complex projects that require many outputs, a shift register is the recommended approach.

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!