Home / Tutorials / BeagleBone Tutorial / Controlling a Servo Motor with BoneScript
beaglebone black adc

Controlling a Servo Motor with BoneScript

Using the Beaglebone Black's PWM

Bonescript also includes an analogWrite function which utilizes the 8 PWM pins of the Beaglebone Black. As you might already know, PWM allows us to control a servo motor. The analogWrite function accepts the pin name, duty cycle, frequency and callback function as parameters. The duty cycle value can be between 0 and 1 while the frequency is in Hertz. Here’s an example:

Here, the PWM pin used is P9_14, the duty cycle is 0.7 or 70% and the frequency is 2 kHz. The analogWrite function returns true if successful and false on failure. In the example above, x.value would have a value of “1” if the call to analogWrite is successful.

A servomotor needs a 1.5 millisecond pulsewidth for its arm to be on the center position. A 1.5 ms pulse width on a 500 Hz frequency is around 75% of duty cycle. Thus, this script will position the arm in the center position:

Beaglebone Black Servo

Here's a script that "sweeps" the servomotor arm to and from its two extreme positions:


Check Also

beaglebone black adc

Analog Signals with BeagleBone BoneScript

Using the Beaglebone Black ADC As I’ve mentioned, one of the advantages of the Beaglebone …