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.

Bottom lineRead an analog knob and use it to set LED brightness via PWM.

Breaking it down

ADC-to-PWM pipelineRead ADCScale to PWM rangeWrite PWM dutyLED brightnesschanges accordingly
ADC-to-PWM pipeline

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.

LevelTime needed
Beginner1-2 hours

What you need (tools and setup)

ToolWhat it isWhy you need itCost
This course's live C sandboxA 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) — optionalThe 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

Mapping an ADC reading to a PWM duty cycleC: edit and run

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

  1. Wire a potentiometer to an ADC-capable pin, and an LED (through PWM) to another pin.
  2. Read the ADC and scale its range down to the PWM duty-cycle range.
  3. Write the scaled value to PWM_WRITE each loop iteration.
  4. Add averaging (from the Analog I/O group) if the raw ADC reading is noisy.
  5. Confirm brightness changes smoothly as the potentiometer is turned.

Checklist before you call it done

CheckWhy it mattersHow to verify
Full potentiometer range maps to the full brightness rangeConfirms the ADC-to-PWM scaling math is correctTurn the potentiometer fully each direction and check extremes
Brightness changes smoothly, not jumpilyConfirms adequate ADC averaging/filteringWatch for flicker or sudden jumps while turning slowly
No division-by-zero or out-of-range PWM valuesDefensive coding around the scaling mathCheck boundary values (ADC at 0 and at its maximum)
Tip:Commit the first working version and every later improvement as separate commits, so the diff itself becomes a teaching example.
Mapping a wider ADC range onto the 0-255 PWM rangeC: edit and run

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?