Example Code for Intel Joule-Servo Control
Last revision 2026/01/24
This article offers example code for controlling a servo motor with Intel Joule using JS+MRAA, focusing on mapping angles to duty cycles.
Sample Code
JS+MRAA config PWM26 for servo, map angle to duty cycle periodically.
var Servo_pin = 26;//Initialize PWM on Digital Pin #26 (D26) and enable the pwm pin
var PWM_period_us = 20000;
var Min_Duty_Cycle = 0.029;
var Max_Duty_Cycle = 0.087;
var mraa = require("mraa"); //require mraa
console.log('MRAA Version: ' mraa.getVersion()); //get the MRAA version
var pwm = new mraa.Pwm(Servo_pin);
pwm.enable(false);
pwm.period_us(PWM_period_us);
pwm.enable(true);
var servoState = true; //Boolean to hold the state of Led
function periodicActivity()
{
moveServo(servoState?80:100); //move the servo to 80 or 100 degree
servoState = !servoState;
setTimeout(periodicActivity,1000);
}
periodicActivity(); //call the periodicActivity function
function moveServo(degree) {
var processedValue = MapRange(degree,0,180,Min_Duty_Cycle,Max_Duty_Cycle);
pwm.write(processedValue); //Write duty cycle value.
}
function MapRange (in_vaule, in_min, in_max, out_min, out_max) {
var output = (in_vaule - in_min) * (out_max - out_min) / (in_max - in_min) out_min;
if (output >= out_max) {
output = out_max;
} else {
if (output <= out_min) {
output = out_min;
}
}
return output
}
Result
Control the Servo through PWM pin 0 to move to 80 or 100 degree.
Was this article helpful?
