0
0
Raspberry Piprogramming~10 mins

Software PWM with RPi.GPIO in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Software PWM with RPi.GPIO
Start Program
Setup GPIO Pin
Enter Loop
Set Pin HIGH
Wait ON Duration
Set Pin LOW
Wait OFF Duration
Repeat Loop
Back to Enter Loop
The program sets up a GPIO pin, then repeatedly turns it ON and OFF with delays to create a PWM signal.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

while True:
    GPIO.output(18, GPIO.HIGH)
    time.sleep(0.001)
    GPIO.output(18, GPIO.LOW)
    time.sleep(0.009)
This code creates a software PWM on pin 18 with 10% duty cycle by turning the pin ON for 1ms and OFF for 9ms repeatedly.
Execution Table
StepActionPin 18 StateDelay (seconds)Loop Iteration
1Set pin 18 HIGHHIGH01
2Wait ON durationHIGH0.0011
3Set pin 18 LOWLOW01
4Wait OFF durationLOW0.0091
5Set pin 18 HIGHHIGH02
6Wait ON durationHIGH0.0012
7Set pin 18 LOWLOW02
8Wait OFF durationLOW0.0092
...............
💡 Loop runs indefinitely until program is stopped manually.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8...final
Pin 18 StateLOW (default)HIGHLOWHIGHLOWRepeats HIGH/LOW
Loop Iteration01122Increments each cycle
Key Moments - 3 Insights
Why does the pin state change between HIGH and LOW repeatedly?
Because the code sets the pin HIGH, waits (ON time), then sets it LOW, waits (OFF time), creating a pulse. See execution_table steps 1-4.
How does the duty cycle relate to the ON and OFF delays?
Duty cycle is ON time divided by total period (ON + OFF). Here, ON is 0.001s and OFF is 0.009s, so duty cycle is 10%. See delays in execution_table.
Why is this called software PWM?
Because the timing is controlled by software delays (time.sleep), not hardware PWM modules. The loop manually toggles the pin.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pin state at Step 3?
AHIGH
BLOW
CUndefined
DStill HIGH
💡 Hint
Refer to Step 3 in execution_table where pin 18 is set LOW.
At which step does the first OFF delay happen?
AStep 2
BStep 1
CStep 4
DStep 3
💡 Hint
Check execution_table for delay after pin is set LOW, which is Step 4.
If the ON delay is increased to 0.005 seconds, how does the duty cycle change?
ADuty cycle increases
BDuty cycle decreases
CDuty cycle stays the same
DDuty cycle becomes zero
💡 Hint
Duty cycle = ON time / (ON + OFF). Increasing ON time increases duty cycle.
Concept Snapshot
Software PWM with RPi.GPIO:
- Setup GPIO pin as output
- Loop: set pin HIGH, wait ON time
- Set pin LOW, wait OFF time
- Repeat for continuous PWM
- Duty cycle = ON time / (ON + OFF)
Full Transcript
This example shows how to create software PWM on a Raspberry Pi using the RPi.GPIO library. The program sets a GPIO pin as output and enters a loop. In each loop, it sets the pin HIGH, waits for the ON duration, then sets the pin LOW and waits for the OFF duration. This cycle repeats indefinitely, creating a pulse-width modulated signal. The duty cycle is controlled by adjusting the ON and OFF wait times. This method uses software delays, so timing is less precise than hardware PWM but is simple to implement.