Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Speed and Direction Control of Stepper Motor using AVR Microcontroller

By Ashutosh Bhatt April 21, 2008

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. 
 
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.
 
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:
 
. 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.
 
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.
[header = 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
 
[header = 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

 

Project Source Code

###



//Program to 


/*

 *Source Code of Buck-Boost Converter with Input voltage = 12V and Output voltage = 5V nad 18V

 *The code has been tested on Atmega328  

 *This code will generate a PWM (Pulse Width Modulation)signal of 200kHz with 60% duty cycle
 and 30% duty cycle.

  As in our experiment we are using a P-channel MOSFET, PMOS is triggered by low voltage.

  So we need to set duty cycle of PWM signal to 40% and 70% so that duty cycle of the MOSFET 
can be 60% and 30% 

/*


/* 

* IN/OUT port connection

portB1(pin 9) set as output pin- To IR2110 Lin pin

portD3(Pin 3) set as output pin - Switch

*/


#define TOP   79                         // Fosc = Fclk/(N*(1+TOP), Fosc = 200kHz, Fclk = 16MHz

#define DUTY_CYCLE_BOOST  32                     // OCR1A value for Boost mode - 40% duty cycle 

#define PWM 9                            // PWM(Pulse Width Modulation) wave at pin 9 

#define Mode_select_switch 3                         // switch at pin 3


void setup() {

// put your setup code here, to run once:

  pinMode(PWM,OUTPUT);                        // set 9 pin as output       

  pinMode(Mode_select_switch,OUTPUT);         // set 3 pin as output       

  TCCR1A = 0;                                 //reset the register

  TCCR1B = 0;                                 //reset the register

  TCNT1 = 0;                                   //reset the register

  TCCR1A |= (1<<COM1A1);                        // set output at non inverting mode

  TCCR1A |= (1<<WGM11);                        // selecting Fast PWM mode

  ICR1 = TOP;                                   // setting frequency to 20KHz

  TCCR1B |= (1<< CS10)|(1<<WGM12)|(1<<WGM13);     //Timer Starts

  OCR1A = DUTY_CYCLE_BOOST;                  // setting PWM to 60% duty cycle

  digitalWrite(Mode_select_switch,HIGH);        //  Pull up the pin 3 

  delay(1000);                                  // delay for compensating the hardware transient time
 with software

}


void loop() {

  // put your main code here, to run repeatedly:

int Read_switch = digitalRead(Mode_select_switch);   // reading the analog voltage at the pin 3

  if( Read_switch == 0)                               // if pin 3 is low then select the BUCK MODE

  {

    OCR1A = 56;                                       // set duty cycle to 70%

  }

  else

  OCR1A = 32;                      // if pin 3 is high then converter remains in BOOST MODE and
 set duty cycle to 40%

}


###

Circuit Diagrams

circuit_0_0


Filed Under: Electronic Projects

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Finding past posts on edaboard?
  • I think i have devised a new type of "super_transformer" for the Electricity grid?
  • Industrial Relay Board Design for Motorcycle Use
  • sequence detector FSM design
  • Need suggestions in task NI6363 retrigger (analog trigger)

RSS Electro-Tech-Online.com Discussions

  • Sump pit water alarm - Kicad 9
  • Pic18f25q10 osccon1 settings swordfish basic
  • Anyone jumped from Easyeda std to Easyeda pro?
  • turbo jet fan - feedback appreciated.
  • More fun with ws2812 this time XC8 and CLC

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How IoT network topologies work
  • The top five AI startups to watch in 2025
  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe