মূল বিষয়বস্তুতে যান
Learn/Analog input & PWM/PWM: fading an LED
Analog input & PWM · Lesson
10 min read

PWM: fading an LED

How quickly switching a pin on and off looks like a dimmer.

What You Will Learn

  • Explain duty cycle
  • Use analogWrite(pin, 0–255)
  • Know which pins support PWM

Digital pins can only be 0 V or 5 V. PWM (pulse-width modulation) switches the pin hundreds of times per second. The fraction of time it's on — the duty cycle — sets the average power. Your eye sees a dimmer LED.

analogWrite valueDuty cycleLED
00 %off
6425 %dim
12850 %medium
255100 %full
Fade with PWMarduino-uno
Open in Lab
UNOATmega328P: The 8-bit AVR microcontroller that runs your sketch (16 MHz, 32 KB flash, 2 KB RAM).ATmega328PUSB-B: USB connection for uploading sketches and for the Serial Monitor. Also powers the board (5 V).DC jack: Barrel jack for 7–12 V from a battery pack or adapter (centre positive).ATmega16U2: Converts USB to serial for uploads and Serial.print().RESET: Restarts your sketch from the beginning (setup() runs again).L (D13): Built-in LED connected to pin 13 (LED_BUILTIN).ON: Power indicator.5 V regulator: Turns 7–12 V from VIN/jack into a steady 5 V.16 MHz: Clock source: the microcontroller runs 16 million cycles per second.ICSP: In-circuit programming header (SPI). Used to burn a bootloader; not needed for normal uploads.220 Ω+
Note: The LED breathes. Try a different step size, or move it to pin 7 (no PWM) and read the warning.
On the Uno, PWM runs at about 490 Hz (980 Hz on pins 5 and 6). The ESP32 uses LEDC with a frequency you choose.

Check Your Understanding

Test what you learned in this lesson

1analogWrite(9, 191) gives about what duty cycle?