Exercise 8 - Connecting a stepper motor

1.      INTRODUCTION

A stepper motor is similar to DC engine but it divided into discrete angular rotations. To manage a stepper motor, you have to excite the coils with a determined sequence depending of the motor that you are using.

It is important to know that exist three types of stepper motor, attending to their physical characteristics:

·         Variable reluctance

·         Permanent magnet

·         Hybrid synchronous

At the same time, these types of stepper motor can have their coils connected on unipolar or bipolar form.

·         Unipolar: They have five or six wires to control the coils. Control a unipolar motor is easier than a bipolar motor. To control a unipolar stepper motor you have to energize the coils in order and the current always have the same direction.

·         Bipolar: Firstly, you can use a unipolar motor as a bipolar if you do not connect the common wire of coils. On the other hand, to control a bipolar stepper motor you need to use a trick, which consist on, polarize the coils in both direction changing the current direction, you can view the sequence on the following table:

STEP

Coil1/ Terminal1

Coil1/ Terminal2

Coil1/ Terminal1

Coil1/ Terminal2

1

+Vcc

-Vcc

 

 

Half

+Vcc

-Vcc

+Vcc

-Vcc

2

 

 

+Vcc

-Vcc

Half

-Vcc

+Vcc

+Vcc

-Vcc

3

-Vcc

+Vcc

 

 

Half

-Vcc

+Vcc

-Vcc

+Vcc

4

 

 

-Vcc

+Vcc

Half

+Vcc

-Vcc

-Vcc

+Vcc

Depending of the number of coils that you have the number of degrees that the stepper motor rotates on each step will be different. With more coils, the motor step rotates a lower number of degrees.


UNIPOLAR

BIPOLAR


2.      HOW TO CONNECT?

To control a bipolar motor you can use a H-Bridge to energize the outputs in the correct order. Firstly, connect H-bridge inputs to 9, 6, 5, 3 pins. PWM pins are used because the stepper motor on this example must be energized with 2V but the power supply is of 9V, using PWM pins you can adapt the voltage you need.

On the other hand, you have to connect the button to pin2 and the potentiometer to pin A0. You can view exercises of CONNECTING A BUTTON and CONNECTING A POTETIOMETER to obtain specific information.

Finally, connect the H-bridge to power supply and Vin and GND on Arduino to +5V and GND on H-bridge. It is important that the grounds of Arduino and power supply are connected.

EXERCISE A&B


3.      PROGRAMMING

To connect a stepper motor, you can use any digital pin because you can control it with digital signals, but in this case, we are using PWM pins to adapt the voltage. On the other hand, by using this method you can see better, how stepper motor works, than using only the library included on Arduino. However, if you have a Stepper motor that uses the same voltage that the power supply you can use this library normally.

This motor can do 200 steps on each full rotation ()

EXERCISE_A (1_ROTATION)

First, declare global variables.

float voltage = 2.5;

float value = 0;

int i = 50;

int button = 2;

int steps = 0;

 

On the setup, Serial communication is initialize.

void setup() {

  Serial.begin(9600);

 

}

 

On loop, the wanted voltage is converted to a valid PWM value. Furthermore, stepperVoltageControl() function is configured with the parameters.

 

void loop() {

  value = (voltage * 255) / 13;

  stepperVoltageControl(value, 9, 6, 5, 3);

 

}

 

This function does the sequence of energizing coils in correct order. On the other hand it reads Serial port to move the motor a determined number of degrees and it also read button to do a full rotation. The function do 2 steps and 2 half-steps. Because this “i” variable equals 50 instead 200.

 

void stepperVoltageControl(float value, int motorPin1, int motorPin2,

                           int motorPin3, int motorPin4) {

 //Find any character on Serial port  

 if (Serial.available() > 0) {//Recognize integers sent via serial

 

    steps = Serial.parseInt();

    i = 0;

  }

  if (digitalRead(button) == HIGH) {

    steps = 50;

    i = 0;

  }

  for (i ; i < steps; i++) {

    analogWrite(motorPin1, value);

    analogWrite(motorPin2, 0);

    analogWrite(motorPin3, value);

    analogWrite(motorPin4, 0);

    delay(20);

    analogWrite(motorPin1, 0);

    analogWrite(motorPin2, value);

    analogWrite(motorPin3, value);

    analogWrite(motorPin4, 0);

    delay(20);

    analogWrite(motorPin1, 0);

    analogWrite(motorPin2, value);

    analogWrite(motorPin3, 0);

    analogWrite(motorPin4, value);

    delay(20);

    analogWrite(motorPin1, value);

    analogWrite(motorPin2, 0);

    analogWrite(motorPin3, 0);

    analogWrite(motorPin4, value);

    delay(20);

  }

}

 

 

EXERCISE_B (VELOCITY_DIRECTION_CONTROL)

This exercise is similar to the previous, but in this case you can switch the direction of rotation and the velocity of it with a potentiometer. An anti-rebound function is implemented on this exercise.

The anti-rebound function variables are also declared as global variables.

float maxVoltage = 13;

float voltage = 2.5;

float value = 0;

int i = 50;

int buttonPin = 2;

int potPin = A0;

int steps = 0;

int ST;

//DEBOUNCE VARIABLES

int antiReboundTime = 100;

int counter = 0;  //Variable to increment the counter

boolean lastState = LOW;    // Last state of the button

boolean currentState = LOW; // Current state of the button

 

 

void setup() {

  Serial.begin(9600);

}

 

debounce() compares last state of the button with the current state, to avoid the rebound of the construction components of the button.

 

boolean debounce(boolean last) // Call anti-rebound function

{

  boolean current = digitalRead(buttonPin);

 

  if (last != current)

  {

    current = digitalRead(buttonPin);

  }

 

  return current;

}

 

On loop the potentiometer and the button are read. The button can change the counter value between 0 and 1 because when counter is higher than 1 counter changes its value to 0.

The button can change the direction of rotation and the potentiometer change the velocity of step time. Step time is a parameter added to stepper function (ST).

void loop() {

  //VOLTAGE CONVERSION

  value = (voltage * 255) / maxVoltage;

  //VELOCITY

  ST = analogRead(potPin);

  ST = map(ST,0,1023,10,300); //Adapts a range of values to another.

  Serial.println(ST);

  //DEBOUNCE / SET DIRECTION

  currentState = debounce(lastState); // Eliminates rebound

 

  if ( lastState == LOW && currentState == HIGH)

  {

    counter = counter + 1;           //Increments counter

    Serial.println(counter); //Prints the value on Serial monitor

  }

 

  lastState = currentState; // Update the state

 

  if (counter == 0) {

    stepperVoltageControlClockWise(value, ST, 9, 6, 5, 3);

  }

  else if (counter == 1) {

    stepperVoltageControlAntiClockWise(value, ST, 9, 6, 5, 3);

  }

  

  if (counter > 1) {

    counter = 0;

  }

  

}

 

Function to clockwise rotation.

 

void stepperVoltageControlClockWise(float value,int ST , int motorPin1, int motorPin2, int motorPin3, int motorPin4) {

 

  analogWrite(motorPin1, value);

  analogWrite(motorPin2, 0);

  analogWrite(motorPin3, value);

  analogWrite(motorPin4, 0);

  delay(ST);

  analogWrite(motorPin1, 0);

  analogWrite(motorPin2, value);

  analogWrite(motorPin3, value);

  analogWrite(motorPin4, 0);

  delay(ST);

  analogWrite(motorPin1, 0);

  analogWrite(motorPin2, value);

  analogWrite(motorPin3, 0);

  analogWrite(motorPin4, value);

  delay(ST);

  analogWrite(motorPin1, value);

  analogWrite(motorPin2, 0);

  analogWrite(motorPin3, 0);

  analogWrite(motorPin4, value);

  delay(ST);

}

 

Function to anti clockwise rotation. It uses an inverse sequence.

 

void stepperVoltageControlAntiClockWise(float value,int ST, int motorPin1, int motorPin2, int motorPin3, int motorPin4) {

 

  analogWrite(motorPin1, value);

  analogWrite(motorPin2, 0);

  analogWrite(motorPin3, 0);

  analogWrite(motorPin4, value);

  delay(ST);

  analogWrite(motorPin1, 0);

  analogWrite(motorPin2, value);

  analogWrite(motorPin3, 0);

  analogWrite(motorPin4, value);

  delay(ST);

  analogWrite(motorPin1, 0);

  analogWrite(motorPin2, value);

  analogWrite(motorPin3, value);

  analogWrite(motorPin4, 0);

  delay(ST);

  analogWrite(motorPin1, value);

  analogWrite(motorPin2, 0);

  analogWrite(motorPin3, value);

  analogWrite(motorPin4, 0);

  delay(ST);

}


Last modified: Tuesday, 21 January 2020, 9:35 AM