Interface do motor de passo com o microcontrolador 8051 (89c51,89c52)

Interfacing stepper motor with 8051 microcontroller (89c51,89c52)

Here is a simple tutorial on how to interface stepper motor with 89c51 microcontroller. The stepper motor, as the name implies, takes steps to complete a full 360-degree circular rotation. We can rotate the step clockwise and counterclockwise. The speed of the stepper motor can also be varied. There are many types of engines steeper than four, eight or eighteen steps. Stepper motors are classified according to the steps taken by a specific motor to complete a full 360 degree rotation. Our focus is on the four-step motor. Once you are familiar with the four-step stepper motor, you can drive any type of stepper motor.

Stepper motor working

I will use four steep stepper motors in this project. There are usually 5 or 6 wires coming out of a four-curve stepper motor. In 5 wires, one is our vcc (external voltage) and the 4 renamed are step wires. In 6 wires, 2 are our vcc (external voltage) and the renamed four are step wires.

  • To know how the stepper motor works? Here is a good tutorial.GOOD TUTORIAL FROM WIKIPEDIA.


The stepper motor works from 5 volts to 12 volts. The stepper motor I am using in this tutorial is NEMA 17. It is a bipolar stepper motor. Each step taken by the nema 17 stepper motor accounts for an increase of 1.8 degrees. Therefore, in 200 steps, nema 17 completes a rotation of 360 (200 * 1.8 = 360) degrees. Our concern in this tutorial is just rotating the stepper motor.

Nema 17 stepper motor

Nema 17 stepper motor

Stepper Motor Power Consumption

The stepper motor consumes 0.1 to 1 amp of current during step rotation under load. Microcontrollers cannot produce this amount of power. Therefore, we cannot directly connect stepper motors with microcontrollers like 89c51 microcontroller, whose pins can produce a maximum of 0.045 amps of current.

Using ULN2003 driver for stepper motor

To cover the above stepper motor power constraint, we need an external power source that can drive heavy loads and provide enough power for stepper motor operation. ULN2003 is an IC containing Darlington configuration pairs of transistors. The ULN2003 receives low power input and outputs a higher power signal. I decided to use this driver IC to drive the stepper motor.

ULN2003 Driver

ULN2003 Driver

8051 microcontroller stepper motor – Design circuit

Connect port #1 of 8051 microcontroller (89c51) four bits lower to ULN2003 driver ic. The 89c51 microcontroller is unable to produce enough voltage/current to drive a motor, so a driver IC is required to meet the motor's voltage/current requirements. The output of the ULN2003 is connected to our stepper motor. Make sure you insert the same wire into the motor that you are specifying in your code step. Make ground pin 8 ULN2003. Apply the same voltage to pin 9 of the ULN2003 that you are applying to your motor.

Stepper motor interface with 89c51 microcontroller circuit diagram is given below. Observe the steps performed by the stepper motor in the GIF image below. For each step, we have to do each step with high pins and follow the pattern shown in the GIF image. Stepper motor pins/wires are colored in yellow, brown, black and green etc. First, we need to be sure of the pitch wire pattern. The Nema 17 stepper motor pattern is shown in the GIF image.

Note: The pattern must be in the correct order to rotate the stepper motor. Pay attention to the color coding of the steepest motor wires below.

stepper motor with 89c51 microcontroller and uln2003 driver

stepper motor with 89c51 microcontroller and uln2003 driver

Stepper motor interface with 8051 microcontroller – Project code

The code is very simple. It is written in c++ language using keil ide as a software tool to write and compile the code. First the necessary file reg51.h is included. Then a delay function is created to provide some delays in the steps. This delay is very important, use it to avoid back emf (electrical driving force) generated by the motor, otherwise your controller will be damaged by this back emf produced by the stepper motor. You can also speed up the engine rotation by reducing the delay time. In the main function I used some commands to rotate the stepper motor. The commands are explained below. Commands are in hexadecimal format.

P1=0x01 Making the port 1 bit 0 high for the first step. Binary equivalent=00000001
P1=0x00 Making port 1 all bits 0 for the next step. Binary equivalent=00000000
P1=0x02 Making the port 1 bit 1 high for the second step. Binary equivalent=00000010
P1=0x00 Making port 1 all bits 0 for the next step. Binary equivalent=00000000
P1=0x04 Making port 1 bit 2 high for the third step. Binary equivalent=00000100
P1=0x00 Making port 1 all bits 0 for the next step. Binary equivalent=00000000
P1=0x08 Making port 1 bit 3 high for the fourth step. Binary equivalent=00001000

You can also individually declare the bits and make them high and low for enough steps. The while(1) loop is continuously running the motor clockwise. You can also reverse the direction counterclockwise by just reversing the commands, placing the last command (0x08) at the beginning and reversing the
Order in the same way for the rest of the commands.

With a slight modification in the code you can vary the speed of the stepper motor with the 89c51 microcontroller. Just decrease the delay or remove it. To rotate at a given angle, calculate the steps needed to reach the angle and place the steps in the for loop. I hope it makes sense to you.

More projects involving stepper motors and different microcontrollers are listed below. Each design is unique and a dedicated microcontroller controls the stepper motor. All projects are open source. You can modify them according to your needs.

Web controlled stepper motor using nodemcu

Stepper motor controller via Bluetooth with Android app

Stepper motor controlled with 32-bit STM32 microcontroller

Download project files and compiled code in keil uvision 4. The code is written in c language. If you have any questions about the post, just leave a comment below. The project video is also provided below.

Watch 89c51 stepper motor interface design video

Stepper motor code 8051

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.