What if you could control a motor's speed or an LED's brightness perfectly without lifting a finger?
Why Duty cycle control for motor/LED in Embedded C? - Purpose & Use Cases
Imagine you want to make a motor spin slower or an LED glow dimmer by turning it on and off manually with a switch. You try flipping the switch on and off quickly by hand to control the speed or brightness.
This manual way is slow, tiring, and very inaccurate. Your hand can't flip the switch fast or steady enough, so the motor speed or LED brightness keeps jumping and feels uneven. It's also easy to make mistakes or get tired.
Duty cycle control uses a smart timer inside the microcontroller to turn the motor or LED on and off very fast and precisely. By changing how long it stays on versus off (the duty cycle), you can smoothly control speed or brightness without any manual effort.
while(1) { turn_on(); delay(100); turn_off(); delay(100); }
set_pwm_duty_cycle(50); // 50% on, 50% off start_pwm();
This lets you control motors and LEDs smoothly and efficiently, making devices quieter, longer-lasting, and more responsive.
Think of a fan that adjusts its speed quietly as you turn a knob, or a lamp that dims gently instead of just on or off.
Manual switching is slow and inaccurate for controlling speed or brightness.
Duty cycle control uses fast, precise on/off timing to adjust power smoothly.
This technique improves device performance and user experience.
Practice
Solution
Step 1: Understand duty cycle meaning
The duty cycle is the fraction of one period in which a signal or system is active.Step 2: Relate to motor/LED control
For motors or LEDs, it controls how long the device is ON in each cycle, affecting speed or brightness.Final Answer:
The percentage of time the device is ON during each cycle -> Option AQuick Check:
Duty cycle = ON time percentage [OK]
- Confusing duty cycle with voltage
- Thinking it controls frequency
- Assuming it controls temperature
Solution
Step 1: Understand timer resolution
An 8-bit timer counts from 0 to 255, so max value is 255.Step 2: Calculate 50% duty cycle value
50% of 255 is approximately 128, so setting OCR0 to 128 sets 50% duty cycle.Final Answer:
OCR0 = 128; // Assuming 8-bit timer with max 255 -> Option CQuick Check:
50% of 255 = 128 [OK]
- Using max value for 50% duty cycle
- Using zero for 50% duty cycle
- Confusing 25% with 50%
unsigned char duty = 64; unsigned char max = 255; float dutyCycle = (duty / (float)max) * 100;
Solution
Step 1: Calculate duty/max ratio
Divide duty (64) by max (255): 64 / 255 ≈ 0.25098.Step 2: Convert ratio to percentage
Multiply by 100: 0.25098 * 100 ≈ 25.1%.Final Answer:
25% -> Option AQuick Check:
(64/255)*100 ≈ 25% [OK]
- Using integer division without casting
- Confusing duty value with percentage
- Rounding incorrectly
unsigned char duty = 75; unsigned char max = 255; OCR0 = duty / max * 255;
Solution
Step 1: Analyze division operation
Both duty and max are unsigned char, so duty/max does integer division: 75/255 = 0 (integer).Step 2: Effect on OCR0 assignment
Multiplying 0 by 255 results in 0, so OCR0 is set to 0, not 75% duty cycle.Final Answer:
Integer division causes duty/max to be zero -> Option BQuick Check:
Integer division truncates to zero [OK]
- Ignoring integer division rules
- Assuming OCR0 cannot be assigned
- Using wrong max value
Solution
Step 1: Understand smooth brightness control
Smooth brightness change requires gradually changing the duty cycle from 0 to max.Step 2: Implement gradual PWM change
Increment OCR0 from 0 to 255 in a loop with delays to create smooth brightness ramp.Final Answer:
Use a loop to increment OCR0 from 0 to 255 with a delay between steps -> Option DQuick Check:
Gradual OCR0 change = smooth brightness [OK]
- Setting OCR0 once for full brightness only
- Using fixed mid value for all brightness
- Not using PWM for brightness control
Ready to prove it?
Pass the test and this pattern moves unknown
