The first step you have to do is export the PWM on a port.
The command to do so is
echo -n "3" > /sys/class/pwm/pwmchip0/export
If you have already exported it, it should display a message similar to this
-sh: echo: write error: Device or resource busy
Next you need to enable a PWM on a port write “1” to a corresponding enable file using this command:
echo -n "1" > /sys/class/pwm/pwmchip0/pwm3/enable
To set the PWM period in nanoseconds use the following command:
echo 25000 >/sys/class/pwm/pwmchip0/pwm3/period
And to set the PWM duty cycle enter the following command
echo 12500 > /sys/class/pwm/pwmchip0/pwm3/duty_cycle
Now, if you want to modify the duty cycle on each period you can write a Python script. In that script you can modify the value of a given duty cycle for a variable one. The command to do so is
To do this you have to import the library OS. This link shows this in more detail https://docs.python.org/3/library/os.html#os.system.
So it seems that even if I use your method, there's a limitation on the output of the pwm.
If I set
echo 70000 > /sys/class/pwm/pwmchip0/pwm3/period
I get an output of about 13kHz. But if I try to increase the frequency, and say:
echo 60000 > /sys/class/pwm/pwmchip0/pwm3/period
the signal goes to 0.
So it doesn't seem like I can get to 40 kHz using this.
We have double checked the output of the PWM signal with an oscilloscope and the fastest speed we could get using this method was about 14kHz. We know you can get faster speeds with the IDE. We will try the Arduino source code in order to know how to get higher frequencies in the Linux side. As soon as we test it we will let you know our results.