PWM: controlling LED brightness on the UNO
Use duty cycle and analogWrite() to dim an LED without changing its supply voltage.
What you will learn: connect a current-limited LED and compare different PWM duty cycles on a classic Arduino UNO R3. Use an UNO, USB cable, breadboard, jumper wires, one LED and a 220 ohm resistor.
Wire with USB disconnected: D9 to the resistor, resistor to LED anode, and LED cathode to GND. Put the legs on separate breadboard rows. A resistor is essential even when software uses a low brightness value: the LED still experiences the full HIGH pulse voltage.
PWM rapidly switches the output between LOW and HIGH. Duty cycle is the fraction of time the signal stays HIGH. On the UNO R3, analogWrite() on a PWM pin accepts values from 0 to 255. It does not create an adjustable steady voltage. UNO R3 PWM pins are D3, D5, D6, D9, D10 and D11.
Try this sketch: void setup() { pinMode(9, OUTPUT); } void loop() { analogWrite(9, 32); delay(1000); analogWrite(9, 128); delay(1000); analogWrite(9, 255); delay(1000); }. The LED cycles through three duty cycles; repeat the observation in steady ambient light.
Challenge: replace the three fixed levels with the Arduino Breathing LED project. Change the update interval and compare the fade speed. A linear duty-cycle ramp does not necessarily look like a linear brightness ramp to your eye.
If the LED does not dim, verify that the wire goes to a PWM pin on the correct board. Do not reuse this UNO pin map blindly on an ESP32. Keep the exercise to a small LED; motors need a driver and a separate power plan.
Reference: https://docs.arduino.cc/language-reference/en/functions/analog-io/analogWrite/