Pulse Width Modulation (PWM) is one of the most important techniques used in Raspberry Pi electronics and automation projects. It allows digital GPIO pins to simulate analog-like control by adjusting signal timing instead of output voltage.

Raspberry Pi PWM Overview
Pulse Width Modulation (PWM) is a method that allows Raspberry Pi GPIO pins to control output power by rapidly switching a digital signal ON and OFF. Instead of creating a true analog voltage, PWM changes the amount of time the signal stays HIGH during each cycle. This ON-time percentage is called the duty cycle.
How PWM Works on Raspberry Pi

A PWM signal repeatedly switches a Raspberry Pi GPIO pin between HIGH and LOW states. The signal remains digital, but connected devices respond to the average effect of the switching.
Two main parameters control PWM operation:
| PWM Parameter | Meaning |
|---|---|
| Frequency | How many times does the signal repeat per second |
| Duty Cycle | How long the signal stays ON during each cycle |
On Raspberry Pi GPIO pins, HIGH is approximately 3.3V. A 50% duty cycle means the signal stays HIGH half the time and LOW half the time. This can make an LED appear half-bright or reduce the average power delivered to a motor.
Duty cycle is calculated using:
Duty Cycle = ON Time / Total Period × 100%
| Duty Cycle | Signal Behavior | Practical Effect |
|---|---|---|
| 0% | Always OFF | No output |
| 25% | ON for one-quarter of the cycle | Low output |
| 50% | ON for half of the cycle | Medium output |
| 75% | ON for three-quarters of the cycle | High output |
| 100% | Always ON | Full output |
Increasing the duty cycle increases average output, while decreasing the duty cycle lowers it.
PWM frequency is the number of PWM cycles per second. It is measured in hertz (Hz).
Frequency is calculated using:
f=1/T
| Device | Typical PWM Frequency |
|---|---|
| LEDs | Hundreds of Hz or higher |
| DC Motors | Hundreds of Hz to several kHz |
| Servos | Around 50 Hz |
| Audio Output | Much higher frequencies |
The wrong frequency can cause flickering LEDs, noisy motors, unstable servos, or poor audio quality.
Hardware PWM vs Software PWM on Raspberry Pi

| Topic | Hardware PWM | Software PWM |
|---|---|---|
| Definition | Uses dedicated PWM hardware inside the Raspberry Pi processor | Uses software-controlled GPIO switching to create PWM pulses |
| Timing Control | Controlled by hardware | Controlled by software and the Linux operating system |
| Timing Accuracy | High | Moderate |
| Signal Stability | Very stable with less jitter | Can jitter, especially under heavy CPU load |
| CPU Usage | Low CPU usage | Higher CPU usage |
| GPIO Flexibility | Limited to selected GPIO pins | Can work on many GPIO pins |
| Frequency Control | More accurate and stable | Depends on system load |
| Performance Under Load | Maintains a stable output better during background activity | May become unstable when Linux background tasks interrupt pulse timing |
| Best Applications | Servos, motor drivers, fans, audio output, precision control systems | LED dimming, beginner projects, low-speed control, status indicators |
| Main Advantage | Smooth and accurate PWM output | Easy setup and more GPIO options |
| Main Limitation | Available only on selected pins | Less accurate timing |
| Recommended Use | Use when timing accuracy matters | Use for simple projects where small timing errors are acceptable |
Raspberry Pi PWM GPIO Pins and Configuration

GPIO Numbering and Hardware PWM Pins
Raspberry Pi uses multiple GPIO numbering systems. Most modern Python libraries use BCM numbering, while physical numbering refers to the actual pin locations on the header.
| Numbering Type | Meaning |
|---|---|
| BCM Numbering | Broadcom GPIO numbering is used internally |
| Physical Pin Numbering | Actual header pin positions |
| WiringPi Numbering | Older deprecated numbering system |
Always verify GPIO numbering before wiring hardware.
Common Hardware PWM Pins
| BCM GPIO | Physical Pin | PWM Channel | Common Use |
|---|---|---|---|
| GPIO12 | Pin 32 | PWM0 | LEDs, motors, servos |
| GPIO13 | Pin 33 | PWM1 | LEDs, motors, servos |
| GPIO18 | Pin 12 | PWM0 | Audio, servos, motor control |
| GPIO19 | Pin 35 | PWM1 | Audio, servos, motor control |
GPIO18 is commonly used because it supports hardware PWM and is widely supported in PWM tutorials and libraries.
PWM Libraries and Configuration
Common Raspberry Pi PWM Libraries
| Library | Main Purpose | Notes |
|---|---|---|
| RPi.GPIO | Basic GPIO and PWM control | Beginner-friendly |
| gpiozero | Simplified device control | High-level interface |
| pigpio | Precise timing and advanced PWM | Supports DMA timing |
| lgpio | Modern GPIO control | Lower-level access |
Typical PWM Configuration Parameters
| Parameter | Description |
|---|---|
| Frequency | PWM switching speed |
| Duty Cycle | Percentage of ON time |
| GPIO Pin | Selected PWM output pin |
| PWM Mode | Hardware PWM or software PWM |
PWM Limitations and Wiring Safety
Raspberry Pi hardware PWM has several limitations that become important in robotics, motor control, audio systems, and industrial applications.
| Limitation | Explanation |
|---|---|
| Limited PWM channels | Only a small number of hardware PWM outputs are available |
| Shared functions | Some GPIO pins share peripherals |
| Audio conflicts | PWM may conflict with onboard audio |
| Pin restrictions | Not all GPIO pins support hardware PWM |
| Frequency sharing | Some PWM channels share clock sources |
| DMA dependency | Advanced PWM methods may use DMA resources |
Many PWM problems are caused by wiring mistakes rather than software issues.
| Wiring Mistake | Why It Matters |
|---|---|
| Mixing BCM and physical numbering | Controls the wrong GPIO pin |
| Connecting motors directly to GPIO | May damage Raspberry Pi |
| Missing common ground | Causes unstable signals |
| Exceeding GPIO current limits | Can permanently damage the GPIO pins |
| Missing flyback diode | Voltage spikes may damage components |
| Wrong voltage connection | GPIO pins support only 3.3V logic |
| Loose jumper wires | Causes unstable PWM signals |
Recommended Protection Components
| Component | Purpose |
|---|---|
| MOSFET | Safely switches higher-current loads |
| H-Bridge Driver | Controls motor speed and direction |
| Flyback Diode | Protects against inductive voltage spikes |
| External Power Supply | Powers motors safely |
| Optocoupler | Provides electrical isolation |
Raspberry Pi PWM Applications
LED Dimming Systems

PWM is widely used in Raspberry Pi LED projects because it can control brightness without requiring a true analog output pin. By changing the duty cycle, LEDs can appear dimmer or brighter. This is useful for LED brightness control, RGB color mixing, decorative lighting, adaptive brightness systems, and status indicators. A high enough PWM frequency should be used to prevent visible flicker.
Motor Speed Control

PWM is commonly used to control DC motor speed in robot wheels, pumps, fans, conveyor systems, and automation actuators. Instead of changing the supply voltage directly, PWM adjusts the average power delivered to the motor. Higher duty cycles generally increase motor speed, while lower duty cycles reduce it. External motor drivers are required because Raspberry Pi GPIO pins cannot safely drive motors directly.\
Servo Position Control

Servo motors use repeating control pulses where pulse width determines position in robotic arms, pan-tilt camera systems, RC vehicles, and automation systems. Small timing errors may cause shaking, buzzing, or unstable movement, so stable PWM timing is important for reliable servo control.
Audio Signal Generation

Raspberry Pi PWM can generate simple tones, alarms, buzzers, and basic audio signals by switching GPIO pins at audible frequencies. PWM is useful for simple embedded sound output, but it cannot fully replace a dedicated DAC for high-quality audio applications.
Industrial and IoT Applications

PWM is useful in Raspberry Pi industrial and IoT systems because it can control adjustable hardware outputs. Common applications include smart agriculture, environmental monitoring, HVAC systems, industrial automation, and IoT control systems. PWM frequently controls fans, pumps, valves, lighting systems, and actuators.
Raspberry Pi vs Arduino for PWM

| Category | Raspberry Pi | Arduino |
|---|---|---|
| Operating System | Runs Linux OS | Runs simple firmware |
| Processing Power | Higher processing power for advanced applications | Lower processing power but optimized for control tasks |
| PWM Timing | Less predictable due to multitasking OS | More predictable real-time PWM behavior |
| Latency | Moderate latency | Lower latency with faster hardware response |
| Hardware Control | Indirect hardware access through OS and libraries | Direct hardware access |
| PWM Stability | Good for general-purpose PWM | Better for stable motor control and control loops |
| Networking | Excellent networking and IoT support | Limited networking without extra modules |
| Multitasking | Can run servers, scripts, databases, and multiple applications | Limited multitasking capability |
| Programming | Easy high-level programming with Python | Simple low-level programming with Arduino IDE |
| Beginner Electronics | Good for software-focused beginners | Excellent for electronics beginners |
| Smart Automation | Excellent for connected smart automation systems | Good for standalone automation |
| Real-Time Motor Control | Moderate performance | Excellent performance |
| Precision Timing | Moderate timing accuracy | Excellent timing accuracy |
| Best Use Cases | IoT systems, smart automation, remote monitoring, Linux-based projects | Motors, servos, robotics, embedded control, real-time PWM |
| Main Advantage | Combines PWM with advanced software and networking | Provides precise and stable real-time PWM control |
Raspberry Pi PWM Troubleshooting
| Problem | Possible Cause | Recommended Fix |
|---|---|---|
| Signal not working | Wrong GPIO numbering | Check whether the code uses BCM, physical, or another numbering system. |
| Signal not working | Incorrect wiring | Recheck GPIO connections, resistor placement, and shared ground. |
| Signal not working | Unsupported pin | Confirm whether the selected pin supports hardware or software PWM. |
| Signal not working | Library or permission issue | Verify the GPIO library installation, permissions, and setup commands. |
| Servo jitter | Weak power supply | Use a separate power supply for the servo. |
| Servo jitter | Missing common ground | Connect the Raspberry Pi ground to the servo power supply ground. |
| Servo jitter | Software PWM instability | Use hardware PWM or a more stable library such as pigpio. |
| Servo jitter | Electrical noise | Shorten wires and improve power filtering or wiring layout. |
| LED flickering | Frequency too low | Increase the PWM frequency until flicker is no longer visible. |
| LED flickering | Poor wiring | Check resistor values, loose connections, and wiring quality. |
| Motor noise or instability | Weak power supply | Use an external motor power supply instead of powering the motor from GPIO. |
| Motor noise or instability | Missing protection components | Use a proper motor driver and add protection components such as a flyback diode when needed. |
| Motor noise or instability | Incorrect frequency | Adjust the PWM frequency to suit the motor and driver circuit. |
| pigpio errors | Daemon not running | Start or restart the pigpio daemon. |
| pigpio errors | GPIO conflicts | Check whether another program is already using the same GPIO pin. |
Frequently Asked Questions [FAQ]
Why is PWM important on Raspberry Pi even though GPIO pins are digital-only?
PWM allows Raspberry Pi GPIO pins to simulate analog-like control by rapidly switching signals between HIGH and LOW states. Instead of changing voltage directly, PWM changes the duty cycle to control average power delivery. This makes it possible to dim LEDs, adjust motor speed, control servo position, and manage fans using standard digital GPIO pins.
When should hardware PWM be used instead of software PWM on Raspberry Pi?
Hardware PWM is better for applications that require stable timing and accurate pulse generation, such as servos, motor drivers, audio output, and robotics. Because dedicated hardware controls the signal, it produces less jitter and uses fewer CPU resources. Software PWM is usually sufficient for simpler tasks like LED dimming or status indicators, where small timing errors are less noticeable.
Why can an incorrect PWM frequency cause problems in Raspberry Pi projects?
Different devices respond best to different PWM frequencies. If the frequency is too low, LEDs may visibly flicker, motors can produce noise or rough movement, and servos may become unstable. Very high frequencies may also reduce PWM resolution or increase processing demands. Choosing the correct frequency improves performance, smoothness, and reliability.
Why should motors and servos never connect directly to Raspberry Pi GPIO pins?
Raspberry Pi GPIO pins support only low-current 3.3V logic signals and cannot safely power motors or servos directly. High-current devices may damage GPIO pins, create voltage spikes, or cause unstable operation. External drivers, MOSFETs, H-bridge circuits, flyback diodes, and separate power supplies help protect the Raspberry Pi and improve PWM reliability.
Why is Arduino often better for precise PWM control than Raspberry Pi?
Arduino boards are designed for real-time hardware control and run simple firmware without a multitasking operating system. This allows more predictable PWM timing, lower latency, and better stability for motors, servos, and control loops. Raspberry Pi is stronger in networking, Linux applications, IoT systems, and smart automation, but Linux background activity can affect PWM timing accuracy.