Arduino Mega LED Bar Graph: Wiring, Code and Simulator
To build this Arduino Mega LED Bar Graph project you need an Arduino Mega 2560 and an LED bar graph, a potentiometer, a 220 Ω resistor and a blue LED. An Arduino Mega 2560 lights a 10-segment bar graph on pins 22–31 from a pot on A8 and dims an LED on pin 44 with timer 5 — pins, inputs and timers the Uno does not have. Make the 26 connections in the wiring table, upload the Arduino C++ code, and turn the pot: the bar graph fills, the serial monitor reports the level, and the LED on pin 44 brightens with it.
Open this circuit in the free Arduino Mega 2560 simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Mega 2560 | the microcontroller board |
| 1 | LED Bar Graph | Ten LEDs in a row: A1–A10 anodes, C1–C10 cathodes. |
| 1 | Potentiometer | VCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023. |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | blue LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Mega 2560 pin 22 | LED Bar Graph A1 |
| 2 | Arduino Mega 2560 pin 23 | LED Bar Graph A2 |
| 3 | Arduino Mega 2560 pin 24 | LED Bar Graph A3 |
| 4 | Arduino Mega 2560 pin 25 | LED Bar Graph A4 |
| 5 | Arduino Mega 2560 pin 26 | LED Bar Graph A5 |
| 6 | Arduino Mega 2560 pin 27 | LED Bar Graph A6 |
| 7 | Arduino Mega 2560 pin 28 | LED Bar Graph A7 |
| 8 | Arduino Mega 2560 pin 29 | LED Bar Graph A8 |
| 9 | Arduino Mega 2560 pin 30 | LED Bar Graph A9 |
| 10 | Arduino Mega 2560 pin 31 | LED Bar Graph A10 |
| 11 | Arduino Mega 2560 GND | LED Bar Graph C1 |
| 12 | Arduino Mega 2560 GND | LED Bar Graph C2 |
| 13 | Arduino Mega 2560 GND | LED Bar Graph C3 |
| 14 | Arduino Mega 2560 GND | LED Bar Graph C4 |
| 15 | Arduino Mega 2560 GND | LED Bar Graph C5 |
| 16 | Arduino Mega 2560 GND | LED Bar Graph C6 |
| 17 | Arduino Mega 2560 GND | LED Bar Graph C7 |
| 18 | Arduino Mega 2560 GND | LED Bar Graph C8 |
| 19 | Arduino Mega 2560 GND | LED Bar Graph C9 |
| 20 | Arduino Mega 2560 GND | LED Bar Graph C10 |
| 21 | Arduino Mega 2560 5V | Potentiometer VCC |
| 22 | Arduino Mega 2560 GND | Potentiometer GND |
| 23 | Arduino Mega 2560 A8 | Potentiometer SIG |
| 24 | Arduino Mega 2560 pin 44 | 220 Ω resistor leg 1 |
| 25 | 220 Ω resistor leg 2 | blue LED anode (A, long leg) |
| 26 | Arduino Mega 2560 GND | blue LED cathode (C, short leg) |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Mega 2560. No install or account is needed.
- Add the parts. Add an LED bar graph, a potentiometer, a 220 Ω resistor and a blue LED from the parts palette.
- Wire 1. Connect Arduino Mega 2560 pin 22 to LED Bar Graph A1.
- Wire 2. Connect Arduino Mega 2560 pin 23 to LED Bar Graph A2.
- Wire 3. Connect Arduino Mega 2560 pin 24 to LED Bar Graph A3.
- Wire 4. Connect Arduino Mega 2560 pin 25 to LED Bar Graph A4.
- Wire 5. Connect Arduino Mega 2560 pin 26 to LED Bar Graph A5.
- Wire 6. Connect Arduino Mega 2560 pin 27 to LED Bar Graph A6.
- Wire 7. Connect Arduino Mega 2560 pin 28 to LED Bar Graph A7.
- Wire 8. Connect Arduino Mega 2560 pin 29 to LED Bar Graph A8.
- Wire 9. Connect Arduino Mega 2560 pin 30 to LED Bar Graph A9.
- Wire 10. Connect Arduino Mega 2560 pin 31 to LED Bar Graph A10.
- Wire 11. Connect Arduino Mega 2560 GND to LED Bar Graph C1.
- Wire 12. Connect Arduino Mega 2560 GND to LED Bar Graph C2.
- Wire 13. Connect Arduino Mega 2560 GND to LED Bar Graph C3.
- Wire 14. Connect Arduino Mega 2560 GND to LED Bar Graph C4.
- Wire 15. Connect Arduino Mega 2560 GND to LED Bar Graph C5.
- Wire 16. Connect Arduino Mega 2560 GND to LED Bar Graph C6.
- Wire 17. Connect Arduino Mega 2560 GND to LED Bar Graph C7.
- Wire 18. Connect Arduino Mega 2560 GND to LED Bar Graph C8.
- Wire 19. Connect Arduino Mega 2560 GND to LED Bar Graph C9.
- Wire 20. Connect Arduino Mega 2560 GND to LED Bar Graph C10.
- Wire 21. Connect Arduino Mega 2560 5V to Potentiometer VCC.
- Wire 22. Connect Arduino Mega 2560 GND to Potentiometer GND.
- Wire 23. Connect Arduino Mega 2560 A8 to Potentiometer SIG.
- Wire 24. Connect Arduino Mega 2560 pin 44 to 220 Ω resistor leg 1.
- Wire 25. Connect 220 Ω resistor leg 2 to blue LED anode (A, long leg).
- Wire 26. Connect Arduino Mega 2560 GND to blue LED cathode (C, short leg).
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Turn the pot: the bar graph fills, the serial monitor reports the level, and the LED on pin 44 brightens with it.
Arduino C++ code
// Arduino Mega: a 10-LED bar graph on pins 22-31, set by a pot on A8.
// Pins 22-31 and analog inputs A8-A15 exist only on the Mega, and pin 44
// is a PWM output driven by timer 5 (the Uno has no timers 3-5).
const int FIRST_LED = 22;
const int LEDS = 10;
int level = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < LEDS; i++) pinMode(FIRST_LED + i, OUTPUT);
pinMode(44, OUTPUT);
Serial.println("Mega bar graph ready");
}
void loop() {
int raw = analogRead(A8); // 0..1023
level = map(raw, 0, 1023, 0, LEDS); // how many LEDs to light
for (int i = 0; i < LEDS; i++) digitalWrite(FIRST_LED + i, i < level ? HIGH : LOW);
analogWrite(44, raw / 4); // pin 44: PWM on timer 5
Serial.print("A8 = ");
Serial.print(raw);
Serial.print(" level = ");
Serial.println(level);
delay(200);
}
What to try
Turn the pot: the bar graph fills, the serial monitor reports the level, and the LED on pin 44 brightens with it.
Frequently asked questions
How do I wire the LED Bar Graph project on an Arduino Mega 2560?
Make 26 connections: Arduino Mega 2560 pin 22 to LED Bar Graph A1; Arduino Mega 2560 pin 23 to LED Bar Graph A2; Arduino Mega 2560 pin 24 to LED Bar Graph A3; Arduino Mega 2560 pin 25 to LED Bar Graph A4; Arduino Mega 2560 pin 26 to LED Bar Graph A5; Arduino Mega 2560 pin 27 to LED Bar Graph A6; Arduino Mega 2560 pin 28 to LED Bar Graph A7; Arduino Mega 2560 pin 29 to LED Bar Graph A8; Arduino Mega 2560 pin 30 to LED Bar Graph A9; Arduino Mega 2560 pin 31 to LED Bar Graph A10; Arduino Mega 2560 GND to LED Bar Graph C1; Arduino Mega 2560 GND to LED Bar Graph C2; Arduino Mega 2560 GND to LED Bar Graph C3; Arduino Mega 2560 GND to LED Bar Graph C4; Arduino Mega 2560 GND to LED Bar Graph C5; Arduino Mega 2560 GND to LED Bar Graph C6; Arduino Mega 2560 GND to LED Bar Graph C7; Arduino Mega 2560 GND to LED Bar Graph C8; Arduino Mega 2560 GND to LED Bar Graph C9; Arduino Mega 2560 GND to LED Bar Graph C10; Arduino Mega 2560 5V to Potentiometer VCC; Arduino Mega 2560 GND to Potentiometer GND; Arduino Mega 2560 A8 to Potentiometer SIG; Arduino Mega 2560 pin 44 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to blue LED anode (A, long leg); Arduino Mega 2560 GND to blue LED cathode (C, short leg).
What parts do I need for this Arduino Mega project?
An Arduino Mega 2560 and an LED bar graph, a potentiometer, a 220 Ω resistor and a blue LED.
Can I simulate this Arduino Mega project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega2560.
What does the code do?
An Arduino Mega 2560 lights a 10-segment bar graph on pins 22–31 from a pot on A8 and dims an LED on pin 44 with timer 5 — pins, inputs and timers the Uno does not have.