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 121840

  • PWM1: GPIO 13194145

  • 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 20425254

  • PWM5: GPIO 21435355

  • PWM6: GPIO 63156

  • PWM7: GPIO 73157

  • PWM8: GPIO 2844

  • PWM9: GPIO 3944

⚠️ 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 PinBCM GPIOPWM ChannelNotes
13.3V-Power
25V-Power
32PWM8SDA1 (I2C), also used for HAT EEPROM
45V-Power
53PWM9SCL1 (I2C), also used for HAT EEPROM
6GND-Ground
74-GPCLK0
814-TXD0 (UART)
9GND-Ground
1015-RXD0 (UART)
1117-
1218PWM0PCM_CLK
1327-
14GND-Ground
1522-
1623-
173.3V-Power
1824-
1910PWM2MOSI (SPI), also used for HAT EEPROM - use with caution!
20GND-Ground
219PWM9MISO (SPI), also used for HAT EEPROM
2225-
2311PWM3SCLK (SPI), also used for HAT EEPROM - use with caution!
248PWM8CE0 (SPI)
25GND-Ground
267PWM7CE1 (SPI)
270-EEPROM ID_SC (HAT)
281-EEPROM ID_SD (HAT)
295-
30GND-Ground
316PWM6/7Shared pin for two channels
3212PWM0
3313PWM1
34GND-Ground
3519PWM1MISO (SPI1), PCM_FS
3616-
3726-
3820PWM4MOSI (SPI1)
39GND-Ground
4021PWM5SCLK (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:

bash
sudo raspi-config

Navigate to Interface Options -> PWM to enable or disable it.

2. Python Example using gpiod:

First, install the necessary library:

bash
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):

python
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

  1. 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.

  2. Choose One Pin Per Channel: Remember that each PWM channel can only drive one physical pin at a time.

  3. 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.

  4. 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.

  5. New GPIO Chip on Pi 5: The Raspberry Pi 5 uses gpiochip4 as its primary GPIO chip, which is different from previous models (often gpiochip0). Always check the correct chip with gpiodetect:

    bash
    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.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How To Connect Stm32 To PC?

How to add a GPS sensor to ESP32 for Wokwi?