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 Runs 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.
// 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);
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Resistor, 220 Ω
- 1 × LED, red
- 1 × Potentiometer
How it is wired
5 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; LED, red pin C; Potentiometer pin 3
- Arduino Uno R3 pin 9; Resistor, 220 Ω pin 1
- Arduino Uno R3 pin 5V; Potentiometer pin 1
- Arduino Uno R3 pin A0; Potentiometer pin 2
- Resistor, 220 Ω pin 2; LED, red pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.