Home / Learn Embedded C / Project: PWM LED Dimmer with a Potentiometer
Project: PWM LED Dimmer with a Potentiometer
Read an analog knob and use it to set LED brightness via PWM.
Breaking it down
Definition
An LED whose brightness is continuously controlled by a potentiometer, connecting ADC reading directly to PWM output in one practical pipeline.
Why you need this
Real projects are where embedded C patterns become habits instead of theory.
How it works
Work through the steps in order; do not skip the checklist.
Where you'll see this
- Portfolio project
- Interview preparation
- Real hardware practice
What you will build
An LED whose brightness is continuously controlled by a potentiometer, connecting ADC reading directly to PWM output in one practical pipeline.
| Level | Time needed |
|---|---|
| Beginner | 1-2 hours |
What you need (tools and setup)
| Tool | What it is | Why you need it | Cost |
|---|---|---|---|
| This course's live C sandbox | A small in-browser C interpreter with simulated GPIO/UART/ADC/PWM. | Every project in this course can be tried here first with zero setup, no board required. | free, built in |
| Code editor (VS Code or similar) | The program you write firmware source files in. | You need somewhere to write and organise the real .c/.h files for the project. | free |
| A real development board (Arduino/AVR, STM32 Nucleo, or ESP32 dev board) — optional | The actual microcontroller hardware. | Lets you move from the simulated board to a real one once the logic works here. | low cost, optional |
Step 1: see it working in the sandbox
Real code scales the ADC's full resolution range (e.g. 0-1023 for 10-bit) down to the PWM range (commonly 0-255) proportionally.
Step by step
- Wire a potentiometer to an ADC-capable pin, and an LED (through PWM) to another pin.
- Read the ADC and scale its range down to the PWM duty-cycle range.
- Write the scaled value to PWM_WRITE each loop iteration.
- Add averaging (from the Analog I/O group) if the raw ADC reading is noisy.
- Confirm brightness changes smoothly as the potentiometer is turned.
Checklist before you call it done
| Check | Why it matters | How to verify |
|---|---|---|
| Full potentiometer range maps to the full brightness range | Confirms the ADC-to-PWM scaling math is correct | Turn the potentiometer fully each direction and check extremes |
| Brightness changes smoothly, not jumpily | Confirms adequate ADC averaging/filtering | Watch for flicker or sudden jumps while turning slowly |
| No division-by-zero or out-of-range PWM values | Defensive coding around the scaling math | Check boundary values (ADC at 0 and at its maximum) |
This scaling is needed whenever the ADC's resolution (often 10-12 bit) differs from the PWM peripheral's duty resolution (often 8 bit).
Test what you just read
1. What earlier-course technique helps if the potentiometer reading makes the LED brightness flicker unexpectedly?
Flicker from a jittery raw ADC reading is exactly the problem the Analog I/O group's averaging/filtering lesson addresses.