Duty cycle control helps change how strong or bright a motor or LED works by turning it on and off very fast.
Duty cycle control for motor/LED in Embedded C
void setDutyCycle(int dutyCyclePercent) { // dutyCyclePercent is from 0 to 100 int compareValue = (MAX_TIMER_VALUE * dutyCyclePercent) / 100; TIMER_COMPARE_REGISTER = compareValue; }
The duty cycle is usually given as a percentage from 0 (off) to 100 (fully on).
The timer compare register sets how long the output stays on in each cycle.
setDutyCycle(0); // LED or motor off
setDutyCycle(50); // Half brightness or speed
setDutyCycle(100); // Full brightness or speed
This program simulates setting the duty cycle for a motor or LED by calculating the timer compare register value and printing it.
#include <stdio.h> #define MAX_TIMER_VALUE 1000 int TIMER_COMPARE_REGISTER = 0; void setDutyCycle(int dutyCyclePercent) { if (dutyCyclePercent < 0) dutyCyclePercent = 0; if (dutyCyclePercent > 100) dutyCyclePercent = 100; int compareValue = (MAX_TIMER_VALUE * dutyCyclePercent) / 100; TIMER_COMPARE_REGISTER = compareValue; printf("Duty cycle set to %d%%, compare register = %d\n", dutyCyclePercent, TIMER_COMPARE_REGISTER); } int main() { setDutyCycle(0); // Off setDutyCycle(25); // 25% on setDutyCycle(50); // 50% on setDutyCycle(75); // 75% on setDutyCycle(100); // Fully on return 0; }
Duty cycle control uses a technique called PWM (Pulse Width Modulation).
Changing the duty cycle changes how long the device is powered in each cycle, affecting brightness or speed.
Always check your hardware limits to avoid damage when setting duty cycles.
Duty cycle controls how long a device is on in each cycle to adjust brightness or speed.
It is expressed as a percentage from 0 to 100.
Use a timer compare register to set the duty cycle in embedded systems.
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
