Dimmer on an Arduino Uno
A potentiometer on A0 sets the brightness of an LED on pin 9. Turn the knob while it runs. 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.
BeginnerRuns in your browser. Free, and no account needed.
Step by step, with the wiring and the common mistakes: Arduino Potentiometer: analogRead and a Dimmer
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Dimmer: a potentiometer sets how bright an LED is.
//
// A0 the pot's wiper (pin 2); its other two legs go to 5 V and GND
// pin 9 the LED, through 220 ohm to GND
//
// The smallest complete analogue circuit there is: read a voltage, write a
// brightness. Turn the knob while it runs.
//
// # Ten bits in, eight bits out
//
// analogRead gives 0 to 1023 and analogWrite takes 0 to 255, so the reading is
// shifted right by two rather than divided: 1023 >> 2 is 255, and a shift on an
// AVR is one instruction where a divide is a call.
//
// # Why the reading is smoothed
//
// A pot's wiper is a sliding contact and the ADC's last bit is noise, so a
// steady knob still reads a count or two either way. Left alone, that makes the
// LED flicker at the bottom of its range where the eye is most sensitive. The
// filter here is the cheapest one that works: three quarters of the old value
// plus a quarter of the new, once every five milliseconds. It settles in about
// thirty milliseconds, which is faster than a hand can turn a knob.
//
// The serial line says the percentage, but only when it has moved by a percent,
// so turning the knob prints a sentence rather than a waterfall.
const int KNOB = A0;
const int LAMP = 9;
int smoothed = 0;
int said = -1;
void setup() {
pinMode(LAMP, OUTPUT);
analogWrite(LAMP, 0);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: dimmer");
Serial.println("turn the knob to set the brightness");
smoothed = analogRead(KNOB);
}
void loop() {
int raw = analogRead(KNOB);
// A quarter of the new reading, three quarters of what we had.
smoothed += (raw - smoothed) / 4;
int level = smoothed >> 2;
if (level > 255) {
level = 255;
}
analogWrite(LAMP, level);
int percent = (int)(((long)smoothed * 100L) / 1023L);
if (percent != said) {
said = percent;
Serial.print("knob ");
Serial.print(percent);
Serial.print("% -> pwm ");
Serial.println(level);
}
delay(5);
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Potentiometer
- 1 × Resistor, 220 Ω
- 1 × LED, yellow
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.
- Arduino Uno R3 pin 9; Resistor, 220 Ω pin 1
- Arduino Uno R3 pin 5V; Potentiometer pin 3
- Ground: Arduino Uno R3 pin GND; Potentiometer pin 1; LED, yellow pin C
- Arduino Uno R3 pin A0; Potentiometer pin 2
- Resistor, 220 Ω pin 2; LED, yellow 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.