ad space

How to control Stepper Motor using ATmega32 and L293D IC

In this microcontroller tutorial, you will learn how to control a stepper motor using ATmega32 using the L293D motor driver. The stepper motor used here is Nema 17 which is a popular stepper motor used widely in 3D printers and other motor projects. We will show how to rotate the stepper motor in clockwise and counter clockwise direction using a switch. You will also find here C programming code that can be used for AVR microcontroller and video demonstration so that you know how it works and what to expect. Using Arduino to control stepper motor as in our Arduino Stepper Motor Tutorial is much easier but doing it using raw microcontroller without pre-coded C program functions, like we are doing here will be fruitful later on in your other engineering projects.

L293D Motor Driver IC(Integrated Circuit)

L293D is a popular motor control driver with 8 pins in DIP package. It can drive two DC motors or can drive a single stepper motor just like we are doing here. We already have a tutorial on DC Motor Control using ATmega32 and L293D

L293D pinout

The L293D motor driver is designed to be interfaced with microcontroller, meaning it accepts TTL logic control signal from microcontroller and outputs sufficient high current into the coil of motors. It can provide 600mA of current(rated peak is 1.2A) and can be power supplied (for motor) in the range of 4.5V to 36V. 

There are 4 inputs 1A,2A,3A and 4A which needs to be connected to the microcontroller. There are 4 outputs, 1Y, 2Y, 3Y and 4Y which needs to be connected the 4 wires of the stepper motor. There are two enable pins 1,2EN and 3,4EN which enables the inputs 1A,2A to their outputs 1Y,2Y and 3A,4A to their outputs 3Y,4Y respectively. When the enable is high then there will be outputs from the inputs otherwise there will be no outputs. These enable pins can be either connected to microcontroller or can be tied to Vcc1 or +5V. There are two types of power supply pins in L293D- Vcc1 and Vcc2. The Vcc1 is for TTL logic and should be connected to the power supply rail of the microcontroller. The Vcc2 is meant for motor voltage. This can be more than 5V, for example 12V in order to satisfy the power requirement of the motor. Here we will connect it to 12V power supply. The other 4 pins labelled GND in the center of the IC are to be connected to the ground.

Stepper Motor Nema 17

There are two kinds of stepper motor- bipolar or unipolar stepper. We are using Nema 17 which is a bipolar stepper motors. 

Bipolar have four wires, two(red & blue) of which goes through one winding coil inside the stepper motor and the other two(green & black) goes to another winding coil inside the stepper motor as illustrated in the picture. Each two wires of the coil should be connected to the motor output 1Y,2Y and 3Y and 4Y of the L293D motor driver as explained previously.

Stepper motor have rated maximum current for rate voltage. For Nema 17, the maximum rated current is 1.68A for rated voltage of 2.8V. It means that when the voltage across each coil is 2.8V then it will draw current of 1.68V. For driving stepper motor it is the current that becomes more important than the voltage. By that it means the stepper motor requires its minimum current to turn and increasing voltage is there to merely increase the current into the coil.

Although L293D motor driver are still popular but there are also other drivers which makes controlling motors much easier. One such driver is the A4988 stepper motor driver which we have used in Stepper Motor Control using ATmega328p tutorial.

Interfacing ATmega32 with L293D and the Stepper Motor

As shown in the schematic drawing, we have used the Atmega32 port D pins 7, 6, 5 and 4 to the L293D inputs A,2A,3A and 4A. The Nema 17 stepper motor is connected to the output pins 1Y, 2Y, 3Y and 4Y.

Interfacing ATmega32 with L293D and the Stepper Motor

In the above electrical schematic drawing, we have connected a switch to PORT D pin 3 to control the direction of rotation. If it is open then the motor will rotate in clockwise direction and if it is closed then the motor will rotate in counter-clockwise direction. It is just one simple example how you could control stepper motor using l293D manually. But you could also use C programming code to control the stepper motor direction.

ATmega32 L293D Stepper motor simulation in Proteus

Below is a video demonstration of simulation of stepper motor control using ATmega32 and L293D IC in proteus software.


Video demonstration of driving Stepper Motor using ATmega32 and L293D IC

The following video shows how to control stepper motor Nema 17 using ATmega32 and L293D integrated circuit(IC) with a switch. When the switch is open, the motor will rotate in clockwise direction and when the switch is closed it will rotate in anti-clockwise direction.

C code for driving L293d bipolar stepper motor

Below is the C programming code for ATmega32 programmed to drive Stepper Motor (Nema17) using L293D IC. In the program code we have used a switch to control the direction of rotation of the motor which has been explained above.


/*

Controlling stepper motor using L293D stepper motor driver
By ee-dairy blog
https://ee-diary.blogspot.com

*/


#ifndef F_CPU
#define F_CPU 4000000UL
#endif


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


//define interfacing pins between L293D and ATmega32
#define a1 (1<<PD4)
#define a2 (1<<PD5)
#define a3 (1<<PD6)
#define a4  (1<<PD7)


//For switch
#define SW (1<<PD3)
 

int main(void){
	//Set a1, a2, a3, a4 as output
	DDRD |= a1 | a2 | a3| a4;
	//Set switch an input & pull-up resistor
	DDRD &= ~SW;
	PORTD |= SW;

	while(1){
		if(!(PIND & SW)){
		// if switch is open then rotate clockwise
		PORTD |= a1;
		PORTD &= ~a2;
		PORTD &= ~a3;
		PORTD |= a4;

		_delay_ms(100);

		PORTD |= a1;
		PORTD &= ~a2;
		PORTD |= a3;
		PORTD &= ~a4;

		_delay_ms(100);

		PORTD &= ~a1;
		PORTD |= a2;
		PORTD |= a3;
		PORTD &= ~a4;

		_delay_ms(100);

		PORTD &= ~a1;
		PORTD |= a2;
		PORTD &= ~a3;
		PORTD |= a4;

		_delay_ms(100);
		}

	else{

		// if switch is closed then rotate clockwise
		PORTD &= ~a1;
		PORTD |= a2;
		PORTD &= ~a3;
		PORTD |= a4;

		_delay_ms(100);	

		PORTD &= ~a1;
		PORTD |= a2;
		PORTD |= a3;
		PORTD &= ~a4;

		_delay_ms(100);

		PORTD |= a1;
		PORTD &= ~a2;
		PORTD |= a3;
		PORTD &= ~a4;

		_delay_ms(100);

		PORTD |= a1;
		PORTD &= ~a2;
		PORTD &= ~a3;
		PORTD |= a4;

		_delay_ms(100);		

	     }
	}
    return(0);

}

Other Stepper motor tutorials:

- Stepper Motor Control with Motor Shield and Arduino

- Stepper Motor control using ATmega32

- Stepper Motor Control using ATmega328p

- Arduino Stepper Motor Control using L298N

 

Post a Comment

Previous Post Next Post