PWM support

Revision as of 05:32, 2 February 2019 by Sicelo (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

For PWM, you need to load the Device Tree Overlay manually as follows:

sudo mkdir /sys/kernel/config/device-tree/overlays/chip-pwm       #Assuming your configfs is mounted
sudo sh -c 'cat /lib/firmware/nextthingco/chip/sample-pwm.dtbo > /sys/kernel/config/device-tree/overlays/chip-pwm/dtbo'

When you no longer need it, you can simply do the following (after unexporting, etc.):

sudo rmdir /sys/kernel/config/device-tree/overlays/chip-pwm

However, if you are using the rd235a kernel, you do not need the above steps as PWM is already enabled.

Regardless of the method you have opted to use, PWM0 can be activated in this way:

cd /sys/class/pwm/pwmchip0
sudo echo 0 > export

Now the new directory pwd0 provides all the meta-files to set up the waveform.

cd pwm0
sudo echo normal > polarity 
sudo echo 1 >enable 
sudo echo 20000000 > period 
sudo echo 1500000 > duty_cycle 

Now if you connect an oscilloscope to the pin PWM0 (U13 pin 18) you'll see a square wave, period 20ms and a pulse width of 1.5ms.


This is exactly the signal needed to keep a servo motor in its central position (or stop a rotational 360 degree servo motor).

If you have a servo motor you can plug it (using a separate power supply, as power spikes from a motor can cause CHIP to reset and reboot):

Servo black wire <-> power supply - <-> CHIP ground (e.g.U13 pin 1)
Servo white wire <-> CHIP PWM0 (U13 pin 18)
Servo red wire <-> power supply +

Now it is possible to change the pulse width, for example:

sudo echo 1400000 > duty_cycle 
sudo echo 1600000 > duty_cycle 
sudo echo 1000000 > duty_cycle 
sudo echo 2000000 > duty_cycle 
sudo echo 1500000 > duty_cycle

move the servo slighly counterclockwise, slightly clockwise, completely counterclockwise (-90 degrees), completely clockwise (90 degree) and back in its central position, respectively. (if the servo is a rotational one, rotates slowly backwards, slowly forward, fullspeed backwards, fullspeed forward,and stop again).

For other applications it is possible to set different values to period, duty_cycle, polarity.

At the end disable pwm0 in this way:

sudo echo 0 > enable

and unexport it:

sudo echo 0 > /sys/class/pwm/pwmchip0/unexport