YKR038 COURSE · MODULE 2 · LESSON 08
Control LED Brightness with ESP32 PWM
Use ESP32 PWM to fade an LED smoothly and understand duty cycle.
Follow the numbered steps in order. Do not connect USB power until the wiring step tells you to. Compare every pin label before continuing.
LESSON GOAL
What you will build and learn
In this lesson, you will learn PWM frequency, resolution, and duty cycle. By the end, the LED fades smoothly from off to full brightness and back.
- Recognize the purpose of each required part.
- Make the connections in a safe, repeatable order.
- Upload or run the lesson procedure and verify the expected result.
- Use observable evidence to fix common problems.
A close, labeled photograph of this exact experiment will be added here. Until then, use the written pin-by-pin connection list below.
STEP-BY-STEP GUIDE
Build the experiment
Understand the target result
Use ESP32 PWM to fade an LED smoothly and understand duty cycle. The expected result is: The LED fades smoothly from off to full brightness and back.
Prepare the board and parts
Disconnect the ESP32 from USB. Put the ESP32, breadboard, jumper wires, and the listed lesson parts in front of you. Straighten bent pins gently and never force a module into the breadboard.
Make each connection with power off
- Connect GPIO 23 to a 220Ω resistor.
- Connect the resistor to the LED anode.
- Connect the LED cathode to GND.
Power check: confirm that no 5V signal goes directly into an ESP32 GPIO and that 3.3V is not connected to GND.
Install any library and upload the code
No additional Arduino library is required for this starter sketch. Select your ESP32 board and serial port before clicking Upload.
const int LED_PIN = 23;
void setup() {
ledcAttach(LED_PIN, 5000, 8);
}
void loop() {
for (int duty = 0; duty <= 255; duty++) {
ledcWrite(LED_PIN, duty);
delay(8);
}
for (int duty = 255; duty >= 0; duty--) {
ledcWrite(LED_PIN, duty);
delay(8);
}
}Power the circuit and observe
Connect USB power only after the wiring check. Open Serial Monitor at 115200 baud when the sketch prints data. Watch the circuit for at least 20 seconds and compare it with this expected behavior: The LED fades smoothly from off to full brightness and back.
Change one value and test again
Make one small change—such as a delay, threshold, brightness, or displayed message—then upload again. This confirms that you understand which part of the program controls the result.
WHY IT WORKS
The key idea
This experiment demonstrates PWM frequency, resolution, and duty cycle. The ESP32 repeatedly reads or updates the connected hardware, applies the program logic, and produces the observable result. Changing one input or one code value helps you see that relationship directly.
TROUBLESHOOTING
If the experiment does not work
Nothing powers on
Disconnect USB. Check the USB cable, 3.3V/5V selection, GND rail, and breadboard row placement.
Upload fails
Confirm the board and port. Close other serial programs, reconnect USB, and hold BOOT only if your board requires it.
Result is unstable
Shorten jumper wires, confirm a shared ground, and verify that input pins are not floating.
Lesson-specific check
Start at low brightness and confirm the resistor is in series.
VIDEO WALKTHROUGH
Hands-on lesson video reserved
The final video will show the real YKR038 parts, every wire connection, the upload process, and the expected result.