Home / Tutorials / BeagleBone Tutorial / Controlling a Servo Motor with BoneScript

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:

var b = require('bonescript');
b.pinMode('P9_14', b.OUTPUT);
b.analogWrite('P9_14', 0.7, 2000.0, printStatus);
function printStatus(x) {
    console.log('value = ' + x.value);
}

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 pulse width for its arm to be in the center position. A 1.5 ms pulse width on a 500 Hz frequency is around 75% of the duty cycle. Thus, this script will position the arm in the center position:

var b = require('bonescript');
b.pinMode('P9_14', b.OUTPUT);
b.analogWrite('P9_14', 0.75, 500.0, printStatus);
function printStatus(x) {
    console.log('x.value = ' + x.value)
}

Beaglebone Black Servo

Here’s a script that “sweeps” the servomotor arm to and from its two extreme positions:

var b = require('bonescript');
var increment = 0.1;

b.pinMode("P9_14", b.OUTPUT);
updatePos();

function loop() {
    b.analogWrite("P9_14", duty_cycle, 500, updatePos); 
}

function updatePos() {
    duty_cycle = duty_cycle + increment;
    if(duty_cycle < 0.5) {
        duty_cycle = 0.5;
        increment = -increment;
    } else if(duty_cycle > 1) {
        duty_cycle = 1;
        increment = -increment;
    }   
    // loop every 200ms
    setTimeout(loop, 200);
}

About Roland Pelayo

Roland Pelayo started TMM in 2015. He is a firmware engineer who has over ten years of experience in developing electronic and microcontroller-based systems. Roland's designs include medical devices, security and automation, robots, emergency alert systems, and educational training modules.   Have something that you like Roland to write about here? or do you need consultation for microcontroller firmware projects? just contact him via the contact page.

Check Also

beaglebone black adc

Analog Signals with BeagleBone BoneScript

Using the Beaglebone Black ADC One of the advantages of the Beaglebone Black over the …