Stepper motors pairs nicely with a smart device like a microcontroller to create precise digitally controlled movements that has made possible many of modern gizmos around us. For example a printer, scanner, plotters, fax, floppy drive (not so modern though!), automatic industrial machines like CNC (Computer numerically controlled) drills, laser shows etc. Though to a naked eye the motor of stepper look no other than a DC motor but the difference is that each step of a stepper motor is in control. For example a high speed desktop printer when the paper moves forward, to a novice it seems like a motor is just pushing the paper out but in reality the control board inside the printer request the motor to move the paper exactly same amount that has been printed. This precise movement keeps the next printed pixel in alignment with previously printed pixels.

The thing is that the stepper motors have certain amount of steps per full 360 degree rotation (exact number depends on model) the controller can request the stepper to rotate any number of steps. For example if you are making a robot, you want it to move exactly as per your program. Like if you say go forward 100cm then rotate right 45 degrees and move forward 50 cm. You cannot do this with DC Motors, because for it you need to calculate the exact speed for DC motor and then use blind timing for movement. Say if you happen to find out the your robot moves at 5cm per second then to move 100cm it will require 20 second. So you keep the motor on for 20 second and expect that it has moved 100cm. But this can prove failure if the speed of robot changes due to drop in level of battery or some additional weight or simply due to an uneven terrain etc. So this method is not so trustworthy.

The second method is the use of stepper motors. Say you have a stepper motor of 7.5 degree per step (that means it move 7.5 degree for single step or their are 48 step in full rotation) and their is a gear reduction of ratio of 1:75 then you can control the stepper with an accuracy of 0.1 degree per steps! wow! that’s really precise! Now if we assume you have attached a wheel of radius 3.5 cm then you can control the liner motor of your robot with an accuracy of 0.00611 cm (I leave the math on you). That’s pretty decent. To move the motor forward 100cm, you just need to step the motor 100/0.00611 times that is 16,366 times. Instead if you step it 16,202 times it will move 99cm. So now the bot is pretty much in your control.

In the same way PCBs are drilled at accurate position, SMT components placed automatically at their desired location, pixel on a paper are printed. Below are some videos that may help you get the point.

 

Types of stepper motor

Their are many types of stepper motors available but the two most common types are

  • Unipolar Stepper Motor- Has simple driver requirement. Less torque at same size and weight as compared to bipolar type.
  • Bipolar Stepper Motor – Has slightly complicated driver requirement. More torque at same size and weight as compared to unipolar type.

Since the Unipolar type is simpler to drive we will start our journey with it.

Driving Stepper Motor with AVR MCU

In the figure below is shown a simplified construction of a stepper motor. The center is a permanent magnet(PM) rotor. Around it are four electromagnets. One end of all four electromagnet is connected to a point called "common". The common is usually connected to the stepper supply voltage (eg. 12v). The four coils are named A,B,C and D. To rotate a stepper motor, coils are excited in turns like A,B,C,D. The rotor will try to align itself with the currently exited coil. Lets say that the white point shown on rotor is magnetic north pole. Also assume that when coils are excited, their inner end becomes magnetic south. So the white point on rotor will try to align with the currently excited coil.(As the opposite poles attract)

Stepper Motor Contruction

Simplified Construction of Stepper Motor

So to drive a stepper motor from AVR MCU you just need to excite the coils A,B,C,D in turns to rotate the motor in anti clock wire direction. If you want to rotate the motor in clock wise direction simply excite the coil in reverse order that is D,C,B,A.

As the port of AVR can only sink or source 20mA current approximately, they cannot be used to drive the the coils directly. So we need some thing that can boost this current. The part that fit perfectly in this scenario is ULN2003A. It is a high voltage, high current Darlington array. It can be driven directly with a TTL level input and the output can source up to 500ma. Since it is array of seven darlington pair, (of which we require only four) it is much compact.

Stepper Motor Connection with ULN2003A

ULN2003A Powering a Stepper

In this example we will use PORTC to drive the stepper motor. So we connect PIN 1,2,3,4 of ULN with PORTC 0,1,2,3 of PORTC. The output is available on pin 16,15,14 and 13 of ULN IC. These are connected to the four coils A,B,C and D of stepper motor. The common of stepper motor is connected to 12v supply from the adaptor. The pin 8 of ULN2003 IC is connected to GND (common of system). The whole connection can be made on a small bread board like this.

Stepper motor uln2003a

ULN2003A Powering a Stepper

 

ULN on breadboard

ULN2003A on Breadboard

 

PORTC on Development Board

PORTC on Dev Board

 

PORTC Connected to ULN2003A

PORTC Connected to ULN2003A

 

Stepper motor connected to ULN2003A

Stepper Also connected.

 

GND PIN of ULN2003A

Ground PIN of ULN2003A

 

Ground pin on avr development board

GND Pin on Development Board

 

Common wires of stepper

Common Wires of stepper

 

Common of stepper connected to 12v

Common wires of stepper connected to 12v

 

Stepper Motor + AVR + Atmega16 + ULN2003A

The whole setup!

Stepper, ULN2003A, AVR ATmega16 on Devboard

avr-gcc source code for stepper motor

Here I will present a small and simple library to drive a single stepper motor. The library supports port configuration that means you can change in which PORT stepper is connected. Their are only three function in the library.

  • void StepperInit()
    • Must be called before calling any other stepper related function. This function configures the related PORT pins as output.
  • void StepperStepCCW()
    • Steps the stepper in counter clock wise direction.
  • void StepperStepCW()
    • Steps the stepper in clock wise direction.

The library is provide in two files. These two files must be copied to current project folder and added to the AVR Studio Project.

I am also providing a sample which demonstrate the use of above functions.


/******************************************************************************

Title:

   Demo program to show the use of simple stepper library.

Description:
   A unipolar stepper motor connected to PORTC0,1,2,3 via
   driver IC ULN2003A.

   The stepper should be a 15 degree per step with approx
   1:85 gear reduction.

   The ATmega16 MCU Must be clocked by a 16MHz crystal

   Fuse bits must be set as follow to disable JTAG and use
   external crystal.

   LOW FUSE    = 0xFF
   HIGH FUSE   = 0xC9

   For More information visit
   http://www.eXtremeElectronics.co.in
   (then search 'stepper motor')


Author:
   Avinash Gupta.

   avinash@eXtremeElectronics.co.in

Copyright:
   eXtreme Electronics, India 2008- 2011

Notice:
   No part of this work can be copied or published in electronic or
   printed form without proper permission from the Original Creators.

   ONLY INTENDED FOR EDUCATIONAL, HOBBY AND PERSONAL USE.
   COMMERCIAL USE IS STRICTLY PROHIBITED.


Disclaimer of Warranty.

   THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
   EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER

   PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER 
   EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
   THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. 
   SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY 

   SERVICING, REPAIR OR CORRECTION.

******************************************************************************/

#include <avr/io.h>
#include <util/delay.h>

#include "xstepper.h"

void main()
{

   //Initialize the stepper library
   StepperInit();

   _delay_loop_2(10000);

   while(1)
   {

      for(uint16_t i=0;i<24*85;i++)
      {
         StepperStepCW();     //Step Clock wise

         _delay_loop_2(10000);
      }

      for(uint16_t i=0;i<24*85;i++)
      {
         StepperStepCCW();    //Step Counter Clock wise

         _delay_loop_2(10000);
      }
   }

}



The sample code first runs the stepper full 360 degree in clock wise direction and then full 360 degree in counter clock wise direction. This process is repeated as long as the board is powered. The code can be compiled using WinAVR Compiler using the AVR Studio as front end. For more details on installing and using this tool please see the following tutorial.

Compile the above program using AVR Studio (compiler is avr-gcc). And finally
burn the program using any ISP
Programmer
to the ATmega16. The fuse bits must be set as
following to enable external crystal as clock source.

  • High Fuse = C9 (hex value)
  • Low fuse =FF (hex value)

After burning the HEX file to MCU, finally you are ready to power up the setup.

Videos for stepper motor control using AVR



 

Downloads

Things Required

Help Us!

We try to publish beginner friendly tutorials for latest subjects in embedded
system as fast as we can. If you like these tutorials and they have helped you
solve problems, please help us in return. You can donate any amount
as you like securely using a Credit or Debit Card or Paypal
.





We would be very thankful for your kind help.

Subscribe!

Don’t miss any article get them right in your inbox! Subscribe to our feeds powered by Feedburner.


Get New Articles Delivered To Your Inbox!

Email address:

Delivered by FeedBurner



By

Avinash Gupta

www.AvinashGupta.com

me@avinashgupta.com