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

Stepper Motor Angle Control using AVR Microcontroller

By Ashutosh Bhatt April 21, 2008

 

There are many applications in which it is required to set the position of an object at a desire angle. Some of the examples are

1.      Satellite Dish Antenna positioning 

The Satellite Dish Antenna should be in straight alignment with Satellite in Space to receive signal with maximum strength. So it’s required to position the Dish Antenna at a certain angle

2.      Aileron, Elevator and Rudder positioning in Aircraft

In an Aircraft it is required to position Aileron, Elevator and Rudder at a desired angle to increase or decrease the lift, to change direction and to increase or decrease the speed

3.      Rudder Positioning in Ship

To move ship in a specific direction it is required to set an angle of Rudder located in front of the Propeller

4.      Tank Gun or Howitzer Positioning

To hit the bull’s eye (Target) the Tank Gun (or Howitzer) must be positioned to specific angle 

 

This project demonstrates how any device or object can be positioned to desired angle. The circuit presented here demonstrates how to position Stepper Motor at a specific Angle using AVR microcontroller. The desired Angle Position is entered by user and when He presses the button to rotate motor, the motor starts rotating and rotates till it reaches that angle. The angle can be entered in step of 15o between 0o to 360o. User can increment or decrement angle value in step of 15o and set the desire angle. Based on set angle motor rotates forward (CCW) or reverse (CW). Like if current motor angle is 60o and user enters 90o then motor rotates CCW and if user enters 30o motor rotates CW.

 

System Circuit Diagram

As shown in figure the circuit is build using AVR microcontroller ATMega32, Current Driver Chip ULN2003A, LCD and few push buttons. 3 push buttons (SW1, SW2 and SW3) are connected to PORTA pins PA0 – PA2 such that when any buttons is pressed logic 0 is given as input to that pin. Normally all 3 pins are given logic 1 by connecting them to Vcc through pull-up resistors. PORTB pins PB0 – PB3 are connected to input pins of ULN2003A. These pins drive stepper motor. The output of ULN drives four coils of unipolar stepper motor. The common terminal of motor coils and ULN chip is connected to Vcc. PORTC drives data pins of LCD and two LCD control pins RS and En are driven by PORTD pins PD0 and PD1 respectively. Control pin RW is connected to ground. A 1K pot is connected to brightness control pin Vee (3) as shown. It can be used to adjust LCD brightness. A 16 MHz crystal is connected to crystal input pins of ATMega32 along with two 22pf capacitors as shown.

 

System Operation

·         When circuit is switched on the LCD displays set angle as 0 degree. The motor also rests at 0 degree

·         By pressing SW1 the angle can be increased in step of 15 degree. Similarly to decrease angle press SW2.

·         After selecting desire angle when SW3 is pressed, the stepper motor rotates till desire angle and waits for next angle value

·         The angle value can be increased or decreased by pressing SW1 or SW2

·         If new selected angle value is less than previous angle value then motor rotate reverse. E.g. if motor is at an angle 75o and now new set angle value is 30o then motor rotates reverse

·         But if new selected angle value is higher, then motor rotates forward

Complete Circuit Operation is due to the software program embedded into AVR microcontroller. First let us see the logic behind the program.

Software Logic & Program

Software Logic

The software logic is based on how to rotate stepper motor at set angle every time. Motor rotation directly depends upon its step resolution. The motor step resolution is 3.75o/ pulse. That means for each pulse applied – motor rotates for 3.75o only. But it is required to give 4 pulses in sequence to all four motor coils. So motor will rotate 3.75o × 4 = 15o. Thus we can rotate motor in multiples of 15o like 30o, 45o, 60o, 75o ……

 The logic is to find out how many number of pulses should be applied to motor so that it can rotate till desire angle value. So to do this, every time when user selects rotation angle, the number of pulses to be applied to motor is found from the equation given below

Number of pulses = 4 × (new set angle value – previous set angle value) / 15

e.g. lets take previously set angle value was 45o and new set angle value is 75o. That means actually the motor has to rotate for 30o. Putting values in above equation

Number of pulses =  4 × (75-45) / 15 = 8

So motor rotates for 8 × 3.75o = 30o

If new set angle value is lesser than previous set angle value then subtraction will be reversed and motor also rotates in reverse (CW) direction.

 

Software Program

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

Senddata() function sends 1 byte data to LCD on its data bus

sendcommand() function sends 1 byte command to LCD on its data bus

printstr() function displays message or string on LCD

inc_angle() function increments angle by 15o and displays on LCD

dec_angle() function decrements angle by 15o and displays on LCD

display_value() function takes integer angle value as input. Convert its all digits into ASCII and displays it as angle value on LCD

lcd_init() function configures and initializes LCD

rotate_motor function rotates stepper motor CW or CCW as per entered angle value till motor reaches that angle position. Please refer the complete program in the code with necessary comments.

Project Source Code

###

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

//////////////// variable declarations////////////////////////////

unsigned int angle=0,new_angle,set_angle=0,num_of_pulses, rotate_reverse_flag=0;

///////////// function to send data to LCD//////////////////////////

void senddata(unsigned char data)
{
  _delay_ms(2);   // wait for LCD to be ready
PORTD=(1<<PD0);   // for data rs=1
PORTC=data;    // put data byte on data-bus
PORTD=(1<<PD0)|(1<<PD1); // give strobe to En pin
PORTD=(1<<PD0)|(0<<PD1);
}

//////////////////// function to send command to LCD////////////////

void sendcmd(unsigned char cmd)
{
_delay_ms(2);   // wait for LCD to be ready
PORTD=(0<<PD0);   // for data rs=0
PORTC=cmd;    // put command byte on data-bus
PORTD=(1<<PD1);   // give strobe to En pin
PORTD=(0<<PD1);
}

///////////// function to send string to LCD //////////////////////
void printstr(unsigned char *s)
{
uint8_t l,i;
l = strlen(s);   // get the length of string
for(i=0;i<l;i++)
{
  senddata(*s);  // write every char one by one
  s++; 
}
}

///////////////// function to display angle value on LCD///////////////

void display_value(unsigned int t)
  {
   unsigned int t1,a;
unsigned char asci[3];
if(t>=100)    // for angle in 3 digit
      {
    a=2;
    while(t>=10)  // separate all 3 digits
    { 
      t1=t%10;  // one by one
      asci[a]=t1+0x30; // convert them into ASCII
      t=t/10;  // store them in array
      a--;
      }
    asci[0]=t+0x30;  // first digit of angle
      }
  else     // for angle in2 digits
      {
    t1=t%10;   // separate both digits
    asci[2]=t1+0x30;  // convert them in to ASCII
    t=t/10;   
    asci[1]=t+0x30;
    asci[0]=0x20;  // take first digit as space
      }
sendcmd(0xC0);
senddata(asci[0]);  // send all digits one by one to
  senddata(asci[1]);  // LCD
  senddata(asci[2]);
printstr(" deg "); 
  }

//////////////// function to initialize LCD /////////////////////////
void lcd_init()
  {
    sendcmd(0x3E);   // configure LCD dot pattern
  sendcmd(0x0E);   // LCD screen on cursor on
  sendcmd(0x01);   // clear LCD memory and home cursor
  sendcmd(0x80);   // set cursor to 1st line 1st column
  printstr("Set Motor Angle:");  // display message
  }

//////////////// function to increase angle by 15 //////////////////
void inc_angle()
  {
   if(angle<345) angle+=15; // increase angle till 345 degree
display_value(angle);  // display new angle value
new_angle = angle;  // update new angle value
  }

////////////// function to decrease angle by 15 ////////////////
void dec_angle()
  {
   if(angle>0) angle-=15;  // decrease angle till 0 degree
display_value(angle);  // display new angle value
new_angle = angle;  // update new angle value
  } 

///////////////// function to rotate motor till desire angle //////////
void rotate_motor()
  {
   int i;
if(new_angle>set_angle)  // if new entered angle is larger
   {
  num_of_pulses = (new_angle-set_angle) / 15; // set number of
  rotate_reverse_flag=0; // pulses and clear flag to rotate
      // motor CCW
   }
else     // if new entered angle is smaller
   {
  num_of_pulses = (set_angle-new_angle) / 30; // set number
  rotate_reverse_flag=1; // of pulses and set flag to rotate
      // motor CW
   }
if(rotate_reverse_flag==0) // check if flag is clear
   {
    for(i=0;i<num_of_pulses;i++) // generate pulse sequence to
    {     // rotate motor CCW
     PORTB = 0x01;
   _delay_ms(100);
   PORTB = 0x02;
   _delay_ms(100);
   PORTB = 0x04;
   _delay_ms(100);
   PORTB = 0x08;
   _delay_ms(100);  
    } 
   }
  else      // else if flag is set
   {
    for(i=0;i<num_of_pulses;i++) // generate pulse sequence to
    {
     PORTB = 0x08;  // rotate motor CW
   _delay_ms(100);
   PORTB = 0x04;
   _delay_ms(100);
   PORTB = 0x02;
   _delay_ms(100);
   PORTB = 0x01;
   _delay_ms(100);
    } 
   }  
   set_angle = new_angle;  // save angle value

  }

/////////////////// main function starts here /////////////////////////
int main(void)
{
DDRC=0xFF;    // PORTB, PORTC and PORTD as
DDRD=0xFF;    // an output ports
DDRB=0x0F;
DDRA=0x00;    // PORT A as an input port
PORTC=0x00;
PORTD=0x00;
PORTB=0x0F;
lcd_init();    // initialize LCD
display_value(angle);  // display angle as 0 degree
loop:while(PINA==0xF0);  // loop until no button is pressed
switch(PINA)   // when button is pressed
   {
   case 0xF1:   // if 1st button is pressed
   inc_angle(); // increment angle by 15 degree
   _delay_ms(200); // key debounce delay
   break;  // get out of loop
  case 0xF2:   // same as above
   dec_angle();
   _delay_ms(200);   
   break;
  case 0xF4:   // for 3rd key
   rotate_motor(); // rotate motor   
   break;   
   }       
      goto loop;    // continuous loop
}

###

 


Circuit Diagrams

Circuit-Diagram-AVR-ATMega32-Based-Stepper-Motor-Angle-Control

Project Video


Filed Under: Electronic Projects
Tagged With: atmega32, avr, stepper motor, ULN2003A
 

Next Article

← Previous Article
Next Article →

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



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

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

  • How to preserve hierarchical instance names like \adder_1/U1 in flattened gate-level netlist in Design Compiler?
  • Can anyone provide a guide or tutorial for Candece simulation?
  • Voltage mode pushpull is a nonsense SMPS?
  • High Side current sensing
  • MOSFET thermal noise in Weak vs Strong inversion

RSS Electro-Tech-Online.com Discussions

  • Photo interrupter Connections
  • Is AI making embedded software developers more productive?
  • Can I make two inputs from one??
  • Why can't I breadboard this oscillator?
  • Cataract Lens Options?

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • GigaDevice launches GD32C231 MCU series with 48MHz Cortex-M23 core and 64KB Flash
  • Advanced Energy releases 425 W CF-rated medical power supply in 3.5 x 6 x 1.5-inch format”
  • LEM combines shunt and Hall effect sensing in 2000 A current measurement unit
  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC

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