What pins on Raspberry Pi 5 can use PWM?
The Raspberry Pi 5 has significantly improved and expanded its PWM capabilities compared to its predecessors. Here's a detailed breakdown of the PWM pins on the Raspberry Pi 5.
Quick Answer: Which GPIO Pins Have PWM?
The Raspberry Pi 5 has up to 10 independent PWM channels that can be output on multiple GPIO pins.
PWM0: GPIO 12, 18, 40
PWM1: GPIO 13, 19, 41, 45
PWM2: GPIO 10 (Note: This is also used for the EEPROM for HATs, so use with caution)
PWM3: GPIO 11 (Note: This is also used for the EEPROM for HATs, so use with caution)
PWM4: GPIO 20, 42, 52, 54
PWM5: GPIO 21, 43, 53, 55
PWM6: GPIO 6, 31, 56
PWM7: GPIO 7, 31, 57
PWM8: GPIO 2, 8, 44
PWM9: GPIO 3, 9, 44
⚠️ Important Note: You can only use one pin per PWM channel at a time. For example, you cannot have PWM0 output on both GPIO 12 and GPIO 18 simultaneously. You must choose one.
Detailed Pinout Table for Raspberry Pi 5 PWM
This table shows all 40 pins of the GPIO header, with PWM-capable pins highlighted and their associated channels.
| Physical Pin | BCM GPIO | PWM Channel | Notes |
|---|---|---|---|
| 1 | 3.3V | - | Power |
| 2 | 5V | - | Power |
| 3 | 2 | PWM8 | SDA1 (I2C), also used for HAT EEPROM |
| 4 | 5V | - | Power |
| 5 | 3 | PWM9 | SCL1 (I2C), also used for HAT EEPROM |
| 6 | GND | - | Ground |
| 7 | 4 | - | GPCLK0 |
| 8 | 14 | - | TXD0 (UART) |
| 9 | GND | - | Ground |
| 10 | 15 | - | RXD0 (UART) |
| 11 | 17 | - | |
| 12 | 18 | PWM0 | PCM_CLK |
| 13 | 27 | - | |
| 14 | GND | - | Ground |
| 15 | 22 | - | |
| 16 | 23 | - | |
| 17 | 3.3V | - | Power |
| 18 | 24 | - | |
| 19 | 10 | PWM2 | MOSI (SPI), also used for HAT EEPROM - use with caution! |
| 20 | GND | - | Ground |
| 21 | 9 | PWM9 | MISO (SPI), also used for HAT EEPROM |
| 22 | 25 | - | |
| 23 | 11 | PWM3 | SCLK (SPI), also used for HAT EEPROM - use with caution! |
| 24 | 8 | PWM8 | CE0 (SPI) |
| 25 | GND | - | Ground |
| 26 | 7 | PWM7 | CE1 (SPI) |
| 27 | 0 | - | EEPROM ID_SC (HAT) |
| 28 | 1 | - | EEPROM ID_SD (HAT) |
| 29 | 5 | - | |
| 30 | GND | - | Ground |
| 31 | 6 | PWM6/7 | Shared pin for two channels |
| 32 | 12 | PWM0 | |
| 33 | 13 | PWM1 | |
| 34 | GND | - | Ground |
| 35 | 19 | PWM1 | MISO (SPI1), PCM_FS |
| 36 | 16 | - | |
| 37 | 26 | - | |
| 38 | 20 | PWM4 | MOSI (SPI1) |
| 39 | GND | - | Ground |
| 40 | 21 | PWM5 | SCLK (SPI1) |
*(Pins 41-46 are part of the new secondary connector and also have PWM capabilities, as listed in the quick answer above).*
How to Use PWM on Raspberry Pi 5
You have two primary methods to control PWM: Hardware PWM (precise, jitter-free) and Software PWM (flexible but less accurate).
Method 1: Using Hardware PWM (Recommended)
The best way to access hardware PWM on the Pi 5 is via the gpiod library, which has replaced the deprecated RPi.GPIO library.
1. Enable PWM (if not already enabled):
PWM is typically enabled by default on the relevant pins. You can check and modify settings using raspi-config:
sudo raspi-configNavigate to Interface Options -> PWM to enable or disable it.
2. Python Example using gpiod:
First, install the necessary library:
sudo apt update sudo apt install python3-libgpiod
Here's a Python script to generate a 1kHz PWM signal with a 50% duty cycle on GPIO 18 (PWM0):
import gpiod import time # Define the chip and line (pin) CHIP = 'gpiochip4' # Typically the main GPIO chip on Pi 5 PIN = 18 # Configure the PWM properties FREQUENCY = 1000 # 1 kHz DUTY_CYCLE = 50 # 50% try: with gpiod.Chip(CHIP) as chip: # Get the line (pin) line = chip.get_line(PIN) # Request the line for output as PWM config = gpiod.line_request() config.consumer = "pwm-example" config.request_type = gpiod.line_request.DIRECTION_OUTPUT line.request(config) # Simple software PWM loop (for true hardware PWM, see lgpio below) period = 1.0 / FREQUENCY on_time = period * (DUTY_CYCLE / 100.0) off_time = period - on_time print("Generating PWM... Press CTRL+C to exit.") while True: line.set_value(1) time.sleep(on_time) line.set_value(0) time.sleep(off_time) except KeyboardInterrupt: print("\nExiting...") except Exception as e: print(f"An error occurred: {e}")
For true hardware PWM performance, consider using the lgpio library, which provides better access to the hardware controllers.
Method 2: Using Software PWM (Flexible but Less Precise)
Software PWM uses CPU cycles to toggle the pin, which can cause jitter but allows you to use almost any GPIO pin.
Python Example with gpiod (Software PWM):
The example above is actually a software PWM implementation. For a more robust solution, you would use a dedicated library or a background thread.
Best Practices and Important Considerations
Avoid PWM2 and PWM3 for HATs: If you are using a HAT (Hardware Attached on Top), avoid using GPIO 10 (PWM2) and GPIO 11 (PWM3), as they are used for the HAT EEPROM communication.
Choose One Pin Per Channel: Remember that each PWM channel can only drive one physical pin at a time.
Check Alternative Functions: Many PWM pins are shared with other functions (SPI, I2C, UART). Ensure you disable those functions if you want to use the pin exclusively for PWM.
Power Limitations: The GPIO pins can only supply a limited amount of current (typically ~16mA per pin, with a total limit for all pins). For driving motors or LEDs, use an external driver or transistor.
New GPIO Chip on Pi 5: The Raspberry Pi 5 uses
gpiochip4as its primary GPIO chip, which is different from previous models (oftengpiochip0). Always check the correct chip withgpiodetect:sudo gpiodetect
Summary
The Raspberry Pi 5 offers extensive and flexible PWM capabilities across 10 independent channels. For the best performance, use the hardware PWM channels (PWM0, PWM1, PWM4, PWM5 are good choices) and access them through modern libraries like gpiod or lgpio. Always refer to the official Raspberry Pi GPIO documentation for the most up-to-date information.

评论
发表评论