Introduction

A stepper motor is a component which transform an electric pulse to an angular movement.

We can find 3 types of motor:

variable reluctance motor
permanent magnet motor
hybrid motor, which is a combination of the two previous technologies

For this course, we will use a permanent magnet motor -> 28BYJ-48

The Stepper motors is not used for accurate angular position such as printer, scanner and hard drive…

Nevertheless, the stepper motor has many advantages. They are cheap and easy to use. It can stay in the position and turn without limit and change direction. This rotation can be controlled precisely.

What is the difference between the direct current motor, stepper motor and servo-motor?

There are different types of motors: direct current motors, stepper motor or servo-motor which have different qualities. We will see what is the used of each motors.

The servo-motor is a motor which turn at 180 degrees. That’s mean this motor can’t complete a tour on itself. It won’t be adapted for a project in which the motor has to complete a tour on itself such as a helicopter or a car for example. Nevertheless with a servo-motor, you can choose a precise angle like a stepper motor.

The direct current motor can complete a tour on itself. It will be useful to a helicopter project, or to move a car forward. Nevertheless you can’t control a direct current motor with an angular position. This motor is not useful for project that needs accuracy.

The stepper motor is the happy medium between the direct current motor and the servomotor. It has not the cons of the servo-motor, that mean it can complete a tour on itself. Furthermore it can be controlled with an accurate angle, not like a direct current motor. Nevertheless the stepper motor can’t run at high speed.

Which component to use?

In this course we will use the permanent magnet motor: 28BYJ-48.

This a 5V motor, that mean we don’t need an external power supply. Nevertheless some motors need a power supply of 12V.

The h bridge included with your stepper motor:

Use a stepper motor with a library

 

 

For the sketch, we can’t show it here but the two wires of power has be separate by two pins with the wire + close to the resistors and the wire – in the other side.

#include <Stepper.h> 
const int stepsPerRevolution = 2048; // To modify to correspond to the number of steps per rotation 
const int StepByMinute = 15; // You can the the number from 0 to 17. 0 is low and 17 is quick
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); // to initialize the pins of the stepper motor

void setup(){
    myStepper.setSpeed(StepByMinute);
}

void loop(){
    myStepper.step(stepsPerRevolution); // in a direction
    delay(500);
    myStepper.step(-stepsPerRevolution); // in the other direction
    delay(500);
}

A library exists to control the movement of the stepper motor: Stepper.h. But it’s also possible to control it without a library.

Use a stepper motor without a library

We will see now a program to use a stepper motor without a library. You have some commentaries in the program to see what you have to change.

In the program without library you have more line to add to your program to make it work.

#define IN1 8 //The define IN correspond  at the pins where you will plug your stepper motor
#define IN2 9
#define IN3 10
#define IN4 11

int Steps= 0; //  We initialize the variables for the direction of the motor, the number of steps ...
boolean Direction = true;
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=4095;
long time;

void setup(){
    Serial.begin(9600);
    pinMode(IN1, OUTPUT); // We initiliaze the pins used with the stepper motor.
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);
}

void loop() {
    while(steps_left>0){
        currentMillis= micros();
        if(currentMillis-last_time>=1000){
            stepper(1);
            time=time+micros()-last_time;
            last_time=micros();
            steps_left--;
        }
    }
    Serial.println(time);
    Serial.println("Wait...!");
    delay(2000);
    Direction=!Direction;
    steps_left=4095;
}

void stepper(int xw){
    for (int x=0;x<xw;x++){
        switch(Steps){
        
        case 0:
        digitalWrite(IN1, LOW); // We control the pins IN to turn on the motor
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW)
        digitalWrite(IN4, HIGH);
        break;
        case 1:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, HIGH);
        break;
        case 2:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, LOW);
        break;
        case 3:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, LOW);
        break;
        case 4:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        break;
        case 5:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        break;
        case 6:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        break;
        case 7:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3,
        LOW);
        digitalWrite(IN4, HIGH);
        break;
        default:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        break;
        }
        SetDirection();
    }
}

void SetDirection(){
    if(Direction==1){
        Steps++;
    }
    if(Direction==0){
        Steps--;
    }
    if(Steps>7){
        Steps=0;
    }
    if(Steps<0){
        Steps=7;
    }
}

For the sketch of the circuit, its the same circuit as the program with library.

Use a stepper motor with a motorshield card

We will see now how to control a stepper motor with a motorshield card.

This is the program to use the stepper motor without a library. In fact, the motorshield card doesn’t need the library to control the stepper motor.

void setup() {
    //We initialize the pins of the direction of the motor 
    pinMode(12, OUTPUT); 
    pinMode(13, OUTPUT); 
    
    //We initialize the pins for the motor
    pinMode(9, OUTPUT); //(disable)Channel A
    pinMode(8, OUTPUT); //(disable) Channel B
}

void loop(){
    digitalWrite(9, LOW); //ENABLE Channel A
    digitalWrite(8, HIGH); //DISABLE Channel B
    digitalWrite(12, HIGH); //Change the direction Channel A
    analogWrite(3, 255); //Turn on Channel A
    delay(5);
    digitalWrite(9, HIGH); //DISABLE Channel A
    digitalWrite(8, LOW); //ENABLE Channel B
    digitalWrite(13, LOW); //Change the direction Channel B
    analogWrite(11, 255); //Turn on Channel B
    delay(5);
    digitalWrite(9, LOW); //ENABLE Channel A
    digitalWrite(8, HIGH); //DISABLE Channel B
    digitalWrite(12, LOW); //Change the direction CH A
    analogWrite(3, 255); //Tourne Channel A
    delay(5);
    digitalWrite(9, HIGH); //DISABLE Channel A
    digitalWrite(8, LOW); //ENABLE Channel B
    digitalWrite(13, HIGH); //Change the direction Channel B
    analogWrite(11, 255); //Turn on Channel B
    delay(5);
}

 

 

 

This is the sketch of the circuit.

As you can see, you don’t need to use the red pin.

Control the speed of the stepper motor

1. Problem with voltage threshold

With the direct current motor, you can use a potentiometer to control its speed. Nevertheless it won’t be possible with the stepper motor because there is a h bridge that need a minimum of power to be turn on.


2. Control the speed in the program

One solution of the problem previously mentioned is to control the speed of the stepper motor in the program.

By resuming the first program, you have to modify one line:

const int  StepByMinutee = 15; //  You can change from 0 to 17.

You can change this number from 0 to 17.

0 is lowest speed and 17 is the quickest.