Sub topic 8
Controlling Brightness with a Potentiometer
This chapter assumes that you know the Arduino setup() and loop() structure, can use pinMode() and digitalWrite(), and understand basic breadboard wiring and Ohm’s law. You will use a potentiometer to set the brightness of an LED connected to a PWM-capable pin.
The signal path
A potentiometer is a variable voltage divider. Its center terminal, called the wiper, produces a voltage between 0 V and the board’s supply voltage as you turn the shaft.
The Arduino Uno’s analog-to-digital converter (ADC) measures that voltage as a 10-bit number:
- 0 represents approximately 0 V.
- 1023 represents approximately 5 V.
- Values between them represent intermediate voltages.
The analogWrite() function uses an 8-bit PWM value:
- 0 means 0% duty cycle, so the LED is off.
- 255 means 100% duty cycle, so the LED is fully on.
- Values between 0 and 255 set intermediate duty cycles.
The program must therefore scale the ADC’s range of 0–1023 into the PWM range of 0–255. Dividing by four, or shifting right by two bits, performs this conversion.
Wiring the circuit
Connect the potentiometer as follows:
| Potentiometer terminal | Connection |
|---|---|
| One outer terminal | 5 V |
| Center terminal, the wiper | A0 |
| Other outer terminal | GND |
Connect the LED separately:
- Connect Arduino pin 9 to a current-limiting resistor, such as 220 Ω or 330 Ω.
- Connect the resistor to the LED’s anode, the longer leg on many through-hole LEDs.
- Connect the LED’s cathode to GND.
The two outer potentiometer terminals can be reversed. Reversing them only changes whether clockwise turns increase or decrease brightness. Do not omit the LED resistor. Without it, excessive current can damage the LED or the Arduino pin.
Reading and scaling the potentiometer
The following sketch reads A0 and uses the result to control the LED.
const uint8_t LED_PIN = 9; // PWM-capable pin on an Arduino Uno
const uint8_t POT_PIN = A0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int reading = analogRead(POT_PIN); // 0..1023
uint8_t brightness = reading >> 2; // 0..255
analogWrite(LED_PIN, brightness);
}The right-shift operator, >>, moves the binary value two places to the right. For these ranges, it is equivalent to integer division by four:
uint8_t brightness = reading / 4;For example, an ADC reading of 512 becomes a PWM value of 128. That produces a duty cycle of about 50%. The LED receives full-voltage pulses, not a steady 2.5 V output. Its apparent brightness comes from the rapid switching and the eye’s response to it.
Common errors and improvements
A potentiometer does not produce a current suitable for driving an LED directly. It only provides a control voltage for the Arduino input. The LED must still be connected to a digital output through a resistor.
Another common error is using a non-PWM pin. On an Uno, pins 3, 5, 6, 9, 10, and 11 support PWM. analogWrite() on another digital pin does not provide the same brightness control.
The knob may produce small brightness changes even when you do not turn it. This is ADC noise or mechanical noise from the potentiometer. A simple improvement is to average several readings:
const uint8_t LED_PIN = 9;
const uint8_t POT_PIN = A0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
long total = 0;
for (uint8_t i = 0; i < 8; i++) {
total += analogRead(POT_PIN);
}
int averageReading = total / 8;
analogWrite(LED_PIN, averageReading >> 2);
}Averaging makes the output steadier, but it also makes the response slightly less immediate.
Exercise
If analogRead(A0) returns 768, what PWM value should the sketch write, and what approximate duty cycle does that represent?
Show answer
The PWM value is 768 >> 2, or 192. Since 192 is 192/255 of the full range, the duty cycle is approximately 75%.