Stepper motor connected to the arduino

Playing with a stepper motor that I found on the dump (actually, it controlled the hot water valve in our former heating unit, the gear box assembly was removed, but the stepper motor still works fine)

The schematics for controlling a stepper motor with a H-bridge is like this:

The only thing I modified was to use the enable lines of the L298N driver IC, and to pull them high whenever you need to run the motor. If you leave them high all the time then there is a continuous current even if you don’t run the motor, it is this current that unnecessarily heats up the 12V regulator. The library to use is in the arduino examples and works fine. Not a very complicated project so far, so I modified it into something else.

Embedding a stepper motor in a toy project

So, lets do something more complicated, go to the Meccano box and build something that is controlled by the stepper motor, and that generates a feedback signal so that the project gets more “brains”. Motor control projects without a feedback mechanism are usually no fun. A robot should for instance drive to a wall, think about what happened, and then turn around do to something else. What I came up with was a toy crane where the problem is to hoist a load until it reaches the table height, and then to lower it so that it gently reaches the floor, but that it does not touch it. So here it is, the crane project where I count with a detector (an EE-SY310 from lowpowerlabs) the number of turns of the hoist wheel. By doing it this way you avoid the problem of hysteresis that is caused from the pulley that runs from the stepper motor axis to the hoist mechanism. The project could have used a timing belt and a gear box for the stepper motor, but I didn’t have them at hand so that I ended up with this solution.

All instructions are in the project video, the arduino code is actually rather short, and it looks like this:

#include <Stepper.h>

const int SPR = 48;  // change this to fit the number of steps per revolution for your motor                              ;
const int led = 13;
const int motor1 = 3;
const int motor2 = 4; 
const int motor3 = 5;
const int motor4 = 6;
const int counter = 2;    // goes high when the white tab passes the sensorhead 
const int enable = 7;
const int maxturns = 6;  // when the counter reaches 6 we switch direction;
volatile int turns = 0;  // counts the turns of the hoist wheel of the crane
long int nsteps = SPR;

// initialize the stepper library 
Stepper myStepper(SPR, motor1, motor2, motor3, motor4 );            

void setup() {
  // initialize the on motor (it is standard in the Arduino libraries), tune the speed so that 
  // you dissipate the least amount of heat
  myStepper.setSpeed( SPR * 4 );  
  pinMode(enable,OUTPUT);  // Enables the motor driver
  pinMode(counter,INPUT);  // output of the optical counter
  pinMode(led,OUTPUT);     // we love that LED for debugging
  turns = 0;
  // so, when the detector sees the white mark and interrupt is generated on port 2, (this is 
  // an int.0, so the first argument should be zero) the updatecounter function is then called
  attachInterrupt( 0, updatecounter, RISING ); 

void Blink(int length, int ntimes)
  for (int i=0; i<ntimes; i++) {

void updatecounter()
  turns = turns + 1;

void loop() {
  for (int j=0; j<2; j++) {  
    if (j==0) { 
      Blink(1000,1);               // one long blink means we are here
      while (turns < maxturns) {
        Blink(3,1);               // blips for every nstep counts (we need about 70 or so)                          
      myStepper.step(+nsteps*2);  // you want the mark to pass  
      digitalWrite(enable,LOW);   // so we turn a little further
      turns = 0;
    if (j==1) { 
      Blink(1000,2);              // two long blinks means we are here
      while (turns < maxturns) {
      turns = 0;   
      myStepper.step(-nsteps*2);  // you want the mark to pass 
      digitalWrite(enable,LOW);   // so we turn a little further

Last update: 24-dec-2014

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s