PWM: how a microcontroller dims an LED
A GPIO pin can be only fully ON (3.3 V) or fully OFF (0 V). Pulse-width modulation (PWM) switches the pin very quickly. PWM changes the fraction of time that the pin is on. This fraction is the duty cycle. At more than about 60–100 Hz, your eye sees the average of the flashes, so the LED looks steadily dimmer or brighter. Start the sound to hear the same signal.
At less than about 60 Hz, you can see the LED flicker.
Harmonic spectrum: how PWM changes the timbre
A square wave at exactly 50% duty has only odd harmonics. These harmonics give a hollow sound. When the duty moves away from 50%, even harmonics occur and the sound becomes thinner and more nasal. At 0% or 100%, there is only a constant (DC) voltage, so you hear no sound.
This code uses the LEDC (LED Control) peripheral API from Arduino-ESP32 core 3.x.
Average = duty × Vhigh
At 25% duty, the average voltage of a 3.3 V pin is 0.825 V. The LED is fully on for ¼ of each period and off for the remaining time, so the LED gives about ¼ of its full light.
Frequency vs. flicker
Try 1 Hz or 5 Hz: you can see the LED blink. At more than about 60–100 Hz, the eye mixes the flashes. LED dimmers often operate at 1–5 kHz, so you cannot see the flicker. Some LED dimmers operate at more than 20 kHz, so you also cannot hear the switching noise.
Resolution vs. max frequency
The ESP32 LEDC timer counts from an 80 MHz clock. With n bits, the counter needs 2n ticks for each period, so fmax ≈ 80 MHz / 2n. More brightness steps give a lower maximum frequency. There is also a minimum frequency. The slowest clock (1 MHz) and the largest divider (≈1024) give fmin ≈ 1 MHz / (1024 × 2n), so very low frequencies need more bits (1 Hz needs a minimum of 10 bits).
PWM as a low-cost DAC
If you send PWM through a resistor–capacitor (RC) low-pass filter, you get a smooth voltage. This voltage is equal to the average. Many microcontrollers use this method to make an output that is almost analog. To see the ripple, select “Show RC-filtered output”.
Dim more than one LED: PWM + a MOSFET
A GPIO pin can safely supply about 20 mA. This current is sufficient for one small LED. A 12 V LED strip, a high-power LED or a motor needs much more current and often a higher voltage. For these loads, connect the pin to the gate of a logic-level N-channel MOSFET. The MOSFET switches the large current on and off with exactly the same PWM pattern.
- Use a logic-level MOSFET with a low RDS(on) at a 3.3 V gate drive. The AO3400 is specified at 2.5 V. The IRLB8721 and IRLZ44N operate well at 3.3 V for loads of a few amps. The usual IRF520 “modules” are rated for a 10 V gate drive. At 3.3 V, they come on only partially.
- Put ~100 Ω between the GPIO pin and the gate. Put 10 kΩ from the gate to GND. Then the load stays off during boot.
- Connect the GND of the strip supply to the ESP32 GND (common ground).
- For LEDs, keep the PWM frequency at 1–20 kHz. If the load is a motor, solenoid or other coil, use a flyback diode.