Control stepper motors with Raspberry Pi and node.js

Continuing the journey on how to control things with node.js on the Raspberry Pi I set up 2 stepper motors and controlled them in real time with Hydna. You can can read futher on how to install Hydna on Raspberry Pi here.

The to pins that controls the motors in this diagram are pin number 16(GPIO 23) and pin number 18(GPIO 24). Pin number 18 controls the direction of the motor and pin number 16 the speed. With 1K ohm resistors connected to the ground for each of these pins we connect them to the Easy driver.

Control stepper motors with Raspberry Pi and node.js.jpgDepending on the stepper motor you have, you will need to check the bipolar stepper motor wiring diagram for your motor. The motors I used had the following diagram where A(black) and C(green) connects to the A pins and B(red) and D(blue) connects to B pins.

Install pi-gpio and go ahead with this script to control the stepper motors:

var gpio=require(“pi-gpio”);stopMotors = false;

function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() start) > milliseconds){      break;    }  }},“output”,function(err){
console.log(“Pin 16 open”); // Opens pin 16 as output   });

Control stepper motors with Raspberry Pi and node.js.Schematic,“output”,function(err){       console.log(“Pin 18 open”); // Opens pin 18 as output   });// Runs motor in the set direction
function move() {
gpio.write(16, 1, function() {         sleep(1000);  gpio.write(16, 0, function() {   sleep(1000);     if(!stopMotors)move(); });}); }

function stopMotor() {
stopMotors = true;}// Changing direction of motorfunction left() {  stopMotors = false;
gpio.write(18, 1, function() {       move();  });  }// Changing direction of motorfunction right() {
stopMotors = false;  gpio.write(18, 0, function() {       move();  });   }

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation, and engineering. I have a clear and simple writing style and am skilled in using infographics and diagrams. I am a great researcher and is able to present information in a well-organized and logical manner.

Scroll to Top