Stepper Motor Control using ATmega328p

Here we show how to interface and control Stepper Motor using ATmega328p microcontroller. The stepper motor code in C and video demonstration are also provided at the end. Microcontroller output signal(voltage & current) from its pins are not enough in strength to drive a stepper motor. Therefore we have to use stepper motor driver. Here we will use A4988 stepper motor driver which makes it easy to control the motor. We will be using Nema 17 stepper motor as it is very popular, frequently used in application like 3D printers and industrial application.

 

Atmega328p Nema17 stepper motor and A4988


 Materials & Components Required

We need the following material/components for this project. 

1. ATmega328p Microcontroller

2. A4988 stepper motor controller

3. Nema 17 stepper motor(or any other bipolar stepper motor)

4. Breadboard & connecting wires

5. 16MHz crystal(you may also use internal osciallator of the microcontroller and in this case you don't need a crystal oscillator)

6. 2x22pF capacitor

7. 10KOhm resistor

8. 100uF capacitor

 

Schematic Drawing & Implementation on Breadboard

After you have the required materials, connect the ATmega328p microcontroller, A4988, Nema 17 stepper motor and other parts as shown in the schematic(drawn in proteus professional software). To learn in details about A4988  breakout pins, interfacing to microcontroller, power supply connections read the previous tutorials- Stepper Motor control using ATmega32 or Arduino Stepper Motor Tutorial. Using A4988 makes it easy to control the motor because it just needs two signals- step and direction and also because the stepper motor driver circuit will be easier to implement.

But some points needs to be said here. 

  • The PD7 and PD6 of the ATmega328p are connected to DIR and STEP pins of A4988
  • ENABLE pin of A4988 is unconnected and is high 
  • MS3, MS2 and MS1 of A4988 driver are unconnected for Full Step resolution
  • RESET and SLEEP pins of A4988 driver are tied together, they are in HIGH state
  • 100uF capacitor is connected across the VMOT and GND of A4988 driver

 

atmega328p a4988 stepper motor schematic diagram

See the pinout of A4988 breakout below.
 

 

On a breadboard, the above schematic applied would look like the following.

stepper motor control using ATmega328p and A4988

Stepper Motor Control Code in C

 Below is the C code for stepper motor control using atmega328p. It is a very simple generic stepper motor control in C code program which can be adjusted/modified and used for other stepper motor control using A4988 driver and atmega328p based projects. That is, no complex stepper library is used which often hides internal working mechanism making it difficult for beginners to understand(like me).

 


#ifndef F_CPU
#define F_CPU 16000000UL
#endif

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

#define dirPin (1<<PD7)
#define stepPin (1<<PD6)

void rotate_cw(void);
void rotate_acw(void);

int main()
 { 
   // setup dirPin and stepPin as output
    DDRD |= dirPin | stepPin;

   while (1){
        
        rotate_cw();
        _delay_ms(1000);
        rotate_acw();
        _delay_ms(1000);
        
    }
   return 0;
 }

void rotate_cw(void){
    unsigned char i;
    //send High pulse for clockwise direction
    PORTD |= dirPin;
    //send 200 pulses to rotate One full cycle
    for(i=0; i<200; i++){
        PORTD |= stepPin;    
        _delay_ms(50);
        PORTD &= ~stepPin;
        _delay_ms(50);
    }
}

void rotate_acw(void){
unsigned char i;
    //send low pulse for anti-clockwise direction
PORTD &= ~dirPin;
    //send 200 pulses to rotate One full cycle
    for(i=0; i<200; i++){
        PORTD |= stepPin;    
        _delay_ms(50);
        PORTD &= ~stepPin;
        _delay_ms(50);
    }
} 

In the above ATmega328p program for controlling the Stepper motor, we have made two functions called rotate_cw() and rotate_acw() which will rotate the motor clockwise and anti-clockwise One Complete Rotation repectively. The direction of rotation is controlled by sending HIGH signal for clockwise rotation and LOW signal for anti-clockwise rotation to the DIR pin of the A4988 driver. For One complete rotation(either clockwise or anti-clockwise) we send 200 pulses because the Nema 17 stepper motor has one step angle of  1.8 degree. Thus with 200 pulses, we get 360 degree rotation(200x1.8 degree) and we have used full step mode.

In the main function, we first define PD7 and PD6 pins of ATmega328p as dirPin and stepPin for easier programming. Then in the while loop we call the functions rotate_cw() and rotate_acw() to rotate the motor clockwise once and again anticlockwise once and do this repeatedly. 


Video Demonstration of Stepper Motor Control using ATmega328


Following are other stepper motor tutorials:

- Stepper Motor Control with Motor Shield and Arduino

- Stepper Motor control using ATmega32

- Arduino Stepper Motor Control using L298N

- How to control Stepper Motor using ATmega32 and L293D IC

Post a Comment

Previous Post Next Post