Built-in project · Arduino Uno R3

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.

Beginner Runs in your browser. Free, and no account needed.

Level light · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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
// 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);
}

Parts list

6 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

7 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.

  • Ground: Arduino Uno R3 pin GND; Potentiometer pin 1; Two-color LED, KY-011 pin C
  • Arduino Uno R3 pin 10; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 9; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 5V; Potentiometer pin 3
  • Arduino Uno R3 pin A0; Potentiometer pin 2
  • Two-color LED, KY-011 pin R; Resistor, 220 Ω (2) pin 2
  • Two-color LED, KY-011 pin G; Resistor, 220 Ω (1) 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.

Free forever. Every board and part. Saves your projects. Works on Chromebooks.