Thursday , March 30 2017
Home / Embedded Systems / Stepper Motor Interface with PIC Microcontroller

Stepper Motor Interface with PIC Microcontroller

In my previous post Introduction to Stepper Motors and How they Work, we had a look at stepper motors and how they work. This post will deal with the programming and circuitry involved in the stepper motor interface with PIC Microcontrollers.

Usually a microcontroller is used to produce the stepping sequence for the stepper motor. But this is not the only method of producing the stepping sequence. It can be produced by using flip flops, logic gates and some knowledge about digital electronics. But this post will deal with the former method as it is better and easier.

Need for Driver Circuits

Your microcontroller works at 5V and the stepper motor will need 12V to operate. So the microcontroller port pins can’t drive the motor on their own. They need something called as the driver circuit. This circuit typically takes low level signal from controller and converts them to high voltage/current signals that are capable of exciting the stator windings.

Depending on the current and voltage requirement of the stepper motor, the driver circuit has to be built. For this post, I will be using the smallest stepper motor that I have with me. This is just to avoid building dedicated power diver circuits.

If you don’t have a small one, you will need driver circuit. For now I will assume you do have a small motor with you. If you don’t, I will write a tutorial on making a driver circuit sometime in the foreseeable future.

Stepper motor
The stepper motor that I am using.

Entire Setup

Here is the set up that I made for the interface. For a change I thought I will build this circuit over a breadboard instead of the conventional development board setup in my posts. It took more time and looks a lot messier, but some things are worth the time spent over it. This is a fully standalone circuit which just needs a 12V supply.

stepper motor interface
Stepper Motor along with its entire setup on a breadboard

The Circuit:

Here is the top view of the breadboard. I have separated the circuit into various sections. Below are the schematics of each of the blocks mentioned. Click them to enlarge. I think its quiet self explanatory and nothing more needs to be said.



The programming is really simple and can be done with just a few lines of C code. But before you go into it, you should have read my previous post on the stepper motor basics to understand the program fully.

In the following program I have included all the three types of stepping sequences in three arrays that are selectable with Macros. Hence to use the code for full step mode, you will have to make define a macro, FULL_STEP

For those who don’t already know, #ifdef MACRO_NAME checks if macro MACRO_NAME is defined. If so, the lines that are in between the #ifdef MACRO_NAME and #endif statements are added to the program. If such a macro is not defined, then the lines in between the #ifdef MACRO_NAME and #endif statements are commented out.

So basically, it is a if statement but in terms of pre-processing. Needless to say, the same logic applies to the if-else statements.

Video of the stepper motor in action!

In this video, I have used the wave type stepping sequence. I’m sure there is not much noticeable difference between the various types of stepping sequence so I didn’t bother to record all of them. You can also see the change in speed with the change in delay in between each step.

Subscribe to our blog with your email and like us on Facebook to get instant notification of updates on our latest posts and how-to tutorials delivered to your inbox.

About Siddharth

Siddharth is a Firmware Engineer, techie, and a movie-buff. His interests include, Programming, Embedded Systems, Linux, Robotics, CV, Carpentry and a lot more. At times, you could see some of his sunday projects converge on release quality. You get to know him on the following social channels.

Check Also


Interfacing 16×2 LCD with C2000 launchpad

  The C2000 Launchpad is an inexpensive evaluation module based on Piccolo family microcontrollers from …

  • Mazen Aounallah

    Amazing and clear explanation, thanks a lot and keep this great work

  • Imsa Naga

    Your explanations are great and simple !
    I wish all your writeup/discussions are made printable
    for simple people like me learning electronics at a late age !
    keep it up !

    • Thanks for your feedback Imsa. Actually I haven’t found a reliable way to add the print feature to this blog.. I’ll add one as soon as possible.

  • Saranya

    Pic-kit 2 Programmer – Problem!!
    I have tried to build my stepper motor program in my PIC16F877A it could be successful but some times I got this error
    “Programming failed at Program Memory address 0x000003”
    What should I do now?

    Can anyone help me to overcome this problem.

  • Sachin Grover

    it should be…

    if (count == SEQ)
    index = 0;

    otherwise value of index crosses the array boundary…

    • Hi Sachin,

      Thanks for pointing that out. It was indeed a bug. There was a better way to fix it and I have done it. Please have a look now.


Keep in touch with the current trends!
Did you like this article? Sign up and get our latest posts delivered to your inbox!
  We hate spam and never share your details.