Level light on an Arduino Uno
A potentiometer sets a level and a two-color LED shows it: red when low, through orange and yellow, to green when full. The whole build, an Arduino Uno and 4 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 15 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.
What you'll learn
- Reading a potentiometer with analogRead
- Mixing two colors with PWM
- Piecewise mapping of one input onto two outputs
- Why each LED die needs its own resistor
- Printing only when a value changes
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
A potentiometer on A0 sets a level from 0 to 1023. A two-color LED has a red and a green die under one lens, each on its own PWM pin through a 220 ohm resistor: red on pin 9 and green on pin 10.
Below halfway, red stays full and green rises with the knob. Above halfway, green stays full and red fades out. Your eye blends them: red, then orange and yellow, then green. The serial monitor prints a line when the percentage changes.
Parts list
6 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Two-color LED, KY-011
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
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 pin | Connects to |
|---|---|
| Potentiometer pin 1 | Arduino Uno R3 pin GND |
| Two-color LED, KY-011 pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 10 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 9 |
| Potentiometer pin 3 | Arduino Uno R3 pin 5V |
| Potentiometer pin 2 | Arduino Uno R3 pin A0 |
| Resistor, 220 Ω (2) pin 2 | Two-color LED, KY-011 pin R |
| Resistor, 220 Ω (1) pin 2 | Two-color LED, KY-011 pin G |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Level light: a potentiometer sets a level, and a two-color LED shows it,
// red when it is low, through orange and yellow, to green when it is full.
// The same light a battery charger or a fuel gauge uses.
//
// two-color LED R -> pin 9 through 220 ohm G -> pin 10 through 220 ohm
// C (the middle leg) -> GND
// potentiometer ends to 5 V and GND, wiper -> A0
//
// A two-color LED is two LEDs, a red one and a yellow-green one, under one
// lens with a shared leg. Each needs its own resistor, as two LEDs would, and
// the KY-011 module has none on it. Light both and the lens glows somewhere
// between the two: the eye adds red and green light into orange and yellow.
//
// So the sketch walks the mix: below halfway the red die is full and the
// green one comes up with the knob; above halfway the green die is full and
// the red one goes down. Turn the knob and watch the lens.
const int RED_PIN = 9; // PWM
const int GREEN_PIN = 10; // PWM
int shown = -1;
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
Serial.println("Level light: turn the knob");
}
void loop() {
int level = analogRead(A0); // 0 to 1023
int red = level < 512 ? 255 : (1023 - level) / 2;
int green = level < 512 ? level / 2 : 255;
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
// A line when the level moves by a whole percent.
int percent = (long)level * 100 / 1023;
if (percent != shown) {
shown = percent;
Serial.print("level ");
Serial.print(percent);
Serial.print("%: red ");
Serial.print(red);
Serial.print(", green ");
Serial.println(green);
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Opens the serial port and sets the red and green pins as outputs. It prints a prompt telling you to turn the knob.
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
Serial.println("Level light: turn the knob");
}loop sketch.ino
Reads level from A0. Under 512, red is 255 and green is level / 2. Above that, green is 255 and red is (1023 - level) / 2. Both go out with analogWrite. It prints a line when the whole percent changes, then waits 20 ms.
void loop() {
int level = analogRead(A0); // 0 to 1023
int red = level < 512 ? 255 : (1023 - level) / 2;
int green = level < 512 ? level / 2 : 255;
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
// A line when the level moves by a whole percent.
int percent = (long)level * 100 / 1023;
if (percent != shown) {
shown = percent;
Serial.print("level ");
Serial.print(percent);
Serial.print("%: red ");
Serial.print(red);
Serial.print(", green ");
Serial.println(green);
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED only shows one color This project
Both R and G need their own 220 ohm resistor and pin: R to pin 9, G to pin 10. - Nothing lights This project
The LED's middle leg is the common cathode and goes to ground. - The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise. - No color shows or only one color works. Two-color LED
Find which leg is the shared one, usually the middle leg or the marked one. Drive the other two legs, each through its own resistor of about 220 ohms. - Orange is hard to get. Two-color LED
Mix the two colors with PWM on both pins at different levels. The brightness of each die differs, so tune the values by eye. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.