Built-in project · Arduino Uno R3

LED bar graph on pins 3 to 12, on an Arduino Uno

A ten-segment LED bar graph with a 220 ohm resistor a bar on pins 3 to 12: one bar lights at a time and runs up the graph and back down, driven from an array of pin numbers. The whole build, an Arduino Uno and 11 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Arrays of pin numbers
  • For loops to drive many outputs
  • A resistor for every LED
  • Animating by lighting one LED at a time
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 13

How it works

A ten-segment LED bar graph has ten separate LEDs in one package. Each anode goes through its own 220 ohm resistor to a pin from 3 to 12, and all ten cathodes go to ground. Pin 3 drives the first bar and pin 12 the last.

The pin numbers sit in an array, so loops can visit every bar. A function lights one bar and turns off the other nine.

The loop runs the lit bar from the first to the last, then back, 100 ms per step.

Parts list

13 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω (10) pin 1Arduino Uno R3 pin 12
Resistor, 220 Ω (9) pin 1Arduino Uno R3 pin 11
Resistor, 220 Ω (8) pin 1Arduino Uno R3 pin 10
Resistor, 220 Ω (7) pin 1Arduino Uno R3 pin 9
Resistor, 220 Ω (6) pin 1Arduino Uno R3 pin 8
Resistor, 220 Ω (5) pin 1Arduino Uno R3 pin 7
Resistor, 220 Ω (4) pin 1Arduino Uno R3 pin 6
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 5
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 4
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 3
LED bar graph, 10 segments pin C10Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C9Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C8Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C7Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C6Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C5Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C4Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C3Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C2Arduino Uno R3 pin GND
LED bar graph, 10 segments pin C1Arduino Uno R3 pin GND
Resistor, 220 Ω (1) pin 2LED bar graph, 10 segments pin A1
Resistor, 220 Ω (2) pin 2LED bar graph, 10 segments pin A2
Resistor, 220 Ω (3) pin 2LED bar graph, 10 segments pin A3
Resistor, 220 Ω (4) pin 2LED bar graph, 10 segments pin A4
Resistor, 220 Ω (5) pin 2LED bar graph, 10 segments pin A5
Resistor, 220 Ω (6) pin 2LED bar graph, 10 segments pin A6
Resistor, 220 Ω (7) pin 2LED bar graph, 10 segments pin A7
Resistor, 220 Ω (8) pin 2LED bar graph, 10 segments pin A8
Resistor, 220 Ω (9) pin 2LED bar graph, 10 segments pin A9
Resistor, 220 Ω (10) pin 2LED bar graph, 10 segments pin A10

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// An LED bar graph: ten bars on pins 3 to 12, one lit at a time.
//
//   pins 3..12 -> 220 ohm each -> the bar graph's ten anodes
//   the ten cathodes -> GND
//
// The pin numbers live in an array, so one for loop can visit them all.
// show(n) lights bar n and turns the other nine off; calling it for 0 to 9
// and back again makes the light run up the graph and down.

const int COUNT = 10;
const int PINS[COUNT] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

void show(int lit) {
  for (int i = 0; i < COUNT; i++) digitalWrite(PINS[i], i == lit ? HIGH : LOW);
  delay(100);
}

void setup() {
  for (int i = 0; i < COUNT; i++) pinMode(PINS[i], OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: LED bar graph on pins 3 to 12");
}

void loop() {
  for (int i = 0; i < COUNT; i++) show(i);
  for (int i = COUNT - 1; i >= 0; i--) show(i);
}

Code walkthrough

The key functions of the sketch, and what each one does.

show sketch.ino

Goes through all ten entries in PINS. A pin gets HIGH only when its index equals the lit argument, and LOW otherwise. That leaves exactly one bar on. It then waits 100 ms, which sets the speed of the animation.

void show(int lit) {
  for (int i = 0; i < COUNT; i++) digitalWrite(PINS[i], i == lit ? HIGH : LOW);
  delay(100);
}

setup sketch.ino

Uses a loop to set every pin in PINS as an output, then starts serial at 9600 baud and prints a message.

void setup() {
  for (int i = 0; i < COUNT; i++) pinMode(PINS[i], OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: LED bar graph on pins 3 to 12");
}

loop sketch.ino

Counts i from 0 to 9, calling show for each, so the light runs up the graph. Then it counts from 9 back to 0, so it runs down.

void loop() {
  for (int i = 0; i < COUNT; i++) show(i);
  for (int i = COUNT - 1; i >= 0; i--) show(i);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • A bar never lights This project
    Check that its resistor is on the right pin, and that every cathode (C1 to C10) is joined to ground.
  • The light jumps around instead of running in order This project
    Resistors must go to the pins in order, pin 3 to bar 1 up to pin 12 to bar 10.
  • Segments are dim, uneven, or the chip is damaged. LED bar graph
    Every segment is its own LED and needs its own current-limiting resistor, about 220 to 330 ohms at 5 V. One shared resistor makes brightness change with how many are lit.
  • The bar lights in reverse order or a segment never lights. LED bar graph
    Check pin 1 and the orientation. Anode and cathode sides are on opposite rows. Some bars are numbered the other way around, so test with one segment at a time.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.