Bird
Raised Fist0
Embedded Cprogramming~20 mins

Duty cycle control for motor/LED in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PWM Duty Cycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PWM duty cycle calculation?
Given the following code snippet that calculates the PWM duty cycle for a motor, what is the value of duty_cycle after execution?
Embedded C
int max_pwm = 255;
int speed_percent = 60;
int duty_cycle = (speed_percent * max_pwm) / 100;
A255
B153
C60
D100
Attempts:
2 left
💡 Hint
Remember that duty cycle is a fraction of max PWM value based on percentage.
❓ Predict Output
intermediate
2:00remaining
What is the LED brightness value after this PWM update?
Consider this code controlling LED brightness with PWM. What is the value of led_brightness after running this code?
Embedded C
int max_pwm = 1023;
int brightness_percent = 25;
int led_brightness = (brightness_percent * max_pwm) / 100;
A256
B255
C1023
D25
Attempts:
2 left
💡 Hint
Calculate the percentage of max PWM value.
❓ Predict Output
advanced
2:00remaining
What error does this PWM duty cycle code produce?
What error will occur when compiling or running this code snippet?
Embedded C
int max_pwm = 255;
float speed_percent = 75.5;
int duty_cycle = (speed_percent * max_pwm) / 100;
ATypeError: cannot assign float to int without cast
BNo error, duty_cycle = 192
CSyntaxError: invalid expression
DWarning: implicit conversion from float to int, duty_cycle = 192
Attempts:
2 left
💡 Hint
Check how float to int conversion works in C.
🧠 Conceptual
advanced
2:00remaining
Which statement best describes PWM duty cycle effect on motor speed?
Select the statement that correctly explains how PWM duty cycle affects motor speed.
AHigher duty cycle means the motor receives power for a longer time, increasing speed.
BPWM frequency controls speed, duty cycle controls torque.
CDuty cycle does not affect motor speed, only voltage does.
DLower duty cycle increases motor speed by reducing power interruptions.
Attempts:
2 left
💡 Hint
Think about how power delivery time affects motor rotation.
❓ Predict Output
expert
3:00remaining
What is the final value of duty_cycle after this code runs?
Analyze this embedded C code that adjusts PWM duty cycle dynamically. What is the final value of duty_cycle?
Embedded C
int max_pwm = 255;
int duty_cycle = 0;
for (int i = 0; i < 5; i++) {
    duty_cycle += (i * max_pwm) / 10;
}
if (duty_cycle > max_pwm) {
    duty_cycle = max_pwm;
}
A0
B127
C254
D255
Attempts:
2 left
💡 Hint
Calculate the sum inside the loop and check the cap condition.

Practice

(1/5)
1. What does the duty cycle control in a motor or LED?
easy
A. The percentage of time the device is ON during each cycle
B. The voltage supplied to the device
C. The frequency of the power supply
D. The temperature of the device

Solution

  1. Step 1: Understand duty cycle meaning

    The duty cycle is the fraction of one period in which a signal or system is active.
  2. 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.
  3. Final Answer:

    The percentage of time the device is ON during each cycle -> Option A
  4. Quick Check:

    Duty cycle = ON time percentage [OK]
Hint: Duty cycle means ON time percentage per cycle [OK]
Common Mistakes:
  • Confusing duty cycle with voltage
  • Thinking it controls frequency
  • Assuming it controls temperature
2. Which of the following is the correct way to set a 50% duty cycle using a timer compare register in embedded C?
easy
A. OCR0 = 0; // Full OFF
B. OCR0 = 255; // Full ON
C. OCR0 = 128; // Assuming 8-bit timer with max 255
D. OCR0 = 64; // 25% duty cycle

Solution

  1. Step 1: Understand timer resolution

    An 8-bit timer counts from 0 to 255, so max value is 255.
  2. Step 2: Calculate 50% duty cycle value

    50% of 255 is approximately 128, so setting OCR0 to 128 sets 50% duty cycle.
  3. Final Answer:

    OCR0 = 128; // Assuming 8-bit timer with max 255 -> Option C
  4. Quick Check:

    50% of 255 = 128 [OK]
Hint: Half of max timer value sets 50% duty cycle [OK]
Common Mistakes:
  • Using max value for 50% duty cycle
  • Using zero for 50% duty cycle
  • Confusing 25% with 50%
3. Given the code snippet below, what will be the duty cycle percentage set for the LED?
unsigned char duty = 64;
unsigned char max = 255;
float dutyCycle = (duty / (float)max) * 100;
medium
A. 25%
B. 50%
C. 75%
D. 64%

Solution

  1. Step 1: Calculate duty/max ratio

    Divide duty (64) by max (255): 64 / 255 ≈ 0.25098.
  2. Step 2: Convert ratio to percentage

    Multiply by 100: 0.25098 * 100 ≈ 25.1%.
  3. Final Answer:

    25% -> Option A
  4. Quick Check:

    (64/255)*100 ≈ 25% [OK]
Hint: Divide duty by max, multiply by 100 for percentage [OK]
Common Mistakes:
  • Using integer division without casting
  • Confusing duty value with percentage
  • Rounding incorrectly
4. Identify the error in the following code that attempts to set a 75% duty cycle on an 8-bit timer:
unsigned char duty = 75;
unsigned char max = 255;
OCR0 = duty / max * 255;
medium
A. OCR0 register cannot be assigned directly
B. Integer division causes duty/max to be zero
C. max value should be 100, not 255
D. Duty cycle calculation is correct

Solution

  1. Step 1: Analyze division operation

    Both duty and max are unsigned char, so duty/max does integer division: 75/255 = 0 (integer).
  2. Step 2: Effect on OCR0 assignment

    Multiplying 0 by 255 results in 0, so OCR0 is set to 0, not 75% duty cycle.
  3. Final Answer:

    Integer division causes duty/max to be zero -> Option B
  4. Quick Check:

    Integer division truncates to zero [OK]
Hint: Cast to float before division to avoid zero result [OK]
Common Mistakes:
  • Ignoring integer division rules
  • Assuming OCR0 cannot be assigned
  • Using wrong max value
5. You want to smoothly increase the brightness of an LED from 0% to 100% duty cycle using PWM on an 8-bit timer. Which approach is best to implement this in embedded C?
hard
A. Use a fixed OCR0 value of 128 for half brightness
B. Set OCR0 to 255 once and leave it
C. Toggle the LED pin ON and OFF manually without PWM
D. Use a loop to increment OCR0 from 0 to 255 with a delay between steps

Solution

  1. Step 1: Understand smooth brightness control

    Smooth brightness change requires gradually changing the duty cycle from 0 to max.
  2. Step 2: Implement gradual PWM change

    Increment OCR0 from 0 to 255 in a loop with delays to create smooth brightness ramp.
  3. Final Answer:

    Use a loop to increment OCR0 from 0 to 255 with a delay between steps -> Option D
  4. Quick Check:

    Gradual OCR0 change = smooth brightness [OK]
Hint: Increment OCR0 gradually with delay for smooth fade [OK]
Common Mistakes:
  • Setting OCR0 once for full brightness only
  • Using fixed mid value for all brightness
  • Not using PWM for brightness control
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard