Stepper Motors are DC brushless motors which can rotate from 00 to 3600 in steps. Stepper motor uses electronic signals to rotate the motor in steps and each signal rotates the shaft in fixed increment (one step). The rotation angel is controlled by applying certain sequence of signals. Unlike Servo Motor, stepper motors can be driven by using GPIO pins of microcontroller rather than PWM pins and can rotate in (+3600) and (-3600). The order of signals decides the clockwise and counter clockwise direction of stepper motor. To control the speed of the motor, we just need to change the rate of control signals applied. The stepper motors rotates in steps. There are several modes of steps to operate Stepper Motor such as full step, half step and microstep. To know more about the basics, theory and working principle of stepper motor, follow the link.
We previously interfaced Stepper Motor with many Microcontrollers:
- Interfacing Stepper Motor with ARM7-LPC2148
- Interfacing Stepper Motor with Arduino Uno
- Interfacing Stepper Motor with MSP430G2
- Interfacing Stepper Motor with STM32F103C8
- Interfacing Stepper Motor with PIC Microcontroller
- Interfacing Stepper Motor with 8051 Microcontroller
- Interfacing Stepper Motor with Raspberry Pi
In this tutorial we will interface 28BYJ-48 Stepper Motor with Atmega16 AVR Microcontroller using Atmel Studio 7.0. The stepper motor is rated to work in 5V. We will be interfacing the stepper motor with both the motor drivers i.e. ULN2003 and L293. Both will be driven by 5V supply. In order to simplify the interfacing we are using prebuild module of both motor drivers. You can also use ULN2003 and L293D standalone IC’s. The number of wires and jumpers can be more, so just be careful while connecting all the connections.
Components Required
- Stepper Motor(28BYJ-48)
- ULN2003 Module/L293D Motor Driver
- Atmega16 Microcontroller IC
- 16Mhz Crystal Oscillator
- Two 100nF Capacitors
- Two 22pF Capacitors
- Push Button
- Jumper Wires
- Breadboard
- USBASP v2.0
- Led(Any Color)
Pin Description of Stepper Motor
Circuit Diagram for Stepper Motor Control using ULN2003 Module
Connect all the components as shown in the diagram below when using ULN2003. Similarly we will be interfacing it using L293D in next step. We are using PORTA of Atmega16 to interface stepper motor for both the motor drivers. There is no need to connect the 5V pin of stepper motor. Only the coil pins are required to move the stepper motor. The pin order is very important to drive stepper motor as the energizing of the coils should be in order to attain steps. Four inputs of ULN2003 and four outputs of ULN2003 are used in this project. The inputs will be connected to PORTA pins and outputs will be connected to Stepper Motor Signal pins. Also, connect one push button in Reset pin for resetting Atmega16 whenever required. Connect Atmega16 with proper crystal oscillator circuit. All the system will be powered by 5V supply.
Below is the actual Picture of ULN2003 Motor Driver Module:
Below we have given Atmega16 pin connections with ULN2003 and L293D to rotate the stepper motor. Interfacing stepper motor with L293D module is explained in later section, remember that only one module either ULN2003 or L293D is required for stepper motor control.
The pin connections for INPUT are as follows:
Atmega16 |
ULN2003 |
L293D |
A0 |
IN1(PIN1) |
IN1(PIN2) |
A1 |
IN2(PIN2) |
IN2(PIN7) |
A2 |
IN3(PIN3) |
IN3(PIN10) |
A3 |
IN4(PIN4) |
IN4(PIN15) |
The pin connections for OUTPUT are as follows:
Stepper Motor |
ULN2003 |
L293D |
Orange |
OUT1(PIN16) |
OUT1(PIN3) |
Yellow |
OUT2(PIN15) |
OUT2(PIN6) |
Pink |
OUT3(PIN14) |
OUT3(PIN11) |
Blue |
OUT4(PIN13) |
OUT4(PIN14) |
Circuit Diagram for Stepper Motor control using L293D Module:
Controlling Stepper Motor with AVR ATmega16
As already told unlike Servo Motor, Stepper motors need external drivers e.g. ULN2003 or L293D motor driver. So just connect the Circuit as above and upload the main.c program given at the end.
The sketch demonstrates the stepper motor rotating in both sides i.e. clockwise and counter clockwise direction. If you want to rotate the stepper in one direction simply comment out another direction’s code lines in the sketch.
Complete AVR code for controlling Stepper Motor is given below. Code is simple and can be understood easily. Two codes are given below, one for rotating stepper motor with ULN2003 and second with L293D module.
Connect your USBASP v2.0 and follow the instructions in this link to program Atmega16 AVR Microcontroller using USBASP and Atmel Studio 7.0. Just build the sketch and upload using external toolchain.
Complete code with Demonstration Video is given below.