Bird
Raised Fist0
Raspberry Piprogramming~10 mins

PWM frequency and duty cycle relationship in Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - PWM frequency and duty cycle relationship
Start PWM Setup
↓
Set Frequency
↓
Set Duty Cycle
↓
PWM Signal Output
↓
Signal ON time = Duty Cycle * Period
↓
Signal OFF time = Period - ON time
↓
Repeat Signal Continuously
This flow shows how PWM starts by setting frequency and duty cycle, then outputs a repeating signal with ON and OFF times based on these settings.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 1000)  # 1kHz frequency
pwm.start(50)  # 50% duty cycle

time.sleep(2)
pwm.stop()
GPIO.cleanup()
This code sets up a PWM signal on pin 18 with 1kHz frequency and 50% duty cycle, runs it for 2 seconds, then stops.
Execution Table
StepActionFrequency (Hz)Duty Cycle (%)Period (ms)ON Time (ms)OFF Time (ms)
1Set frequency1000-1--
2Set duty cycle10005010.50.5
3PWM signal cycle start10005010.50.5
4Signal ON for ON time10005010.50.5
5Signal OFF for OFF time10005010.50.5
6Repeat cycle10005010.50.5
7Stop PWM-----
💡 PWM stopped after 2 seconds, ending signal output.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 7
Frequency (Hz)-10001000-
Duty Cycle (%)--50-
Period (ms)-11-
ON Time (ms)--0.5-
OFF Time (ms)--0.5-
Key Moments - 2 Insights
Why does changing frequency affect the period but not the duty cycle percentage?
Frequency sets the total period length (1/frequency). Duty cycle is a percentage of that period, so changing frequency changes ON and OFF times but duty cycle percentage stays the same (see execution_table rows 1 and 2).
What happens to ON and OFF times if duty cycle is 0% or 100%?
At 0% duty cycle, ON time is 0 ms (signal always OFF). At 100%, ON time equals the full period (signal always ON). This is implied by the calculation ON Time = Duty Cycle * Period in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what is the ON time in milliseconds?
A0.5 ms
B1 ms
C0 ms
D2 ms
💡 Hint
Check the 'ON Time (ms)' column at Step 2 in the execution_table.
At which step does the PWM signal start repeating its ON and OFF cycle?
AStep 1
BStep 3
CStep 6
DStep 7
💡 Hint
Look for the first mention of PWM signal cycle start in the execution_table.
If the frequency is doubled to 2000 Hz but duty cycle stays 50%, how does the ON time change?
AON time doubles
BON time stays the same
CON time halves
DON time becomes zero
💡 Hint
Recall ON Time = Duty Cycle * Period and that period is 1/frequency from variable_tracker.
Concept Snapshot
PWM frequency sets the signal's period (Period = 1/Frequency).
Duty cycle is the percent of the period signal is ON.
ON time = Duty Cycle * Period.
Changing frequency changes period and ON/OFF times but not duty cycle %.
PWM repeats ON and OFF cycles continuously until stopped.
Full Transcript
PWM (Pulse Width Modulation) works by setting a frequency which determines the total period of the signal. The duty cycle is a percentage that controls how long the signal stays ON during each period. For example, a 1kHz frequency means the period is 1 millisecond. A 50% duty cycle means the signal is ON for 0.5 milliseconds and OFF for 0.5 milliseconds. The code example sets up PWM on a Raspberry Pi pin with these values, runs it for 2 seconds, then stops. The execution table shows each step, including setting frequency, duty cycle, and the ON/OFF times. Key points include that changing frequency changes the period and ON/OFF times but not the duty cycle percentage. If frequency doubles, ON time halves because the period is shorter. This visual trace helps understand how PWM signals are generated and controlled.

Practice

(1/5)
1. What does the duty cycle in PWM control on a Raspberry Pi?
easy
A. The percentage of time the signal is ON during one cycle
B. The speed at which the signal pulses repeat
C. The voltage level of the signal
D. The total duration of the PWM signal

Solution

  1. Step 1: Understand duty cycle meaning

    The duty cycle is the fraction of one cycle when the signal is ON (high).
  2. Step 2: Differentiate from frequency

    Frequency controls how fast cycles repeat, not how long signal stays ON.
  3. Final Answer:

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

    Duty cycle = ON time percentage [OK]
Hint: Duty cycle = ON time percentage in each pulse [OK]
Common Mistakes:
  • Confusing duty cycle with frequency
  • Thinking duty cycle controls voltage
  • Assuming duty cycle affects total signal duration
2. Which of the following is the correct Python syntax to set a PWM frequency to 1000 Hz on a Raspberry Pi GPIO pin using RPi.GPIO?
easy
A. pwm = GPIO.PWM(pin, 1000)
B. pwm = GPIO.setPWM(pin, 1000)
C. pwm = GPIO.PWM(pin).frequency(1000)
D. pwm = GPIO.frequency(pin, 1000)

Solution

  1. Step 1: Recall RPi.GPIO PWM syntax

    The correct way to create a PWM instance is GPIO.PWM(pin, frequency).
  2. Step 2: Check options for correct method and parameters

    pwm = GPIO.PWM(pin, 1000) matches the correct syntax; others use invalid methods or chaining.
  3. Final Answer:

    pwm = GPIO.PWM(pin, 1000) -> Option A
  4. Quick Check:

    GPIO.PWM(pin, freq) creates PWM [OK]
Hint: Use GPIO.PWM(pin, frequency) to set PWM frequency [OK]
Common Mistakes:
  • Using non-existent methods like setPWM or frequency()
  • Trying to set frequency after creating PWM without parameters
  • Confusing method chaining syntax
3. Consider this Python code snippet controlling an LED brightness with PWM on Raspberry Pi:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
pin = 18
GPIO.setup(pin, GPIO.OUT)
pwm = GPIO.PWM(pin, 500)
pwm.start(0)
pwm.ChangeDutyCycle(75)

What is the effect of setting the duty cycle to 75%?
medium
A. LED will be off
B. LED will be at full brightness
C. LED will blink 75 times per second
D. LED will be at 75% brightness

Solution

  1. Step 1: Understand duty cycle effect on LED brightness

    Duty cycle of 75% means LED is ON 75% of the time, so brightness is about 75%.
  2. Step 2: Confirm frequency and duty cycle usage

    Frequency 500 Hz controls pulse speed; duty cycle controls brightness level.
  3. Final Answer:

    LED will be at 75% brightness -> Option D
  4. Quick Check:

    Duty cycle 75% = 75% brightness [OK]
Hint: Duty cycle % roughly equals LED brightness % [OK]
Common Mistakes:
  • Confusing frequency with blinking count
  • Thinking duty cycle 75 means LED off
  • Assuming full brightness only at 100% duty cycle
4. This code tries to change PWM frequency on Raspberry Pi but does not work as expected:
pwm = GPIO.PWM(18, 1000)
pwm.start(50)
pwm.changeFrequency(2000)

What is the error and how to fix it?
medium
A. ChangeFrequency is not a valid method; use pwm.ChangeFrequency(2000)
B. ChangeFrequency method name is incorrect; use pwm.ChangeFrequency(2000)
C. Frequency cannot be changed after PWM.start(); recreate PWM object
D. Method name is case sensitive; use pwm.ChangeFrequency(2000)

Solution

  1. Step 1: Identify method name case sensitivity

    RPi.GPIO does not provide a method to change frequency after PWM object creation.
  2. Step 2: Correct approach to change frequency

    To change frequency, stop PWM and create a new PWM instance with the new frequency.
  3. Final Answer:

    Frequency cannot be changed after PWM.start(); recreate PWM object -> Option C
  4. Quick Check:

    RPi.GPIO PWM frequency fixed at creation [OK]
Hint: To change frequency, recreate PWM object [OK]
Common Mistakes:
  • Using wrong method name spelling
  • Ignoring case sensitivity in Python
  • Trying to change frequency by creating new PWM instance
5. You want to control a motor speed using PWM on Raspberry Pi. The motor requires a minimum frequency of 1000 Hz for smooth operation. If you set frequency to 1000 Hz and duty cycle to 20%, what happens if you increase frequency to 2000 Hz but keep duty cycle at 20%?
hard
A. Motor speed doubles because frequency doubled
B. Motor speed stays roughly the same because duty cycle is unchanged
C. Motor stops because frequency is too high
D. Motor speed halves because frequency doubled

Solution

  1. Step 1: Understand frequency vs duty cycle effect on motor speed

    Motor speed depends mainly on duty cycle (power ON time), not frequency.
  2. Step 2: Analyze frequency change with constant duty cycle

    Increasing frequency while keeping duty cycle constant keeps average power same, so speed stays similar.
  3. Final Answer:

    Motor speed stays roughly the same because duty cycle is unchanged -> Option B
  4. Quick Check:

    Speed ~ duty cycle, frequency affects smoothness [OK]
Hint: Motor speed depends on duty cycle, not frequency [OK]
Common Mistakes:
  • Assuming frequency change affects speed directly
  • Thinking doubling frequency doubles speed
  • Ignoring duty cycle's role in power delivery
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