Why does the loop stop when dutyCycle is 120 even though we set it to 100 in the last step?
The loop condition checks if dutyCycle <= 100 before running the loop body. When dutyCycle becomes 120 after adding 20, the condition fails and the loop stops, as shown in execution_table step 7.
What does setPWM(dutyCycle) do in this code?
setPWM(dutyCycle) sets the PWM signal's duty cycle, controlling how long the signal is ON versus OFF, which changes the brightness or motor speed. This is the key action in each loop step (see execution_table Action column).
Why do we add 20 to dutyCycle each time?
Adding 20 increases the duty cycle in steps, making the LED or motor gradually brighter or faster. This step size controls how smooth or fast the change happens, as seen in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the dutyCycle value at step 4?
A60
B40
C80
D100
💡 Hint
Check the dutyCycle column in execution_table row 4.
At which step does the loop condition become false and stop the loop?
AStep 5
BStep 6
CStep 7
DStep 3
💡 Hint
Look at the Condition and Action columns in execution_table row 7.
If we change the increment from 20 to 10, how would the variable_tracker change?
AdutyCycle would stay the same
BdutyCycle would increase by 10 each step instead of 20
CdutyCycle would decrease each step
DdutyCycle would jump directly to 100
💡 Hint
Check how dutyCycle changes in variable_tracker and think about changing the increment.
Concept Snapshot
Duty cycle controls ON time in PWM signal.
Higher duty cycle means brighter LED or faster motor.
Use a loop to change duty cycle gradually.
setPWM(dutyCycle) applies the PWM signal.
Loop stops when dutyCycle exceeds max value.
Full Transcript
This example shows how to control a motor or LED brightness using PWM duty cycle in embedded C. The program starts with dutyCycle at 0 and increases it by 20 each loop iteration. Each step sets the PWM signal with setPWM(dutyCycle), waits 100 milliseconds, then increases dutyCycle. The loop stops when dutyCycle exceeds 100. The execution table traces each step, showing variable values and actions. Key moments explain why the loop stops and what setPWM does. The visual quiz tests understanding of dutyCycle values and loop behavior. The concept snapshot summarizes how duty cycle affects brightness or speed.
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
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 A
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
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 C
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?
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 A
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
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 B
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
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 D
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 unknownstrongwhile earning +1 Grip toward your certificate.