3
votes

Raspberry pi B+ has four hardware PWM. How to use them in C/C++. The less complicated solution - the better.

I found one working solution with WiringPi

gpio mode 1 pwm  
gpio pwm-bal  
gpio pwmr 1024  
gpio pwm 1 500  

This solution works only for one PWM output (GPIO18). I thinks it is because WiringPi is designed for previous version of raspberry pi (Model B+ has new PWMs on GPIO12, GPIO13 and GPIO19).

For controlling regular GPIO I found solution:

pi@auto:~$ sudo -i  
root@auto:~# echo "16" > /sys/class/gpio/export  
root@auto:~# echo "out" > /sys/class/gpio/gpio16/direction  
root@auto:~# echo "1" > /sys/class/gpio/gpio16/value  
root@auto:~# echo "0" > /sys/class/gpio/gpio16/value  

But there is no way how to control hardware PWM.

Product https://www.pololu.com/product/2753 has reference to python library - that do exactly what I want.

Please no software PWM suggestions.

Thanks

Vlado

1
Why don't you read the source code of the working python library, and do the same thing (it uses WiringPi2 -- github.com/Gadgetoid/WiringPi2-Python/tree/master/WiringPi/…)Ben Voigt

1 Answers

1
votes

Here is a a distro that can hopefully fit your need. It provides PWM and Servo kernel modules. You can simply modify the kernel files under /sys/class/rpi-pwm/ to create custom PWM output.
Here is the download link.