Level meter on an Arduino Uno
A potentiometer on A0 and a ten-segment LED bar graph with a resistor a bar. The top bar dims with PWM, so the meter moves smoothly. The whole build, an Arduino Uno and 12 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.
IntermediateRuns in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Level meter: a potentiometer on A0 and a ten-segment LED bar graph.
//
// potentiometer ends to 5 V and GND, wiper -> A0
// bar graph anodes A1..A10 -> pins 2 to 11, each through its own 220 ohm
// cathodes C1..C10 -> GND
//
// A bar graph is ten separate LEDs in one block, each with its own two legs,
// so it wants ten resistors exactly as ten LEDs would. Turn the knob and the
// bar follows; the top bar is dimmed with PWM in proportion to how far the
// reading is into it, so the meter moves smoothly rather than in tenths.
const uint8_t BAR_PINS[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
void setup() {
for (uint8_t i = 0; i < 10; i++) pinMode(BAR_PINS[i], OUTPUT);
}
void loop() {
// 0..1023 into 0..100: whole bars, and hundredths of the next one.
long level = (long)analogRead(A0) * 100 / 1023;
uint8_t whole = (uint8_t)(level / 10);
uint8_t part = (uint8_t)(level % 10);
for (uint8_t bar = 0; bar < 10; bar++) {
uint8_t pin = BAR_PINS[bar];
if (bar < whole) {
digitalWrite(pin, HIGH);
} else if (bar == whole && part > 0 && (pin == 3 || pin == 5 || pin == 6 || pin == 9 ||
pin == 10 || pin == 11)) {
analogWrite(pin, part * 25); // the pins with PWM can show the fraction
} else {
digitalWrite(pin, LOW);
}
}
delay(20);
}
Parts list
14 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
23 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 11; Resistor, 220 Ω (10) pin 1
- Arduino Uno R3 pin 10; Resistor, 220 Ω (9) pin 1
- Arduino Uno R3 pin 9; Resistor, 220 Ω (8) pin 1
- Arduino Uno R3 pin 8; Resistor, 220 Ω (7) pin 1
- Arduino Uno R3 pin 7; Resistor, 220 Ω (6) pin 1
- Arduino Uno R3 pin 6; Resistor, 220 Ω (5) pin 1
- Arduino Uno R3 pin 5; Resistor, 220 Ω (4) pin 1
- Arduino Uno R3 pin 4; Resistor, 220 Ω (3) pin 1
- Arduino Uno R3 pin 3; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 2; Resistor, 220 Ω (1) pin 1
- Arduino Uno R3 pin 5V; Potentiometer pin 1
- Ground: Arduino Uno R3 pin GND; LED bar graph, 10 segments pin C10; LED bar graph, 10 segments pin C9; LED bar graph, 10 segments pin C8; LED bar graph, 10 segments pin C7; LED bar graph, 10 segments pin C6; LED bar graph, 10 segments pin C5; LED bar graph, 10 segments pin C4; LED bar graph, 10 segments pin C3; LED bar graph, 10 segments pin C2; LED bar graph, 10 segments pin C1; Potentiometer pin 3
- Arduino Uno R3 pin A0; Potentiometer pin 2
- LED bar graph, 10 segments pin A1; Resistor, 220 Ω (1) pin 2
- LED bar graph, 10 segments pin A2; Resistor, 220 Ω (2) pin 2
- LED bar graph, 10 segments pin A3; Resistor, 220 Ω (3) pin 2
- LED bar graph, 10 segments pin A4; Resistor, 220 Ω (4) pin 2
- LED bar graph, 10 segments pin A5; Resistor, 220 Ω (5) pin 2
- LED bar graph, 10 segments pin A6; Resistor, 220 Ω (6) pin 2
- LED bar graph, 10 segments pin A7; Resistor, 220 Ω (7) pin 2
- LED bar graph, 10 segments pin A8; Resistor, 220 Ω (8) pin 2
- LED bar graph, 10 segments pin A9; Resistor, 220 Ω (9) pin 2
- LED bar graph, 10 segments pin A10; Resistor, 220 Ω (10) pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.