Learn to code

ARDUINO & CODES

Previous Post
Direction and speed of a DC Motor with an H-Bridge and Arduino
Next Post
Controlling DC motors with TB6612FNG breakout board for Arduino

Controlling a stepper motor with an H-Bridge and Arduino

in Arduino Motor Control

Stepper motors are motors that have multiple coils in them, so that they can be moved in small increments or steps. Stepper motors are typically either unipolar or bipolar, meaning that they have either one main power connection or two. Whether a stepper is unipolar or bipolar you can control it with an H-bridge. This tutorial shows you how to set up a unipolar stepper motor using an H-Bridge. You can use the same control circuit with a bipolar motor too.

The sketch that you will create steps the motor in response to serial commands. A numeric value followed by a + steps in one direction while a – symbol steps in the other direction. For example, 12+ steps a 12-step motor through one complete rotation in one direction, and a 6- steps half a rotation in the other direction.

Note to 3.3v microcontrollers

The H-Bridge that you will use in this tutorial is great if you are using an Arduino Uno or other microcontroller that operates at 5Volts. However, if you are using a board that operates on 3.3Volts you will not be able to use the H-Bridge that you will use in this tutorial. The problem is that the 3.3Volt boards do not supply enough logic voltage for the L293 H-Bridge. For these boards, you can use 3.3 compatabile boards breakout Boards.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Stepper Motor Stepper Motor × 1
Dupont Wires Dupont Wires × 20
H-Bridge × 1
DC Barrel Jack DC Barrel Jack × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

H-Bridge L293d

H-Bridges are a type of component known as integrated circuits (IC). ICs are components that hold large circuits in a tiny package. These can help simplify more complex circuits by placing them in an easily replaceable component. For example, the H-bridge you’re using in this example has a number of transistors built-in. To build the circuit inside the H-bridge you would probably need another breadboard.

Stepper Motor

Stepper motors are useful for when you need to rotate a full 360 degrees, but need to position your motor at a particular angle. In other words, you can turn the stepper motor by telling how much degrees it needs to turn. We call those degress “steps”.

How the Stepper Motor works

The stepper motor has two coils to control it as shown in Figure 13. Each coil has a center connection as well, and the center connections are joined together, which is what makes this a unipolar stepper. If you don’t connect the center connection, then the motor will work very much like a bipolar stepper, each coil operating independently. This is how you’ll use it for this exercise. Each coil will connect to one side of the H-bridge. The pink and orange wires (wires number 2 and 4) are connected tothe first coil. They will connect to one side of the bridge, while the yellow and blue wires (wires number 3 and 5) are the other coil, and will connect to the other side of the bridge. In this case, the red wire, pin 1, will not be used.

How the H-Bridge L293 works

So, to be able to use the H-Bridge L293d correctly, understanding of the component is essentiel. Below are all the pins that you can use on a H-Bridge.

Pin 1 is the enable pin for terminals 1 and 2. This enables the motor to turn on when it is connected to power.

Pin 2 is the motor logic pin for terminal 1. It will provide one voltage to one of the motor terminals.

Pin 3 is the pin where we connect one of the terminals of the motor to.

Pin 4 and Pin 5 both get grounded.

Pin 6 is where we connect the other motor terminal to. This completes the 2-terminal connections necessary for a motor to be hooked up to the H-bridge IC.

Pin 7 is the motor logic pin for terminal 2. This is the second voltage signal feed to the mtor.

Pin 8 is the pin which receives the voltage needed to power on the motor. This is the pin that is connected to the positve voltage supply pof the motor. Remeber to use the appropriate voltage supply. If you have a 9V DC Motor, then you will need to feed 9V into this pin. If the motor is 12V you will need to feed 12V in this pin.

Pin 9 is the enable pin for terminals 3 and 4. It enables the motor to turn on when connected to power and disables the motor when connected to ground.

Pin 10 is the motor logic pin for terminal 3. This is one of the logic voltage signals that determines the action that the second motor will take.

Pin 11 is the pin where we connect one of the terminals of the second motor to.

Pin 12 and Pin 13 both get grounded.

Pin 14 is the pin where we connect the other terminal of the second motor to.

Pin 15 is the motor logic pin for terminal 4. It is the second voltage signal that we feed into the second motor to determine the action the motor will take.

Pin 16 is the pin which receives the voltage needed for power for the IC. It is the IC power pin. The IC of the H-Bridge needs 5Volts to operate. Therefore, we feed 5V into this pin. This can easily be done if you are using an Arduino Uno Board.

 

BreadBoard Layout

BreadBoard Layout –  Attach 5V and ground to one side of your BreadBoard. You will use this to power the IC of the H-Bridge. Both of the rails will be 5V. For this tutorial, both enable pins are connected to 5V to keep them HIGH all the time so the bridge is constantly enabled. To supply the motor of voltage an external 12V DC power supply is used. Remember, we are using a 12V stepper motor. Check the datasheet of your steppermotor if the Voltage is 12V before continuing this tutorial.

Place the H-Bridge in the center of your BreadBoard. Connect pin1 of the H-Bridge to the positive side of the 5v rail on your breadboard. You are going to do this on both sides of the H-Bridge so that the H-Bridge is enabled at all times.

Pin 8,9,10 and 11 of the Arduino are used for the connection to the Stepper Motor.

Finally, each coil will connect to one side of the H-bridge. The pink and orange wires (wires number 2 and 4) are connected to the first coil. They will connect to one side of the bridge, while the yellow and blue wires (wires number 3 and 5) are the other coil, and will connect to the other side of the bridge. In this case, the red wire, pin 1, will not be used. So it works like a biploar stepper.

The H-Bridge will get its power from pin16, that pin is connected to the positive rail on your 5V rail on the BreadBoard. Pin4 and Pin5 are connected to the ground of the 5V rail.

Test Code

You can upload the code below to test your breadboard layout.

/* Test Code for stepper motor */
#include <Stepper.h>

const int stepsPerRevolution = 64;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one step:
  myStepper.step(1);
  Serial.print("steps:" );
  Serial.println(stepCount);
  stepCount++;
  delay(500);
}

 

The Code

/*
   Stepper_bipolar sketch

   the stepper is controlled from the serial port.
   a numeric value followed by '+' or '-' steps the motor

*/
#include <Stepper.h>
/const int stepsPerRevolution = 64;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int steps = 0;

void setup()
{
  // set the speed of the motor to 30 RPMs
  myStepper.setSpeed(30);
  Serial.begin(9600);
}
void loop()
{
  if ( Serial.available()) {
    char ch = Serial.read();
    if (ch >= '0' && ch <= '9') {
      steps = steps * 10 + ch - '0';
    }
    else if (ch == '+') {
      myStepper.step(steps);
      steps = 0;
    }
    else if (ch == '-') {
      myStepper.step(steps * -1);
      steps = 0;
    }
  }
}

 

Code Explanation

Import the Stepper Library

To be able to use the stepper motor you will use the stepper library. This wil make coding much easier. You will have to tell the Arduino how many steps the stepper motor can make for a 360 degrees turn. Furthermore, the pins are initialized that will drive the stepper motor. Since,the sketch will use the input from the Serial Monitor to turn the stepper motor we will need a variable that holds the input.

#include <Stepper.h>
/const int stepsPerRevolution = 64;  // change this to fit the number of steps per revolution
// for your motor
Begin Serial Communication and set the speed

In the void setup() part of the sketch, you will begin Serial Communication with your computer. You can also set the speed in which the stepper motor makes steps by using the command below. You can try adjusting the number to see the effect.

// set the speed of the motor to 30 RPMs
  myStepper.setSpeed(30);
  Serial.begin(9600);

 

Read the input of user ( 0 – 9 )

In the void(loop) part of the sketch you need to read the input from the Serial Monitor first. Based on the input of the Serial Monitor the steps are determined. The calculation ensures that the correct amount of steps are taken. After the number is enterd it is stored in the variable “steps”. Now you can determine the direction that the stepper motor should turn.

if ( Serial.available()) {
    char ch = Serial.read();
    if (ch >= '0' && ch <= '9') {
      steps = steps * 10 + ch - '0';

 

Read the input of user ( + / – )

If the user entered a minus or plus symbol into the Serial Monitor after entering a number the stepper motor will step a direction. After the steps are made the code sets the steps to 0.

else if (ch == '+') {
      myStepper.step(steps);
      steps = 0;
    }
    else if (ch == '-') {
      myStepper.step(steps * -1);
      steps = 0;
    }

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu