PWM is a timer running in output-compare mode, not software pin-toggling. The timer counter ramps from 0 up to the auto-reload value ARR and repeats, which sets the PWM period (and thus frequency: f = f_clk/((PSC+1)(ARR+1))). A compare register CCR sets the switching point: the output is high while the counter is below CCR and low at or above it (PWM mode 1), so the duty cycle is CCR/(ARR+1). Once configured, the hardware regenerates the waveform every period autonomously, to change brightness or motor speed you just write a new CCR value, with zero CPU involvement and no jitter. This is exactly why PWM is done in hardware: software toggling would consume the CPU and produce timing jitter, whereas the timer gives glitch-free, precise edges. ARR sets frequency, CCR sets duty, that's the whole mental model.
Embedded Systems Fundamentals · Interview question
How is PWM generated using a timer?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
PWM
Fake an analog level with a digital pin: a fixed-frequency square wave whose duty cycle sets the average voltage, built on a timer and used for LEDs, motors, and servos.
More PWM questions
What is PWM and how does it produce an "analog" output from a digital pin?What's the tradeoff between PWM frequency and duty-cycle resolution?Why can't you drive a motor directly from a PWM-capable GPIO pin?How does PWM control a hobby servo, and how is that different from LED dimming?What is dead-time in a complementary PWM pair, and why does an H-bridge need it?Your PWM-controlled LED looks steady to the eye but flickers badly on camera video. What's going on and how do you fix it?
Browse all 472 interview questions