Here it is shown how to control a stepper motor using ATmega32 microcontroller and A4988 stepper motor driver module. Here Nema 17 stepper motor is used for demonstration. The A4988 stepper driver makes it easy to control stepper motors like Nema 17. With A4988, only two signals called step and direction are required to control all aspects of the stepper motor. Also you don't need to worry about writing complex sequence of signal which is usually required for stepper motor.
Circuit Diagram for Interfacing ATmega32, A4988 and Nema 17 Stepper Motor
Below is wiring diagram between ATmega32, A4988 and Stepper Motor.
Shown below is the A4988 breakout module pin diagram.
Interfacing ATmega32 with A4988 Stepper Motor Driver
In the schematic above, the PD7 and PD6 of ATmega32 are connected to DIR pin and STEP pin of the A4988 driver. The ATmega32 controls the motors direction and speed using these two pins.
Sending High signal on PD7 connected to DIR pin of the driver will cause the motor to rotate in clockwise direction, Sending LOW signal on PD7 connected to DIR pin of the driver will cause the motor to rotate in anti-clockwise direction. This is how a microcontroller like ATmega32 controls the direction of rotation of a stepper motor.
Similarly, the speed of the stepper motor is controlled using the STEP signal. If we send a pulse of certain duration from the pin PD6 to the STEP pin of the A4988 driver, the driver will cause the motor to rotate one step if full step mode is used. The Nema 17 step motor has a step angle of 1.8 degree and hence when we send a pulse the motor will rotate by an angle of 1.8 degree. If we send series of 200 pulses then the motor will rotate through 360 degree thus making a One complete rotation.The MS3, MS2 and MS1 pins are used to controls the micro-stepping resolution. Here we have used full step resolution and for this these pins are left unconnected. When left unconnected these will be in LOW state because of the internal pull-up resistor inside the driver. You can read the A4988 datasheet for configuring other microstep as shown below.
The ENABLE is active low pin and is left unconnected because it is pulled low by internal pull-down resistor. Thus A4988 driver is enabled. The SLEEP and RESET pins are also active low pins. They are wired together in our case. This is because the RESET pin in the breakout board pulled high and therefore the SLEEP pin is also high. Which means sleep mode is disabled. Next the A4988 power pins VDD and GND should be connected to 5V and GND pin of ATmega32. Shown below is picture of ATmega32 connected to A4988 stepper motor driver on a breadboard.
Connecting A4988 to 12V power supply
On the other side we have the power supply and the connection to the motor. The VMOT and GND just below it should be connected to the motor power supply which in our case is a 12V DC power input.
Interfacing A4988 driver to Nema 17 Stepper Motor
The next 4 pins labelled 2A,2B, 1A and 1B are for connecting to the motor two phases or coils ends. The 2A and 2B should be connected to the one coil/phase of the motor and the 1A and 1B should be connected to the other coil or phase. How to do this was explained in the Arduino Stepper Motor tutorial.
The following picture shows how to interface Nema 17 stepper motor with A4988 motor driver module below.
Code and Programming
C programming code for controlling stepper motor using ATmega32 is provided below. We first define PC0 and PD7 as stepPin and dirPin because it first makes easier if we want to change the pins and second because the code will be easier to understand. Then there are two function prototypes called rotate_cw() and rotate_acw(). These two functions are below the main function. The rotate_cw() function is a function that will rotate the stepper motor one complete rotation in clockwise direction and the rotate_acw() function will rotate the stepper motor one complete rotation in anti-clockwise direction. In the rotate_cw() function, we send a high signal via dirPin which will tell the A4988 driver to make clockwise rotation. After the direction is setup, we send 200 pulse using for loop to rotate the motor one complete rotation. This is because 200*1.8degree/step gives us 360 degree rotation. Similarly in the rotate_acw() function we have first send a low pulse to setup the direction in reverse direction, that is anti-clockwise. Then we again use the for loop to send 200 pulses for 360 degree rotation. In the main function, we have setup the stepPin and dirPin as an output pin. Then in the while() loop we call the rotate_cw() function to rotate the stepper motor one complete rotation in clockwise direction. After a delay of 50ms we call the rotate_acw() function to rotate the motor in anti-clockwise direction.
#ifndef F_CPU
#define F_CPU 4000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#define dirPin (1<<PD7)
#define stepPin (1<<PC0)
void rotate_cw(void);
id rotate_acw(void);
int main(){
// setup dirPin and stepPin as output
DDRD |= dirPin | stepPin;
while (1){
rotate_cw();
_delay_ms(50);
rotate_acw();
_delay_ms(50);
}
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(10);
PORTD &= ~stepPin;
_delay_ms(10);
}
}
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(10);
PORTD &= ~stepPin;
_delay_ms(10);
}
}
Video Demonstration of Stepper Motor control using ATmega32
The following video shows how the stepper motor rotates.