Sub topic 10
Dimming an LED with the Potentiometer
Assume you know Arduino setup() and loop(), can upload a sketch, and understand basic voltage, ground, and digital-output wiring. This chapter uses an Arduino Uno, where analogRead() returns values from 0 to 1023 and analogWrite() accepts values from 0 to 255 on PWM-capable pins.
From a potentiometer reading to LED brightness
A potentiometer connected to A0 produces a changing raw reading. To control an LED, the sketch must convert that reading into the smaller range expected by analogWrite().
The map() function performs this conversion:
int brightness = map(raw, 0, 1023, 0, 255);A raw value of 0 becomes 0, 1023 becomes 255, and values between them are scaled proportionally.
analogWrite() does not usually produce a continuously varying voltage. On an Uno, it uses pulse-width modulation (PWM), which switches the pin on and off quickly. The duty cycle is the percentage of each cycle that the signal is on. A higher duty cycle makes the LED appear brighter.
| `analogWrite()` value | Approximate duty cycle | LED result |
|---|---|---|
| 0 | 0% | Off |
| 128 | 50% | About half brightness |
| 255 | 100% | Full brightness |
Wiring the circuit
Use a potentiometer with three terminals:
- Connect one outer terminal to
5V. - Connect the other outer terminal to
GND. - Connect the center terminal, called the wiper, to
A0.
The wiper voltage changes as you turn the knob. Reverse the two outer connections if you want the knob to increase in the opposite direction.
For the LED:
- Connect Arduino pin 9 to a resistor, such as 220 ohms.
- Connect the resistor to the LED's anode, the longer leg.
- Connect the LED's cathode, the shorter leg, to
GND.
The resistor limits current and protects the LED and Arduino pin.
The complete sketch
The sketch reads the potentiometer and immediately uses the scaled value as the PWM output.
const int POT_PIN = A0;
const int LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int raw = analogRead(POT_PIN);
int brightness = map(raw, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
}The LED_PIN must support PWM. On an Uno, the PWM pins are marked with a tilde: 3, 5, 6, 9, 10, and 11. A regular digital pin can be switched on or off, but it cannot provide the same brightness control with analogWrite().
Checking the mapping
If the LED does not respond as expected, print both values:
const int POT_PIN = A0;
const int LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int raw = analogRead(POT_PIN);
int brightness = map(raw, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
Serial.print("raw=");
Serial.print(raw);
Serial.print(" brightness=");
Serial.println(brightness);
delay(100);
}Turn the potentiometer slowly and watch the Serial Monitor. The raw value should move through most of the 0 to 1023 range. The brightness value should move through 0 to 255.
Exercise
Why does analogWrite(LED_PIN, raw) produce an incorrect result on an Uno?
Show answer
raw can be as high as 1023, but analogWrite() expects a PWM value from 0 to 255. The raw reading must be mapped to that output range first.