Four channel PWM on a Raspberry Pi 5
Hi.
I'm just trying out things. My goal is to move from a Motor HAT to a smaller chip with the RPI5 PWM channels driving an H-bridge.
The rp1-peripherals document very clearly states that there are 4 independent PWM channels. I'm just trying to access them.
First with bash, then later with c++.
Trying to use gpioctrl + sysfs just for proof of concept. No custom overlays loaded.
And I do the simplest possible thing.
And I believe that I should be able to measure a voltage on those pins. However, its all zeros.
And it's controlling the fan on pwm3!
which is weird because
Any help would be greatly appreciated.
I'm just trying out things. My goal is to move from a Motor HAT to a smaller chip with the RPI5 PWM channels driving an H-bridge.
The rp1-peripherals document very clearly states that there are 4 independent PWM channels. I'm just trying to access them.
First with bash, then later with c++.
Trying to use gpioctrl + sysfs just for proof of concept. No custom overlays loaded.
Code: Select all
uname -a
Linux raspberrypi 6.12.47+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.12.47-1+rpt1 (2025年09月16日) aarch64 GNU/Linux
Code: Select all
#unbind some fan driver holding pwm 3. a bit brute-ish but works.
echo "1f0009c000.pwm" | sudo tee /sys/class/pwm/pwmchip0/device/driver/unbind
# give all the channels!
echo 0 | sudo tee /sys/class/pwm/pwmchip0/export
echo 1 | sudo tee /sys/class/pwm/pwmchip0/export
echo 2 | sudo tee /sys/class/pwm/pwmchip0/export
echo 3 | sudo tee /sys/class/pwm/pwmchip0/export
# after this we excpect to see pwm0-3 in /sys/class/pwm/pwmchip0/
ls -l /sys/class/pwm/pwmchip0/
total 0
lrwxrwxrwx 1 root gpio 0 Nov 6 14:10 device -> ../../../1f0009c000.pwm
--w--w---- 1 root gpio 16384 Nov 6 14:15 export
-r--r--r-- 1 root gpio 16384 Nov 6 14:10 npwm
drwxrwxr-x 2 root gpio 0 Nov 6 14:10 power
drwxrwxr-x 3 root gpio 0 Nov 6 14:15 pwm0
drwxrwxr-x 3 root gpio 0 Nov 6 14:14 pwm1
drwxrwxr-x 3 root gpio 0 Nov 6 14:15 pwm2
drwxrwxr-x 3 root gpio 0 Nov 6 14:15 pwm3
lrwxrwxrwx 1 root gpio 0 Nov 6 14:10 subsystem -> ../../../../../../../class/pwm
-rw-rw-r-- 1 root gpio 16384 Nov 6 14:10 uevent
--w--w---- 1 root gpio 16384 Nov 6 14:10 unexport
# now we set the pins!
sudo pinctrl set 12-15 a0
# and confirm
pinctrl get 12-15
12: a0 pd | lo // GPIO12 = PWM0_CHAN0
13: a0 pd | lo // GPIO13 = PWM0_CHAN1
14: a0 pd | lo // GPIO14 = PWM0_CHAN2
15: a0 pd | lo // GPIO15 = PWM0_CHAN3
#and enable the outputs!
for i in 0 1 2 3; do
echo "=== pwm$i ==="
echo 100000 | sudo tee /sys/class/pwm/pwmchip0/pwm$i/period
echo 50000 | sudo tee /sys/class/pwm/pwmchip0/pwm$i/duty_cycle
echo 1 | sudo tee /sys/class/pwm/pwmchip0/pwm$i/enable
done
And it's controlling the fan on pwm3!
which is weird because
Code: Select all
pinctrl | grep -i pwm
12: a0 pd | lo // GPIO12 = PWM0_CHAN0
13: a0 pd | lo // GPIO13 = PWM0_CHAN1
14: a0 pd | lo // GPIO14 = PWM0_CHAN2
15: a0 pd | lo // GPIO15 = PWM0_CHAN3
45: a0 pd | hi // FAN_PWM/GPIO45 = PWM1_CHAN3
- aBUGSworstnightmare
- Posts: 13432
- Joined: Tue Jun 30, 2015 1:35 pm
Re: Four channel PWM on a Raspberry Pi 5
how did you enable the PWMs?
here is an overlay for you to get you started viewtopic.php?p=2205275#p2205275
here is an overlay for you to get you started viewtopic.php?p=2205275#p2205275
Re: Four channel PWM on a Raspberry Pi 5
Humble pie for me!aBUGSworstnightmare wrote: ↑Thu Nov 06, 2025 2:26 pmhow did you enable the PWMs?
here is an overlay for you to get you started viewtopic.php?p=2205275#p2205275
adding
dtoverlay=pwm
in /boot/firmware/config.txt made it work after reboot.
Thanks a lot!
Return to "Interfacing (DSI, CSI, I2C, etc.)"
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion