Built-in project · Arduino Uno R3

Knob on A0 dims the LED on pin 9, on an Arduino Uno

A potentiometer across 5 V with its wiper on A0 sets the brightness of an LED on pin 9; the serial monitor shows the reading and the brightness. The whole build, an Arduino Uno and 3 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $31. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A potentiometer as a voltage divider
  • Reading an analog input
  • Scaling a range with map
  • Dimming an LED with PWM
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A potentiometer has 5V on one end and ground on the other. Its middle leg, the wiper, slides between them, so the voltage on A0 changes from 0 V to 5 V as you turn the knob. The LED sits on pin 9 behind a 220 ohm resistor.

The sketch reads A0 as a number from 0 to 1023, scales it to 0 to 255, and sends that to pin 9 as PWM. A bigger reading means a brighter LED. It also prints the reading and the brightness, but only when the knob moves by more than 8.

Parts list

5 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.

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 pinConnects to
LED, red pin CArduino Uno R3 pin GND
Potentiometer pin 3Arduino Uno R3 pin GND
Resistor, 220 Ω pin 1Arduino Uno R3 pin 9
Potentiometer pin 1Arduino Uno R3 pin 5V
Potentiometer pin 2Arduino Uno R3 pin A0
LED, red pin AResistor, 220 Ω pin 2

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
// A knob dims an LED: the potentiometer on A0, the LED on pin 9.
//
//   potentiometer: one end -> 5V, the other end -> GND, the middle -> A0
//   pin 9 -> 220 ohm -> LED -> GND
//
// The middle leg (the wiper) slides between the two ends, so A0 sees
// anything from 0 V to 5 V and analogRead() gives 0 to 1023. analogWrite()
// takes 0 to 255, so map() scales one range onto the other.
//
// Drag the knob and watch the LED and the numbers.

const int KNOB = A0;
const int LED = 9;

int shown = -1;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: knob on A0, LED on pin 9");
}

void loop() {
  int reading = analogRead(KNOB);
  int brightness = map(reading, 0, 1023, 0, 255);
  analogWrite(LED, brightness);
  if (abs(reading - shown) > 8) {
    shown = reading;
    Serial.print("reading ");
    Serial.print(reading);
    Serial.print("  brightness ");
    Serial.println(brightness);
  }
  delay(20);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets LED (pin 9) as an output and starts serial at 9600. The knob pin A0 is an input by default, so it needs no setup.

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: knob on A0, LED on pin 9");
}

loop sketch.ino

Reads KNOB with analogRead to get 0 to 1023. map turns that into a brightness of 0 to 255, and analogWrite sets the LED. It prints only when the reading has moved more than 8 from the last shown value, so the monitor does not flood. A 20 ms delay ends the pass.

void loop() {
  int reading = analogRead(KNOB);
  int brightness = map(reading, 0, 1023, 0, 255);
  analogWrite(LED, brightness);
  if (abs(reading - shown) > 8) {
    shown = reading;
    Serial.print("reading ");
    Serial.print(reading);
    Serial.print("  brightness ");
    Serial.println(brightness);
  }
  delay(20);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED is always full on or off This project
    The wiper must go to A0. The two outer legs go to 5V and GND.
  • The LED does not light at all This project
    Check the LED direction, with the cathode toward ground, and that the resistor connects pin 9 to the anode.
  • 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.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • 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.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.