Bird
Raised Fist0
Embedded Cprogramming~15 mins

Duty cycle control for motor/LED in Embedded C - Deep Dive

Choose your learning style10 modes available
Overview - Duty cycle control for motor/LED
What is it?
Duty cycle control is a way to adjust how much power a motor or LED receives by turning it on and off very quickly. Instead of just on or off, the device gets power for a fraction of each cycle, called the duty cycle. This controls brightness for LEDs or speed for motors smoothly. It uses a technique called Pulse Width Modulation (PWM) to do this switching.
Why it matters
Without duty cycle control, motors and LEDs can only be fully on or fully off, which limits how smoothly you can control speed or brightness. This would make devices jerky or harsh, wasting energy and reducing lifespan. Duty cycle control lets you save power, extend device life, and create smooth, precise control that feels natural and efficient.
Where it fits
Before learning duty cycle control, you should understand basic electronics concepts like voltage, current, and simple on/off control. After this, you can learn about advanced PWM techniques, feedback control systems, and motor driver circuits to build smarter, more efficient devices.
Mental Model
Core Idea
Duty cycle control changes how long a device is powered on in each cycle to smoothly adjust its effective power without changing voltage.
Think of it like...
It's like flicking a light switch on and off very fast, but changing how long the switch stays on each time to make the light look dimmer or brighter.
┌───────────────┐
│ PWM Cycle    │
│ ┌─────────┐ │
│ │ ON      │ │  <-- Device powered ON for this time
│ └─────────┘ │
│ ┌─────────┐ │
│ │ OFF     │ │  <-- Device powered OFF for this time
│ └─────────┘ │
└───────────────┘

Duty Cycle = ON time / Total cycle time
Build-Up - 6 Steps
1
FoundationUnderstanding On/Off Control Basics
🤔
Concept: Learn how simple on/off control works for motors and LEDs.
In embedded systems, turning a motor or LED fully on means applying full voltage, and turning it off means no voltage. This is done by setting a digital pin HIGH or LOW. For example, setting a pin HIGH powers the device fully, and LOW turns it off.
Result
The motor runs at full speed or stops; the LED is fully bright or off.
Knowing basic on/off control is essential because duty cycle control builds on switching power rapidly between these two states.
2
FoundationWhat is Duty Cycle and PWM?
🤔
Concept: Introduce the idea of switching power on and off quickly to control average power.
Duty cycle is the percentage of time the power is ON during one cycle. PWM means Pulse Width Modulation, where the width of the ON pulse changes to adjust power. For example, a 50% duty cycle means power is ON half the time and OFF half the time.
Result
Devices receive partial power, allowing control between fully on and off.
Understanding duty cycle as a ratio helps visualize how average power changes without changing voltage.
3
IntermediateImplementing PWM in Embedded C
🤔Before reading on: do you think PWM requires special hardware or can it be done with simple code loops? Commit to your answer.
Concept: Learn how to write embedded C code to generate PWM signals using timers or software loops.
Most microcontrollers have hardware timers to generate PWM signals automatically. You configure the timer's period and duty cycle registers. Alternatively, you can write software loops that turn pins on and off with delays, but this is less efficient.
Result
PWM signals control the motor or LED brightness smoothly.
Knowing hardware PWM is more efficient and precise than software PWM helps write better embedded programs.
4
IntermediateAdjusting Duty Cycle to Control Speed/Brightness
🤔Before reading on: do you think increasing duty cycle always increases brightness/speed linearly? Commit to your answer.
Concept: Explore how changing duty cycle affects motor speed or LED brightness and the real-world response.
Increasing duty cycle generally increases power, making motors spin faster and LEDs brighter. However, the relationship may not be perfectly linear due to device characteristics like motor inertia or LED response curves.
Result
You can control speed or brightness smoothly but may need calibration for precise control.
Understanding non-linear responses prevents surprises when tuning devices in real applications.
5
AdvancedHandling PWM Frequency and Its Effects
🤔Before reading on: do you think PWM frequency affects device behavior? Commit to your answer.
Concept: Learn why PWM frequency matters and how to choose it for motors and LEDs.
PWM frequency is how fast the on/off cycles repeat each second. Too low frequency causes visible flicker in LEDs or jerky motor motion. Too high frequency can cause more power loss or electromagnetic noise. Typical frequencies are a few kHz for LEDs and motors.
Result
Choosing the right frequency improves smoothness and efficiency.
Knowing frequency effects helps avoid flicker and noise problems in real devices.
6
ExpertAdvanced Duty Cycle Control with Feedback
🤔Before reading on: do you think open-loop PWM control is always enough for precise motor speed? Commit to your answer.
Concept: Explore closed-loop control where sensors adjust duty cycle automatically for stable performance.
In advanced systems, sensors measure motor speed or LED brightness and adjust duty cycle dynamically using control algorithms like PID. This compensates for load changes or power supply variations, keeping performance steady.
Result
Motors run at consistent speeds and LEDs maintain brightness despite changes.
Understanding feedback control reveals how duty cycle control scales from simple to sophisticated real-world systems.
Under the Hood
Internally, PWM uses timers that count clock pulses to create repeating cycles. The timer compares the current count to a threshold set by the duty cycle. When the count is below the threshold, the output pin is set HIGH; otherwise, it is LOW. This switching happens so fast that the device averages the power over time.
Why designed this way?
PWM was designed to control power efficiently without wasting energy as heat. Instead of reducing voltage with resistors, PWM switches full voltage on and off rapidly, which is easier for digital circuits and reduces power loss.
┌─────────────┐
│ Timer Count │
│ 0 ──────────┐
│             │
│    Compare ─┼─> Output HIGH if count < duty threshold
│             │
│ Max Count ──┘
│             
│ Output Pin ──┐
│             │
│ PWM Signal  │
└─────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does a 50% duty cycle mean the device gets half the voltage? Commit yes or no.
Common Belief:A 50% duty cycle means the device receives half the voltage all the time.
Tap to reveal reality
Reality:The device receives full voltage but only half the time, so the average power is half, not the voltage.
Why it matters:Confusing voltage with duty cycle can lead to wrong circuit designs and damage devices expecting steady voltage.
Quick: Is higher PWM frequency always better? Commit yes or no.
Common Belief:Higher PWM frequency always improves device performance.
Tap to reveal reality
Reality:Too high frequency can cause more power loss, heat, and electromagnetic interference, harming efficiency.
Why it matters:Ignoring frequency tradeoffs can cause noisy circuits and reduced device lifespan.
Quick: Can software PWM always replace hardware PWM without downsides? Commit yes or no.
Common Belief:Software PWM works just as well as hardware PWM in all cases.
Tap to reveal reality
Reality:Software PWM is less precise, uses more CPU, and can cause timing issues, especially at high frequencies.
Why it matters:Using software PWM in critical timing applications can cause unstable control and poor device behavior.
Quick: Does increasing duty cycle always increase motor speed instantly? Commit yes or no.
Common Belief:Increasing duty cycle immediately increases motor speed linearly.
Tap to reveal reality
Reality:Motor speed changes gradually due to inertia and load; response is not instant or perfectly linear.
Why it matters:Expecting instant speed changes can lead to poor control algorithms and unstable systems.
Expert Zone
1
Some motors require specific PWM frequencies to avoid audible noise or mechanical resonance.
2
LED brightness perception is logarithmic, so linear duty cycle changes don't appear linear to the human eye.
3
Combining PWM with current sensing allows safer and more efficient motor control by preventing overload.
When NOT to use
Duty cycle control is not suitable when precise voltage levels are needed or for devices sensitive to switching noise. In such cases, analog voltage regulators or digital-to-analog converters (DACs) are better alternatives.
Production Patterns
In production, duty cycle control is often combined with sensor feedback and safety checks. For example, motor drivers use PWM with current limiters and temperature sensors to protect hardware. LED drivers may use gamma correction tables to adjust duty cycle for perceived brightness.
Connections
Analog Signal Processing
Duty cycle control simulates analog voltage levels using digital switching, bridging digital and analog domains.
Understanding PWM helps grasp how digital systems approximate analog signals efficiently.
Control Systems Engineering
Duty cycle control is a fundamental actuator method used in feedback loops for regulating speed or brightness.
Knowing duty cycle control clarifies how control systems physically implement commands.
Human Visual Perception
LED brightness controlled by duty cycle interacts with how humans perceive light intensity non-linearly.
Recognizing this helps design better user experiences by adjusting PWM for perceived brightness, not just electrical power.
Common Pitfalls
#1Using very low PWM frequency causing visible flicker in LEDs.
Wrong approach:Set PWM frequency to 50 Hz for LED brightness control.
Correct approach:Set PWM frequency to at least 1 kHz to avoid flicker.
Root cause:Misunderstanding that low frequency PWM causes visible flicker because the LED turns on and off slowly enough for the eye to detect.
#2Trying to implement PWM with software delays in a busy system causing unstable timing.
Wrong approach:Use delay loops in main program to toggle pin for PWM.
Correct approach:Use hardware timers to generate PWM signals independently of main program flow.
Root cause:Not realizing software delays are affected by other code and interrupts, making PWM timing inconsistent.
#3Assuming duty cycle percentage equals perceived brightness linearly.
Wrong approach:Set LED brightness by directly mapping duty cycle 0-100% to brightness.
Correct approach:Apply gamma correction to duty cycle values to match human brightness perception.
Root cause:Ignoring the non-linear way humans perceive light intensity.
Key Takeaways
Duty cycle control uses fast switching to adjust average power without changing voltage.
PWM frequency and duty cycle together determine smoothness and effectiveness of control.
Hardware timers provide precise and efficient PWM signals compared to software loops.
Real devices respond non-linearly to duty cycle changes, requiring calibration or correction.
Advanced control uses feedback to adjust duty cycle dynamically for stable performance.

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