0
0
Raspberry Piprogramming~10 mins

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

Choose your learning style9 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.