dart_periphery icon indicating copy to clipboard operation
dart_periphery copied to clipboard

Raspberry Pi Model 4B PWM Error

Open AllyTechEngineering opened this issue 10 months ago • 0 comments

The documentation in the README (the link) for setting up the Raspberry Pi Model 4B, so that it can run successfully using the dart_periphery package, appears to be incomplete. For example, after a reboot on my Raspberry Pi Model 4B, I would get a pwm error when opening pwmchip0, pwm0 and pwm1 channels. The next time or two restarting in debug mode it would start working. Upon investigation, the issue was with the /etc/udev/rules.d/99-pwm.rules file and with the dtoverlay file. When the Pi would boot up, the pwmchip0 did not have pwm0 and pwm1 exported.

Note that the Model 4 only has two pwm channels on pwm0. You can only use GPIO 12,13,18 or 19 for PWM. I am using GPIO 12 and 13. In the link about how to setup the pwm there are other dtoverlays but I have not tested all of them.

Here is My Solution

Step One: Update the config.txt

Use this dtoverlay in config.txt

dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4

Step Two: Update the udev rule file

Open the file, and you will probably only see: SUBSYSTEM=="pwm", GROUP="gpio", MODE="0660"

sudo nano /etc/udev/rules.d/99-pwm.rules

Copy and paste the two ACTION lines under the SUBSYSTEM line.

SUBSYSTEM=="pwm", GROUP="gpio", MODE="0660"
ACTION=="add", SUBSYSTEM=="pwm", KERNEL=="pwmchip0", RUN+="/bin/sh -c 'echo 0 > /sys/class/pwm/pwmchip0/export'"
ACTION=="add", SUBSYSTEM=="pwm", KERNEL=="pwmchip0", RUN+="/bin/sh -c 'echo 1 > /sys/class/pwm/pwmchip0/export'"

After saving the file, reload the udev rules.

sudo udevadm control --reload-rules && sudo udevadm trigger

You will need to reboot.

Step Three: verify that pwmchip0 exported pwm0 and pwm1.

This command will check that pwm0 has been exported. If you do not see this, then retrace your steps and try again.

ls /sys/class/pwm/pwmchip0

You should see this response. device export npwm power pwm0 pwm1 subsystem uevent unexport

Step Four: Run Flutter App with dart_periphery


On the Raspberry Pi Model 4B the only valid options are pwm(0,0) and pwm(0,1) These steps fixed the issue for me.

AllyTechEngineering avatar Feb 04 '25 01:02 AllyTechEngineering