What is PWM, how is it generated on a microcontroller, and what is it used for?
What the interviewer wants to hear
- A square wave with fixed frequency and variable duty cycle
- Average voltage or power is proportional to duty cycle
- Generated by a timer in compare mode with a period and compare value
- Used for LED dimming, motor speed, servos and simple DACs
- Frequency choice matters: flicker, audible noise, switching loss
Model answer
PWM, or pulse width modulation, is a square wave at a fixed frequency where we change how long it stays high in each period, the duty cycle. The average power delivered is proportional to the duty cycle, so 25 percent on gives about a quarter of full power. On an MCU a hardware timer generates it: the auto-reload value sets the period, and a compare register sets when the output toggles, so the CPU does no work per cycle. Common uses are LED dimming, motor speed control through an H-bridge, hobby servos, which read the pulse width, and simple DACs with an RC filter. Choosing the frequency matters: LEDs need it high enough to avoid visible flicker, motors usually above 20 kHz to avoid audible whine, but higher frequencies raise switching losses in the drivers.
Can you answer it under time pressure?
Take a timed easy round and get scored on every answer.
More easy Embedded Systems Engineer questions
- What is the difference between a microcontroller and a microprocessor?
- What is an interrupt, and how is it different from polling?
- Compare UART, SPI and I2C. When would you use each?
- Explain volatile and const in embedded C. Can a variable be both?
- When would you use bare-metal firmware versus an RTOS?
- What GPIO modes does a typical microcontroller pin support, and when would you use each?
- What is a bootloader in an embedded device, and why would a product need one?
- What is endianness, and how do you deal with it when exchanging data between devices?
- How do you approach reading a datasheet for a new sensor or microcontroller?