Skip to main content

PWM output

PWM output

PWM 0-13 channels are available on 2.54 mm header pins numbered 1-14 accordingly. Servos can be controlled by setting the correct frequency (50hz is a common frequency) and duty cycle corresponding to the length of a pulse (usually between 1 and 2 milliseconds).

To try to control servo, connect the servo to the Navio2’s output channel number 1 and run the provided example. Do not forget to supply power to the servo rail.

If you have not already done that, download Navio2 drivers and examples code here.

C++

note

If you have not build the examples yet, perform the following commands:

cd C++
make

To check the PWM output example, you need to run this command:

cd Build
sudo ./Servo

Python

Move to the folder with the code and run the example:

cd Python
sudo python Servo.py

For further information, see cource code. Note that set_period function sets the period for PWM_OUTPUT channel depending on frequency value, which passed as a second parameter to the function.

To set the pulse range appropriate for your servo, you can change the SERVO_MIN and SERVO_MAX values.

note

Kernel driver for Navio2 that generates PWM needs to be fed with data at least every 100 ms. So it is necessary to update the value in set_duty_cycle every 100 ms or less to make PWM output works.

GPIO support

The servo rail pins also might be operated as GPIOs.

You can use /sys/class/gpio/gpiochip500 to control pins on the servo rail by:

echo N > /sys/class/gpio/export

Where N equals 500 + header pin number - 1. So, in order to control the second pin on the rail, you need to use echo 501.

Then, you can write and read values.