Stepper motor control with an ATmega8 microcontroller

2.33 avg. rating (55% score) - 3 votes

Stepper Motor Control with an Atmega8 and a L293D driver

board with motor


This note provides basic implementation details and procedural information to design and assemble a stepper motor system. The controller discussed here is the ATmel mega8, an 8-bit microcontroller (MCU).

The note consists of a general description and gives highlights of implementing a basic stepper motor system application. To amplify the application, the software was generated using Atmel mega8 microcontroller. The program created with the controller is shown in “Control software Explanation” section of this note. For convenience, a copy of the code is available and is included in a folder name “code”. For the sake of this note being educational, the hardware was also simulated with Proteus software prior to design of PCB. Code folder includes a file name 4lead stepper.prjpcb designed with Altium layout software. You may get the PCB of the controller circuit built by placing an order with any board fabricator.

Stepper motor theory

Originally, this controller was designed to control a car’s small sliding door related to the 4 Floor Elevator system project. As it is mentioned in that project, in the up graded version, I used a 4 wire stepper motor to open or close the sliding door.

A 4-phase stepper motor is really two motors sandwiched together. Each motor is composed to two windings. Wires connect to each of the four windings of the motor pair, so there are eight wires coming from the motor. The commons from the windings are often ganged together, which reduces the wire count to 4, 5, or six instead of eight.

Stepper Phasing

A 4-phase stepper motor requires a sequence of four pluses applied to its various windings for proper rotation. By their nature, all stepper motors are at least two-phase. The majority are 4-phase, some are six phase. Usually, but not always, the more phases in a motor, the more accurate it is.

Step Angle

Stepper motors vary in the amount of rotation the shaft turns each time a winding is energized. The amount or rotation is called the step angle and can vary from as small as 0.9 degrees to 90 degrees. The step angle determines the number of steps per revolution. A stepper with a 1.8 degree step angle, for example, must be pulsed 200 times for the shaft to turn one complete revolution. A stepper with a 7.5 degree step angle must be pulsed 48 times for one revolution, and so on.

Controlling a Stepper motor

Steppers have been around for a long time. In the old days, stepper motors were actuated by a mechanical switch, a solenoid-driven device that pulsed each of the windings of the motor in the proper sequence. Now, stepper motors are invariable controlled by electronic means. Basic actuation can be accomplished via computer control by pulsing each of the four windings in turn. The computer can not directly power the motor, so transistors must be added to each winding, as shown in figure 1.


Figure 1

Figure 1 shows the basic hookup connection to drive a stepper motor from a microcontroller. The phasing sequence is provided by software output through a port in the following four-bit binary sequence: 1010, 0110, 0101, 1001(reverse the sequence to reverse the motor.


Figure 2: table showing 4-bit binary sequence

Using a microcontroller to generate four step actuation sequence

In this case, I am going to program an Atmel mega8 microcontroller to provide a four step actuation sequence.

Four port B pins are used to drive the inputs to the stepper motor coils. Direct connection from the controller to the motor is fine for applications that require minimal drive. But for applications that require increased current drive capabilities, enhanced circuits are necessary. One way to increase drive current is to use a motor controller IC designed specifically for that purpose. In my case, I chose to use the L293D to amplify the output current drive of microcontroller. In my case, I chose to use the L293D to amplify the output current drive of microcontroller output port B. The L293is designed to provide bidirectional drive currents of up to 600-mA at voltages from 4.5 V to 36 V. Both L293 and L293D devices are designed to drive inductive loads such as relays, solenoids, dc and bipolar stepping motors, as well as other high-current/high-voltage loads in positive-supply applications.

Control software Explanation

In the control program, two functions CW and CWW are used to define the revolution direction of the stepper. When CW function is called, the stepper motor revolves in clockwise direction, and when CWW is called, it changes its direction to a Counterclockwise one. The phasing sequence shown in figure 4 is defined as a hex file and is sent out of B port of microcontroller which is connected to the stepper motor via L293D IC. There is a delay of xx ms between each phase which is defined by the “d” variable. The code prepared for CW or CWW is shown in the following figure.

void cw()











void ccw()











To control the speed of revolution, we can increase or decrease the content of “d” variable. Two external interrupts 0 and 1 are use to do so. Amount of variable “d” is increased when external interrupt 0 is activated and is decreased when interrupt 1 is activated instead. Also amount of d can not be greater than 250 or less than 5.

// External Interrupt 0 service routine

interrupt [EXT_INT0] void ext_int0_isr(void)






// External Interrupt 1 service routine

interrupt [EXT_INT1] void ext_int1_isr(void)






 The way the revolution direction of the stepper motor is controlled is when pushbutton # 1 is depressed, it is turned on and revolves CW, and when pushbutton #2 is also depressed in the same time, its direction changes to CCW. There are few conditions regarding the direction of revolution. When pushbutton connected to PIND.0 is depressed, stepper revolves CW and when PIND.1 is also pushed in the same time, it revolves CCW.












And when PIND.0 is released, stepper motor is turned off.






Download of software

You may find the developed software from the folder named “Code”


Circuit schematic

 Figure 3 displays the schematic diagram related to stepper motor controller.

As you see from the schematic of the circuit, four pins of output port B are connected to 4 inputs of L293 to amplify the output current drive of microcontroller output port B.  4 diodes are used at output ports of L293, for protection of the IC. S1 and S2 are used to control the speed of rotation. Since eventually, the commands to turn motor on/ off or come form a PLC, then two zener diodes plus two resistors in series are used to protect the controller circuit against 24 v of the PLC’s output port voltage level, so the combination of diodes and resistors act similar to a voltage level shift circuit.


Figure 3

P1 terminal is used to connect two input signals MS1 and MS2 to the controller’s input ports. These two signals come from two limit switches that detect if the door is completely closed or open. You do not need to be worry about the extra elements. Just try to understand the schematic and the control software and tailor it according to your own project at hand.

PCB layout of the circuit

Figure 6 shows the PCB layout of the circuit. I used Altium PCB layout software to convert circuit’s schematic diagram (shown in figure 5) into a PCB layout which is shown in figure 6.


Figure 6 PCB layout of the circuit

Conclusion and summary

There are numerous stepper motor applications that can take advantage of the power, features and flexibility of the Atmel mega8 MCU. Applications would be include robotics controllers, turning machine tools and other precise shaft positioning control environments. This example is a general solution that demonstrates the ease with which a microcontroller can be designed into a stepper motor control application.

Due to the types of applications supported, stepper motors operate at relatively low rotating speeds. The actual speed is controlled by varying the delay between coil activations. With this system application, the stepper motor converts binary input pulses coming from MPU to rotary shaft movement on the stepper motor. The direction of turn is a function of the sequenced in which the binary pulses are applied to the stepper motor.

In addition, the requirement for a digital to analog converter is eliminating when using stepper motors versus dc or ac motors in dc systems. Ac and dc motors provide continuous shaft rotation. However, stepper motors produce shaft rotation in precise steps or increments as the result of the applied binary pulses. This can be in the form of either half or full steps (step-angular sensitivity) depending on the sequence of coil activations.

It is noteworthy to mention that most stepper motors are used in application with relatively small loads. An overload condition could result in a shaft slip. This undesirable condition could induce an error that might not be recognized and affect operating precision. To minimize the possibility of this occurring, buffer type amplifiers should be place between the MCU and the stepper motor. In terms of reliability, MCUs can operate problem-free in stepper motor applications for years if used within their specified limits.

Click below icon to Download this project files

Download ZIPYou may also watch the video of this and our other projects at

from the following links:

1- Programming with SIMATIC S7 300/400 Programming Controllers

2- Programming with SIMATIC S7-200 PLCs using STEP 7-Micro/win software

3- Programming Siemens S5 PLCs using IBHSoftec simulator software

4- Programming Siemens S5 series of PLCs using PG 2000 compiler software

5- Basics of Programmable Logic Controllers (PLC)

6- Programming Alley Bradley series of PLCs using RSLogix 500 software compiler

7- Programming 4 types of PLC in 5 project

8- Household wiring, circuits, diagrams and projects

Design and Implementation of typical end year college projects using PLC or a typical microcontroller with site links and watch the projects in action:

9- Programming a 4 floor elevator with SIMATIC STEP 7 & LOGO! SoftComfort software

10- Programming SIEMENS LOGO! PLC as a dyeing machine temperature controller

11- Programming a Traffic Light Controller system using SIMATIC S7-300 & HMI display

12- Programming a PLC based car Parking System with SIMATIC STEP 7 and S7-300 PLC

13- Programming a Traffic Light Control system using an AVR ATmega8 microcontroller

14- Temperature controller and monitoring with a microcontroller

15- Microcontroller based 4 Floor Elevator System

16- How co control a stepper motor with ATmega8 Microcontroller and a L293D IC

To learn more about each title, please check out my personal websites at the below addresses:

2 thoughts on “Stepper motor control with an ATmega8 microcontroller

  1. Its design is very good and I’ve seen that works … but in yours too good explanation, nothing was said about the clock microcontroller … For the schematic gives to see what is internal but, what value?

  2. Hi, I do believe this is a great website. I stumbledupon it ;
    ) I’m going to revisit once again since I book-marked it.
    Money and freedom is the greatest way to change, may you be rich and
    continue to help others.

Leave a Reply

Your email address will not be published. Required fields are marked *