Speed and Direction Control of Stepper Motor using AVR Microcontroller

Stepper motor can be termed as digital motor because it operates on pulses. Unlike AC or DC motor that rotates continuously, stepper motor rotates in steps. It rotates in number of steps as per applied number of pulses. Stepper motor is used in many applications like:
Speed and Direction Control of Stepper Motor using AVR Microcontroller
. Robotic application – to move robotic arm to specific position, to move robo-vehicle in specific direction at specific angle
· Disk drive application (CD drive, HDD) – to position reading head at specific position· Defence application – to fire a shell or to launch missile at perfect angle
· Domestic application – to open or close window shutter / curtain / blinds etc
· Communication application – to lock the position of satellite dish antenna at specific angle

There are different types of stepper motors permanent magnet, variable reluctance, unipolar, bipolar etc. In this project I have used unipolar type stepper motor with following ratings
·  Max rated voltage: –    5 V
·  Max rated current per coil: – 0.5 Amp
·  Step resolution: –    1.8 degree / pulse
·  Max RPM: –    60In this project I am varying speed and altering rotating direction of motor. But to vary the speed or to alter the direction of motor one has to first understand how unipolar stepper motor rotates. So first let us understand how to rotate unipolar stepper motor
Speed and Direction Control of Stepper Motor using AVR Microcontroller schematic
In unipolar stepper motor there are four coils. There can be six terminals or five terminals. In six terminals stepper motor one end of two coils is shorted and taken out as common terminal. similarly for other two coils. These two common terminals are always connected to +Ve / -Ve terminal of supply. The remaining four terminals of each coil are used to rotate motor. In five terminals stepper motor one end of all four coils are shorted and taken out as single common terminal that is to be connected to +Ve / -Ve terminal.Now to rotate motor (clockwise or anticlockwise) pulses has to be applied to these four coil terminals in following sequence.

Step

clockwise

anticlockwise

Coil4

Coil3

Coil2

Coil1

Coil 4

Coil3

Coil2

Coil 1

Step1

0

0

0

1

1

0

0

0

Step2

0

0

1

0

0

1

0

0

Step3

0

1

0

0

0

0

1

0

Step4

1

0

0

0

0

0

0

1

Note – in the above sequence ‘1’ indicate the coils are given +Ve supply. The common terminal is connected to –Ve (Gnd) terminal.So as we apply this four steps sequence continuously, the motor will rotate clockwise or anticlockwise. Now to change the speed of motor we have to change pulse repetition frequency (PRF) that is the frequency of applied pulses. If PRF is increased the pulse duration decreases and speed increases and vice versa.  So this project demonstrates how to vary the speed and change the direction of given stepper motor using AVR microcontroller ATmega32. It alters pulse sequence to change the direction and varies pulse duration (PRF) to vary the speed of motor. So let us start with system block diagram.

 System Block Diagram

System block diagram: –

The major building blocks of this system are as shown in figure that are ATmega32 and steeper motor driver chip ULN2003A. Along with them we need some push buttons for controlling and LEDs for indications. Each block is described briefly here.

Push buttons – five push buttons are given to run / stop, to vary speed and change direction of motor

button

function

Button 1 Run motor in clockwise direction
Button 2 Run motor in anticlockwise direction
Button 3 Stop motor
Button 4 Increase speed
Button 5 Decrease speed
ULN2003A chip – this chip takes input from micro controller and drives steeper motor. The microcontroller output current is not enough to drive motor directly. The chip has 7 Darlington pair drivers. It will provide up to 500 mA current to motor that is enough to drive it.

LED indications – Four LEDs are used for different indications

LED

colour

indication

LED 3

Blue

Blinks when speed in increased

LED 4

Orange

Blinks when speed is decreased

LED 5

red

Lits ON when speed limit is maximum

LED 6

red

Lits ON when speed limit is minimum

AVR micro controller – ATmega32 is low power, high performance, 8-bit, Advance RISC architecture based microcontroller. It has 32 KB of in system programmable FLASH. Also it has built in peripherals like 10-bit, 8 channel ADC, 4 PWM channels, 1 16-bit and 2 8-bit timers/counters, USART, SPI, IIC etc. In this system it performs following tasks

· Scans push buttons continuously to get user input

· Give different indications on LEDs

· Run or stop steeper motor

· Change direction of motor

· Varies speed of motor

System circuit diagram: –

The circuit is made up of ATmega32, ULN2003A and few discrete components like LEDs, registers, capacitors, push buttons etc

· 5 push buttons SW1 – SW5 are connected to PORTA pins PA0 to PA4. Also all port pins are pulled high through external pull up resistors as shown

· 4 LEDs LED1 – LED4 are connected to PORTD pins PD0 to PD3 through current limiting resistors

· PORTC pins PC0 to PC3 are connected to 4 inputs of UNL2003A chip

· 4 terminals of stepper motor are connected to output of ULN2003A chip

· Two common terminals of motor are connected to 5 V Vcc supply

· A 16 MHZ crystal with two 22 pF capacitor is connected to crystal input pins XTAL1 & XTAL2

· Vcc supply of 5 V is connected to required pins of both chips and pull up resistors

 Working & Operations

System working and operation: –

· Initially motor is stop. All the LEDs are OFF

· When SW1 is pressed momentarily, the motor starts rotating in clockwise direction

· Now if SW4 or SW5 button is pressed, the motor speed will increase or decrease. It is indicated by blink of LED1 or LED2

· If anyone keep on pressing SW4 or SW5 then speed will keep on increasing or decreasing. When max or min limit is reached, the corresponding LED3 or LED5 will be ON

· While motor is running in clockwise direction if SW2 is pressed, the motor will immediately start rotating in anticlockwise direction and vice versa

· Pressing SW3 will stop running motor

Program: –

Program is written in C language. Its compiled and simulated using AVR studio software. Complete program is made up of different functions

· Keydly() function – this function generates key debounce delay of approx 0.2 sec that is required when key is pressed

· Incspeed() function – this function decreases delay applied between pulse (increases PRF) fed to motor by decreasing time of delay loop library function. It also checks for maximum speed limit by checking min time for loop. If maximum speed limit is reached it turns ON LED3

· decspeed() function – this function increases delay applied between pulse (decreases PRF) fed to motor by increasing time of delay loop library function. It also checks for minimum speed limit by checking max time for loop. If minimum speed limit is reached it turns ON LED4

· clockwise() function – it continuously gives sequences of pulses as shown in table 1 to motor to rotate it in clockwise direction till any other button is not pressed

· anticlockwise() function – it continuously gives sequence of pulses to motor to rotate it in anticlockwise direction till any button is not pressed

· main() function – it initializes ports as input or output and then it scans all the buttons in continuous loop.

Project Source Code

###
#include <avr/io.h>
#include <util/delay.h>
uint16_t t=50535;
// time delay value for delay loop
void keydly()
// key debounce delay
{
uint8_t i;
for(i=0;i<7;i++)

_delay_loop_2(10000);
// delay loop library function with fixed delay

}

void incspeed()
// increase speed of motor by

{

PORTB = 0x00;

if(t>20535) t=t-2000;
// decreasing time delay value if its min limit is

if(t==20535) PORTB = 0x01;
// not reached. For min limit give indication on LED3

}

void decspeed()
// decrease speed of motor by

{

PORTB = 0x00;

if(t<60535) t=t+2000;
// increasing time delay value if its max limit is not

if(t==60535) PORTB = 0x02;
// reached. For max limit give indication on LED4

}

void clockwise()
// rotate motor clockwise

{

while(PINA==0xFF)
// apply pulse sequence till no button is pressed

{

PORTC=0x01;
// apply pulse to 1st coil

_delay_loop_2(t);
// generate delay as per time delay value

PORTC=0x02;
// apply pulse to next coil and like wise

_delay_loop_2(t);

PORTC=0x04;

_delay_loop_2(t);

PORTC=0x08;

_delay_loop_2(t);

}

}

void anticlockwise()
// rotate motor anti clockwise

{

while(PINA==0x0F)
// apply pulse sequence till no button is pressed

{

PORTC=0x08;
// apply pulse to last coil

_delay_loop_2(t);
// give delay as per time delay value

PORTC=0x04;
// apply next pulses as per required sequence

_delay_loop_2(t);

PORTC=0x02;

_delay_loop_2(t);

PORTC=0x01;

_delay_loop_2(t);

}

}

int main(void)

{

uint8_t r=0,d;
// run and direction flag

DDRC=0xFF;
/ / initialize ports as input and output

DDRB=0x03;

DDRD=0x03;

DDRA=0x00;

PORTC=0x00;

PORTB=0x00;

PORTD=0x00;

while(PINA==0xFF);
// wait till no key is pressed

loop:switch(PINA)

{

case 0xFE:
// for 1st key

keydly();
// give key debounce delay

r=1;
// set run flag

d=0;
// clear direction flag for CLK direction

clockwise();
// start rotating motor clock wise

break;

case 0xFD:
// for 2nd key

keydly();
// key debounce delay

r=1;
// set run flag

d=1;
// set direction flag for ACLK direction

anticlockwise();
// start rotating motor anticlockwise
break;

case 0xFB:
// for 3rd key

PORTC = 0x00;
// stop motor

r=0;
// clear run flag

PORTB = 0x00;
// all indication off

break;

case 0xF7:
// for 4th key

PORTD=0x01;
// give indication on LED1

keydly();

PORTD=0x00;

incspeed();
// increase speed

if(r==1)
// if motor was running

{

if(d==0) clockwise();
// check direction and

else anticlockwise();
// keep it running

}

break;

case 0xEF:
// for 5th key

PORTD=0x02;
// indication on LED2

keydly();

PORTD=0x00;

decspeed();
// decrease speed

if(r==1)
// if motor was running

{

if(d==0) clockwise();
// check direction and

else anticlockwise();
// keep it running

}

break;

}

goto loop;
// continuous loop

}
###

Circuit Diagrams

Circuit-Diagram-AVR-ATMega32-Based-Stepper-Motor-Controller

Project Video

For more detail: Speed and Direction Control of Stepper Motor using AVR Microcontroller


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top