STANNUM
Open the simulator

Tutorials / Output

LED bar

A row of 2 to 16 LEDs, one GPIO each — the level meter of every stereo.

Parts in the example:
  • LED bar
  • Potentiometer
The LED bar block as it appears on the canvas.

What it is

A bar-graph display is a row of LEDs in one package, each with its own anode and a common cathode. Light the first n of them and you have a level meter: volume, battery, temperature — any quantity that is easier to glance at than to read.

In the simulator

The block shows one segment per LED, numbered from 1. The Number of LEDs setting (2 to 16) changes the block's pins: each segment is a signal pin, plus one ground. In the example, a potentiometer sets how many segments are lit.

Pins

PinWhat it is
1signal — wire it to a GPIO of the board
2signal — wire it to a GPIO of the board
3signal — wire it to a GPIO of the board
4signal — wire it to a GPIO of the board
5signal — wire it to a GPIO of the board
6signal — wire it to a GPIO of the board
7signal — wire it to a GPIO of the board
8signal — wire it to a GPIO of the board
GNDcommon ground of all segments — to a GND symbol

Wiring

The circuit below is the example LED bar — level meter from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
LED bar pinGoes to
1D23 on the board (GPIO 23)
2D22 on the board (GPIO 22)
3D21 on the board (GPIO 21)
4D19 on the board (GPIO 19)
5D18 on the board (GPIO 18)
6TX2 on the board (GPIO 17)
7RX2 on the board (GPIO 16)
8D4 on the board (GPIO 4)
GNDa GND symbol

Eight wires that do not cross: the sketch lists the GPIOs in the physical order of the board's right header (23, 22, 21, 19, 18, 17, 16, 4), so segment 1 sits level with the first pin. Out of order the circuit still works — the drawing just turns into a tangle.

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the LED bar.
SettingAcceptsDefaultNotes
Number of LEDs2 to 168Each LED takes one GPIO. Lowering it deletes the wires of the segments that stop existing.
LED colorRed · Green · Blue · Yellow · Orange · WhiteRed

Code

sketch.cpp
#include <Arduino.h>

// GPIOs in the SAME physical order they appear on the right header of the
// board (top to bottom). That way segment 1 of the bar sits level with the
// first pin, 2 with the second, and the eight wires run straight without
// crossing. Out of order the circuit still works, but the drawing turns into
// a tangle — and reading the schematic is half of the learning.
const int LEDS[8] = {23, 22, 21, 19, 18, 17, 16, 4};
const int POT = 34;

void setup() {
  Serial.begin(115200);
  for (int pino : LEDS) pinMode(pino, OUTPUT);
}

void loop() {
  int leitura = analogRead(POT);
  int acesos = map(leitura, 0, 4095, 0, 9);   // 0..8 LEDs

  for (int i = 0; i < 8; i++)
    digitalWrite(LEDS[i], i < acesos ? HIGH : LOW);

  Serial.printf("pot=%d LEDs on=%d\n", leitura, acesos);
  delay(60);
}

An array of pins and a loop: map() turns the reading (0 to 4095) into a count of lit segments (0 to 8), and each digitalWrite lights a segment if its index is below the count.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

Drag the potentiometer's slider from 0 to 100 % and the bar climbs from empty to full. The serial monitor prints the reading and the count.

How the simulation models it

Try this

  1. Turn it into a “dot” meter: light only the segment at the level, not all the ones below it.
  2. Make the top two segments blink when the reading passes 90 % — a peak warning.

See also