Sub topic 6
Setting Brightness with `analogWrite()`
Assume you know the basic Arduino sketch structure, pinMode(), digitalWrite(), breadboard wiring, and introductory Ohm’s law. This chapter uses an Arduino Uno and an LED connected to a PWM-capable pin.
`analogWrite()` is PWM control
An Arduino Uno does not create a steady voltage such as 1.7 V on a digital output. Instead, analogWrite() uses pulse-width modulation (PWM). PWM switches the pin between 0 V and 5 V very quickly.
The function accepts a value from 0 to 255. This value sets the duty cycle, which is the percentage of each repeating period that the signal stays HIGH.
| `analogWrite()` value | Approximate duty cycle | Result for an LED |
|---|---|---|
| 0 | 0% | Off |
| 64 | 25% | Dim |
| 128 | 50% | Medium brightness |
| 255 | 100% | Fully on |
For example, a value of 64 keeps the pin HIGH for about one quarter of each PWM period and LOW for the remaining three quarters. The LED receives full-voltage pulses, but the average power is lower.
Select a PWM-capable pin
On a standard Arduino Uno, the PWM pins are marked with a tilde symbol:
- 3
- 5
- 6
- 9
- 10
- 11
The LED circuit still needs a current-limiting resistor. Connect the LED anode, the longer leg, through a resistor to pin 9. Connect the cathode, the shorter leg, to GND. A resistor between 220 Ω and 1 kΩ is common for a small indicator LED.
The resistor limits current. Without it, the LED or the Arduino output pin can be damaged.
A basic brightness sketch
This sketch sets the LED to half duty cycle:
const uint8_t LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
analogWrite(LED_PIN, 128);
}analogWrite() is different from analogRead(). The analogRead() function measures an input voltage with the analog-to-digital converter. The analogWrite() function produces PWM on a supported digital output. On the Uno, analogWrite() does not provide a true analog voltage.
The call can be placed in setup() when the brightness never changes. Keeping it in loop() is also valid, although it repeatedly writes the same setting.
Mapping a value to brightness
A sensor or control often produces a different range. A potentiometer connected to an Uno analog input gives a reading from 0 to 1023, while analogWrite() expects 0 to 255. Shift the reading right by two bits, or use map().
const uint8_t LED_PIN = 9;
const uint8_t POT_PIN = A0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int reading = analogRead(POT_PIN); // 0..1023
int brightness = map(reading, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
}map() changes the numerical range, but it does not smooth noisy readings. If the LED flickers when the potentiometer is still, average several readings or add a small dead band.
Common errors
- Using pin 8 or another non-PWM pin and expecting gradual brightness.
- Reversing the LED, which prevents it from conducting.
- Omitting the resistor and overloading the LED or output pin.
- Passing a value below 0 or above 255. Store brightness in a suitable integer range and constrain external input when needed.
- Assuming a value of 128 looks exactly half as bright. The eye’s response is nonlinear, so later chapters will address gamma correction.
Exercise
An LED is connected to Uno pin 9. What does analogWrite(LED_PIN, 64) do, and why is a resistor still required?
Show answer
It produces about a 25% PWM duty cycle, reducing the LED's average power and perceived brightness. The resistor limits current through the LED and protects both the LED and the Arduino pin.