The first fill
Ten pins set as outputs, ten writes that fill the bar one segment at a time, and ten more that empty it at once. Forty lines, and every one of them is a thing you will change later.
What the sketch does
setup() runs once. It makes all ten pins outputs and writes HIGH to every one
of them, which is the line that keeps the bar dark at power-up. Leave it out and
the pins come up LOW, and the bar is fully lit before the program starts.
loop() runs forever. The first for loop writes LOW from D4 upwards with a
delay after each one, and nothing turns the earlier segments off, so the bar
fills. The second writes HIGH to all ten with no delay, so the bar goes out
rather than draining.
setup() doing its job: an output that starts LOW would light its segment the instant the sketch began, and the bar would come up already half full.The for loop is the point
Ten segments written out by hand is twenty lines that all say the same thing.
The loop says it once and counts. That is also why the pin numbers are
const int at the top: moving the bar to D2–D11 is two edits, not twenty.
Three things to change first
stepDelayMs. 50 is brisk, 500 is a slideshow. This is the number to play with.- The direction. Count down from
lastPinand the bar fills from the other end. - The second loop. Put a
delay(stepDelayMs)in it and the bar empties one segment at a time instead of all at once.
And one thing to notice
At the top of each fill, all ten segments are lit at the same time. That is the most current this circuit ever draws, and on an Uno it is a number worth knowing — which is the next page.
The code
The demo sketch. Upload it, and the bar should fill from the segment on D4 to the segment on D13, go out, and start again. If it fills from the wrong end, your ten wires are in reverse order — swap them or count down instead of up.
// Wiring for this sketch.
//
// Resistor board COMMON -> Uno 5V
// Resistor board row -> one row of the bar's pins, segment for segment
// Bar's other row -> Uno D4 (segment 1) … D13 (segment 10)
//
// No ground wire to the bar: the GPIO pins are the ground end of every
// segment. Active low — LOW lights a segment, HIGH puts it out.
//
// Arduino IDE: Tools > Board > Arduino Uno, and the port your board is on.
// No library.
const int firstPin = 4; // segment 1
const int lastPin = 13; // segment 10
const int stepDelayMs = 120; // how long each segment waits before the next
const int allOffDelayMs = 300; // the pause with the bar dark
void setup() {
for (int pin = firstPin; pin <= lastPin; pin++) {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH); // HIGH is off, so the bar starts dark
}
}
void loop() {
// Fill: each segment stays on once it is on.
for (int pin = firstPin; pin <= lastPin; pin++) {
digitalWrite(pin, LOW);
delay(stepDelayMs);
}
// Empty: ten writes with no delay between them, so the bar goes out.
for (int pin = firstPin; pin <= lastPin; pin++) {
digitalWrite(pin, HIGH);
}
delay(allOffDelayMs);
}stepDelayMs is the fill speed: try 50 and then 500. Changing the second loop's digitalWrite to LOW, with a delay, turns the fill into a sweep that empties one segment at a time.
When it does not work
The ten wires are in the opposite order to the ten segments. Either move them, or count the loop down — `for (int pin = lastPin; pin >= firstPin; pin--)`. Nothing is wrong electrically.
That is the bootloader, not your sketch. D13 also drives the Uno's own L LED, and the bootloader blinks it at every reset and at the start of every upload. The segment on D13 blinks with it. Move that segment to another pin if it bothers you — A0 to A5 work as digital pins too.
That is the second loop working as written: ten digitalWrite calls with no delay between them take a few microseconds. Put a delay(stepDelayMs) inside that loop and you get a sweep.
Check COMMON against GND with a meter before you read the code — it should be 5 V. When nothing lights walks through the four faults that all look like this one.
If it is a multicolour bar, that is the forward voltage and it is expected. If it is a single-colour bar, look for a bad joint on the resistor board, or, when all ten are lit at once, read ten segments, one chip.
Edit this page — content/books/led-bar-graph/the-first-fill.mdx
Questions about this product
See what other owners have asked, and read their solutions.
10-Segment LED Bar Graph Kit, 12 Bars in 6 Colours
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.