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

QtyPartWhat it does
1Arduino Mega 2560the microcontroller board
1LED Bar GraphTen LEDs in a row: A1–A10 anodes, C1–C10 cathodes.
1PotentiometerVCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1blue LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1Arduino Mega 2560 pin 22LED Bar Graph A1
2Arduino Mega 2560 pin 23LED Bar Graph A2
3Arduino Mega 2560 pin 24LED Bar Graph A3
4Arduino Mega 2560 pin 25LED Bar Graph A4
5Arduino Mega 2560 pin 26LED Bar Graph A5
6Arduino Mega 2560 pin 27LED Bar Graph A6
7Arduino Mega 2560 pin 28LED Bar Graph A7
8Arduino Mega 2560 pin 29LED Bar Graph A8
9Arduino Mega 2560 pin 30LED Bar Graph A9
10Arduino Mega 2560 pin 31LED Bar Graph A10
11Arduino Mega 2560 GNDLED Bar Graph C1
12Arduino Mega 2560 GNDLED Bar Graph C2
13Arduino Mega 2560 GNDLED Bar Graph C3
14Arduino Mega 2560 GNDLED Bar Graph C4
15Arduino Mega 2560 GNDLED Bar Graph C5
16Arduino Mega 2560 GNDLED Bar Graph C6
17Arduino Mega 2560 GNDLED Bar Graph C7
18Arduino Mega 2560 GNDLED Bar Graph C8
19Arduino Mega 2560 GNDLED Bar Graph C9
20Arduino Mega 2560 GNDLED Bar Graph C10
21Arduino Mega 2560 5VPotentiometer VCC
22Arduino Mega 2560 GNDPotentiometer GND
23Arduino Mega 2560 A8Potentiometer SIG
24Arduino Mega 2560 pin 44220 Ω resistor leg 1
25220 Ω resistor leg 2blue LED anode (A, long leg)
26Arduino Mega 2560 GNDblue LED cathode (C, short leg)

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Mega 2560. No install or account is needed.
  2. Add the parts. Add an LED bar graph, a potentiometer, a 220 Ω resistor and a blue LED from the parts palette.
  3. Wire 1. Connect Arduino Mega 2560 pin 22 to LED Bar Graph A1.
  4. Wire 2. Connect Arduino Mega 2560 pin 23 to LED Bar Graph A2.
  5. Wire 3. Connect Arduino Mega 2560 pin 24 to LED Bar Graph A3.
  6. Wire 4. Connect Arduino Mega 2560 pin 25 to LED Bar Graph A4.
  7. Wire 5. Connect Arduino Mega 2560 pin 26 to LED Bar Graph A5.
  8. Wire 6. Connect Arduino Mega 2560 pin 27 to LED Bar Graph A6.
  9. Wire 7. Connect Arduino Mega 2560 pin 28 to LED Bar Graph A7.
  10. Wire 8. Connect Arduino Mega 2560 pin 29 to LED Bar Graph A8.
  11. Wire 9. Connect Arduino Mega 2560 pin 30 to LED Bar Graph A9.
  12. Wire 10. Connect Arduino Mega 2560 pin 31 to LED Bar Graph A10.
  13. Wire 11. Connect Arduino Mega 2560 GND to LED Bar Graph C1.
  14. Wire 12. Connect Arduino Mega 2560 GND to LED Bar Graph C2.
  15. Wire 13. Connect Arduino Mega 2560 GND to LED Bar Graph C3.
  16. Wire 14. Connect Arduino Mega 2560 GND to LED Bar Graph C4.
  17. Wire 15. Connect Arduino Mega 2560 GND to LED Bar Graph C5.
  18. Wire 16. Connect Arduino Mega 2560 GND to LED Bar Graph C6.
  19. Wire 17. Connect Arduino Mega 2560 GND to LED Bar Graph C7.
  20. Wire 18. Connect Arduino Mega 2560 GND to LED Bar Graph C8.
  21. Wire 19. Connect Arduino Mega 2560 GND to LED Bar Graph C9.
  22. Wire 20. Connect Arduino Mega 2560 GND to LED Bar Graph C10.
  23. Wire 21. Connect Arduino Mega 2560 5V to Potentiometer VCC.
  24. Wire 22. Connect Arduino Mega 2560 GND to Potentiometer GND.
  25. Wire 23. Connect Arduino Mega 2560 A8 to Potentiometer SIG.
  26. Wire 24. Connect Arduino Mega 2560 pin 44 to 220 Ω resistor leg 1.
  27. Wire 25. Connect 220 Ω resistor leg 2 to blue LED anode (A, long leg).
  28. Wire 26. Connect Arduino Mega 2560 GND to blue LED cathode (C, short leg).
  29. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  30. 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.