Exact Circuit and MicroC code for stepper motor

Well saw that people still are having quite some doubts on the stepper motor post that i had put up. so i thot to make that abstact clear i better put the exact circuit and the code here.

The exact circuit to connect the stepper motor to 16F877A is given below

In this the common is connected to the 16V supply(12 v is actually enough actually higher current drives the motor better as far as i know*). If u are new here and donot know how to find the common line in a unipolar Stepper motor then see here

The MicroC code for running the motor is as below.
void main()
{
TRISB = 0x00;
PORTB = 0x00;

//routine to move in one direction
for (i=0;i<50;i++)// increse i=50 no i=100 to cover double the distance
{
PORTB=0x80;
delay(20ms);
PORTB=0x40;
delay(20ms);
PORTB=0x20;
delay(20ms);
PORTB=0x10;
delay(20ms);
}

//routine to move in the other direction

for (i=0;i<50;i++) // increse i=50 no i=100 to cover double the distance
{
PORTB=0x10;
delay(20ms);
PORTB=0x20;
delay(20ms);
PORTB=0x40;
delay(20ms);
PORTB=0x80;
delay(20ms);
}
}

There are much easier ways to write the above code but then i am not worried about the memory it’l take nor have any pressing need to keep my code small and trim ,so i guess this will make you absolutely clear of what i’m trying to do to make my motor run.

If you din know MicroC well,let me brief. The line PORTB = 0x80 (1000000)makes my 8th pin of the PORT “B” that’s b7 pin or the 40th pin go high.The next line PORTB = 0x40(01000000) will make the 39th pin go high and all other low.

Now whenever the pin at the input of the ULN2003 goes high say pin 1 the output pin 16 drains the current throuh the motor thus completing the circuit from supplt 16V through motor to the ground in that line. When you keep doing this in series as i called it the abcd pulse the motor will rotate . When your reverse the pulse order the motor rotates in the other direction. As simple as that. For most robotic application atleast at contest level you may use this circuit uinless you go for bigger stepper motors that need more current to drive and is greater than the 500mA that the ULN2003 can sink!!
i guess i was clear about using ULN2003 to drive a sstepper motor. pl post your comments and doubts
–Madan
I like keeping all my circuits small and simple and not crowd it with resistors and capacitors.Afterall its a hobby and we need not complicate it.Cheers.

Author: Madan Thangavelu

Software Engineer

99 thoughts on “Exact Circuit and MicroC code for stepper motor”

  1. Pingback: Anonymous
  2. you’l need a programmer… that’s a device…. u need to connet it to the computer and transfer the code u wrote on computer to the microcontroller… see it in the ‘Micro-controller’ page in the top.

  3. Yes you are right . in 8 should be grounded . i made a mistake here. i forgot to change that. i had noted it though. i’l try to change it as soon as poosible.

    Thanks for reminding

  4. hi madan..
    i trying to move stepper motor with the help of AT89S51 using ISP(in system programming)…
    from ur referance i m not getting the pin configuration of ULN2003 ..
    8-PIN is ground .. where is Vcc ..

    and also if u having AT89S51 ISP ckt. plz send me..

  5. finally i corrected the circuit diagram. Sorry for all the ones above.

    @ Vishal Uln Will get not have any direct VCC. It will have ground at pin 8. And read datasheet or the post carefully to know what uln does here vishal.

    ISP? u want a circuit for that form me?

  6. Hi Madan
    Was wondering is this the easiest way to interface between 4 stepper motors and my PIC16F877 development board, have seen circuits were two motors can be controlled using the ULN2003, is it possible that you might be able to send me a circuit design like this or maybe another alternative. First time acually experimenting with this field of electonics so some help would be great…

    Thanx Ryan

  7. Hi Madan

    Is it necessary to put a diode between the COM line and the power supply(16V)? I heard it is to prevent the back emf of the stepper motor…

  8. For small motors it is not. if the rating is higher you might swell put a optical coupler to avoid the bacf emf problem completely.

  9. hi madan,
    i just needed 2 knw dat isnt dre ne need 2 power d ic’s… i mean wat abt vcc’s… is dis d complete circuit… don mind.. actually m a noobie in dis field… just askin.. n ya i wanted to knw dat is dre ne way 2 drive a motor of 3.25 A.. i hv heard dat uln2003 cannot drive motors above 1.2 A or sumthin…

  10. @Rohit To drive 3.2 A motors try to use relays. or 2 uln’s one over
    the other. And pls try to type your questions fully.
    Thankyou

  11. hi madan…
    thnx for ur reply… i really want ur help regarding dis circuit… i hv seen the datasheet of 877a also n frankly speaking m unable 2 understand it fully.. m a mechanical engg student. plz help me regarding powering 877A n uln2003(i mean vcc’s).. i need the complete circuit.. hope 2 receive ur reply

  12. hey that problem is solved…

    but i have to change delay to delay_ms and 20ms to 20 before i was able to run …thanks it worked….

    but as u know it limits the current to 500ma ….wat if i need to run a motor of around 3.25 A as i need to take up high load….

  13. @mukul
    You are right . and i said do i have to mention that means you have to !!!.

    Then the syntax in MicroC is delay_ms and not delay(20ms)
    That was just a representaiton.

  14. Sir,
    I am very new in stepper motor field.So,I have some problems.Please help me-
    1)please,tell me the significance of the 5th,6th and 7th pin of the ULN2003 IC.
    2)Is there any chip select input in ULN2003?
    3)please,give me the most cost efficient and easy circuit diagram for driving three and six stepper motors by using ULN2003,parallel port and other IC’s(At any time only one stepper motor will be active and others will be disable)but without using AND gates.
    4)Send me the complete C code for robot gripper(three stepper motors) and robot leg(six stepper motors).

  15. Hi all…

    I am very new to stepper motors.
    i have to control a UNIPOLAR stepper motor to control a sluice gate in my college Mini project.
    I am using PIC16F877A for my project and i have develope a PIC simulation board and a PIC programmer.
    im using MikroC to program and ic-prog to burn .hex to the chip.

    You guya anyone have a complete circuit diagram (PCB top and Bottom) to do this task?
    do i have to use ULN2003 to do this task????
    does anyone have the source code for above thing???

    pls post a reply or reply to isuru66@gmail.com

    Thanx.
    Isuru

  16. Hi all,
    i have try out above source code.
    but it provide 3 errors.. tell me whats wrong in this code….

    void main()
    {
    TRISB = 0×00;
    PORTB = 0×00;

    //routine to move in one direction
    for (i=0;i<50;i++)// increse i=50 no i=100 to cover double the distance
    {
    PORTB=0×80;
    delay_ms(20);
    PORTB=0×40;
    delay_ms(20);
    PORTB=0×20;
    delay_ms(20);
    PORTB=0×10;
    delay_ms(20);
    }

    //routine to move in the other direction

    for (i=0;i<50;i++) // increse i=50 no i=100 to cover double the distance
    {
    PORTB=0×10;
    delay_ms(20);
    PORTB=0×20;
    delay_ms(20);
    PORTB=0×40;
    delay_ms(20);
    PORTB=0×80;
    delay_ms(20);
    }
    }

    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

    error message is

    ‘;’ expected but x found
    internal error
    internal error

    i think errors are in line 3 o 11

    please give me a solution…

    1. the problem is that: you have to declare i as type int,write delay_ms(value without units)insteady of delay(20ms)and lastly you have to write x in proper way.

  17. hey madan …

    u are well known of the fact that with uln2803 i get around .6A what if i need more current as the motor i wanna use has the max rating of 3.25A ,10kg-cms holding torque.Its kinda important for the project to use such high torque motors and these cant be avoided so i m kinda stuck with it.

  18. can you please tell me how to accomplish the above without the microcontrollers???(…i am very much familiar with them.)

  19. I have another doubt, does this code work for bipolar (6 wires) motors? If not, could you please help me? I’m stagnant about how to drive it using C code.

  20. i have the same problem with ICEMAN, i use 4 stepper motor.my problem is how is the command to make the stepper motor run the different rotation at the same time?

    like motor a turn clockwise direction while another motor run at counter clockwise direction.

  21. i want to know the program to run stepper motor.
    i use pic 16f628 and the driver ic is 3967. also i want the interfacing onnection between these ic’s.

  22. hi all..

    I want to know the

    “configuration of stepper motors”

    to be used in

    “micromouse”

    which are sufficient enough to drive my bot..Im not going for a complicated and high standard bot, but for a simple one ,as a beginner.

    Thankyou for the reply…

  23. void main()
    {
    TRISB = 0×00;
    PORTB = 0×00;

    //routine to move in one direction
    for (i=0;i<50;i++)// increse i=50 no i=100 to cover double the distance
    {
    PORTB=0×80;
    delay(20ms);
    PORTB=0×40;
    delay(20ms);
    PORTB=0×20;
    delay(20ms);
    PORTB=0×10;
    delay(20ms);
    }

    //routine to move in the other direction

    for (i=0;i<50;i++) // increse i=50 no i=100 to cover double the distance
    {
    PORTB=0×10;
    delay(20ms);
    PORTB=0×20;
    delay(20ms);
    PORTB=0×40;
    delay(20ms);
    PORTB=0×80;
    delay(20ms);
    }
    }

    i have written this code but when i built then three error is shown below:
    ‘;’ expected but x found
    internal error
    internal error
    plz give me solution

  24. hi i have a step motor produced by philips and its model MB 11. but i dont know i can drive it. i have a circuit eith ULN integer but i dont know how i can connect the integer and motor. i mean i dont know wire connection. please help me.

    if you want to help me.

    please send me your information:

    adnanmerter@gmail.com

  25. hello sir,
    where r the pins of port b connected in 16f877a .i m using 16f877a
    to generate PWM which is connected to the power drive circuit to run the stepper motor.how r the interrupts used.

  26. Hey thank you for posting this.

    Does this circuit work only for unipolar Stepper motors? Or it also works for Bipolar Stepper motors. Also, can we distinguish between different pins of the bipolar Stepper motors in the same way as you explained for the Unipolar Stepper motors?

    Thank you

  27. Sir,
    I am very new in stepper motor field.So,I have some problems.Please help me-
    1)please,tell me the significance of the 5th,6th and 7th pin of the ULN2003 IC.
    2)Is there any chip select input in ULN2003?
    3)please,give me the most cost efficient and easy circuit diagram for driving three and six stepper motors by using ULN2003,parallel port and other IC’s(At any time only one stepper motor will be active and others will be disable)but without using AND gates.
    4)Send me the complete C code for robot gripper(three stepper motors) and robot leg(six stepper motors).

  28. Sir,
    I am very new in stepper motor field.So,I have some problems.Please help me-
    1)please,tell me the significance of the 5th,6th and 7th pin of the ULN2003 IC.
    2)Is there any chip select input in ULN2003?
    3)please,give me the most cost efficient and easy circuit diagram for driving four and six stepper motors by using ULN2003,parallel port and other IC’s(At any time only one stepper motor will be active and others will be disable)but without using AND gates.
    4)Send me the complete C code for robot gripper(four stepper motors) and robot leg(six stepper motors).

  29. @shahin_198:

    1.they also similar to the pins 1,2and3 inputs.
    2.so far i didn’t heard any chip select in ULN..

    aravinth.s

  30. Hi,
    I’m currently uskng PIC16F877A and CCS c compiler to do the programming, is it possible to use the above codes with ccs c? i’m relatively new to stepper and micro-controller. hope you would be able to help with this issue. Thanks alot.

  31. hi CAn you help me? I need to control four stepper motor with the pic 16f877, then I need the circuit and d the code..

    help please

  32. dear sir,

    I am new in robotics field. I wasted my uptill 14000/- rupees in bilding micromouse . please help I am ery much in trouble. Please tell me the exact coding flood fill algo. or Djiktar’s algo. and there are very few days left in competition please help!!!
    regards
    gaurav

  33. hi, i need ur help.i am doing robotic arm project.In ur project we r using 89c51microcontroller.to drive stepper motors we r using latch,optocouplers-(4N35),steppermotordriver(TIP122)in order to run stepper motor.But u r saying that with only one device uln2003 is sufficient to run stepper motors.so wat r the advantages and disadvantages in using uln2003 and cant we use ur method to run motors. which is efficient one in taking into consideration all the factors like cost,efficiency,accuracy etc. i am circuit also.please giv reply at my mailid

  34. hey, i am pretty new 2 to stepper motors
    can any body help me with micro c code to control ir sensors pls send ur reply 2 my mail id

  35. hi
    i have to do the project on fault analysis of dual stepper motors using pic such that on is stopped by some fault the other motors should automatically starts wotking.the pic i am using is 16F873A WITH TWO ANALOGUE MULTIPLEXER

  36. hi
    i have to do the project on fault analysis of dual stepper motors using pic such that one is stopped by some fault then the other motor should automatically starts wotking.the pic i am using is 16F873A WITH TWO ANALOGUE MULTIPLEXER.
    i want the code for pic in micro c language

  37. helo sirr i ws rely informtive but cn u plzz advice me how to control speed of a 12v dc motor …i m begner and only know how to synronice led using atmega 16…..my im is to buid a line ollower …i am in 2nd yer….plz avdice me

  38. Hi Madan
    I am doing my final year project for the completion of computer engg degree. THe project is all about controlling the movements of a battery operated robotic arm through the PC’s parallel port.
    THe arm is OWI-007(made by OWI Inc.) The stepper motors need a 3v supply while the parallel port giving me 5 volts! So Should I use a voltage regulator here to step down the voltage or simply use ULN 2803 with my circuit?
    waiting for reply

  39. I’ve tried your code but my unipolar stepper is rotating in the same direction for both loop, what could be the problem??? pls help!!!

  40. Hi Madan
    I am doing my final year project for the completion of computer engg degree. THe project is all about controlling the movements of a battery operated robotic arm through the PC’s parallel port.
    The arm is OWI-007(made by OWI Inc.) The motors need a 3v supply while the parallel port giving me 5 volts! So Should I use a voltage regulator here to step down the voltage or simply use ULN 2803 with my circuit?
    waiting for reply

  41. hi,
    I am new to this feild of robotics & don’t have any idea about it.I want some help on how to build a robot that can solve the maze i.e. micromouse kind of thing.please help me out

  42. hi madan,

    we need to control a stepper motor using 89c51. we used the same circuit you have posted, using uln 2003. But the problem we are facing is the stepper rotates two steps clockwise and two steps anti clockwise continuously. Its not rotating in the same direction. Help us to fix it…

  43. hai madan i m getting a problem in installing the LPT1.exe in my system it is asking for the file HanDes.dll but when i run the file after downloading it is not wprking properly can u please sujjest the optimum solution for this.

  44. plz send me the deails of (,hardware and software)stepper motor speed controller in my above mentain email id

  45. hi,
    ur pgm is nice,can u pls help modify the pgm so as to provide rotating a certain angle then a 10 sec pause,again rotating at an angle then pause hence providing a complete 360deg…this is regarding our project

  46. Mam,
    I am a final year engineering student.In our project,we use stepper motor and ULN2003 as the driver..We have to rotate the stepper motor in both clockwise and anticlockwise directions……..When a condition,say true, is met,stepper must rotate in clockwise direction otherwise in anticlockwise direction.program must be written in mikroc..So please help us.pls send me the prgrm.it’s very urgent

  47. thank u thank u thank u i will try this ,..how can i rate this ,..oh star star star star star ^_^ sooo happy

  48. Thanks buddy, this was realy very useful for me. we are in the process of making a micromouse. your posts are very much useful for not only to us but also to many other robotics enthusiast…keep up the good work

  49. Hi..just a question… and what is the ULN2003 Vcc? 16v too?
    i try to connect my ULN2803 to 12V and I burned it!! 😦 do you know why?

  50. Hello sir,
    i m working on a project using stepper motor
    Model: 24BYJ48-01
    Rated voltage: 12VDC
    Number of Pole: 4
    Speed Variation Ratio: 1/64
    Stride Angle: 5.625 /64
    DC resistance: 200ohm +-7%(25k)
    Idle In-traction Frequency: >600Hz
    Idle Out-traction Frequency: >1000Hz
    In-traction Torque: >29.4mN.m(120Hz)
    Self-positioning Tor: >29.4mN.m
    insulated resistance: >10M ohm(500V)
    insulated electricity power :600VAC/1mA/1s
    Insulation grade: A
    Rise in Temperature:<40K(120Hz)
    Noise; <40dB(120Hz,No load,10cm)
    Weight About: 40g

    i m confused which driver ic would b the perfect as its current rating is high.
    Please inform me the suitable driver ic and possible coding.
    thanks

  51. hello,i am using microbasic ,if i want the motor to rotate only 45 angle clkwise,90 angl anticlkwise,180 angle anti clkwise ,what modificatiion shall i do?

  52. Hello
    I am currently working on a robotics project that requires using 2 stepper motors and 3 gear heads to control a 5 DOF robot. I am using either MicroC ar CCS C. I will love any hints available on the coding. Also if anyone has done something like this before, I would please love to see the code used.

  53. hello..i’m doing final year project…can i ask..i m using 16f877a to generate PWM which is connected to the power drive circuit (h-bridge…L298) to run the stepper motor…I’m currently using PIC16F877A and CCS c compiler to do the programming, is it possible to use the above codes with ccs c? I hope you would be able to help with this issue. (^_^)

  54. hai, i have interfaced a sensor with the pic and once its output is “1” the stepper motor should move to 180 degree. i have tried different methods but the motor is stopping it is continuously running till the sensor is “on”. plz help me how to stop it. m using mplab and c code…

  55. Hye, thanks for your code.
    I need your help. If i want to rotate stper motor in a 90 degree rotation then what modefication is need.
    Is it possible with yous code or need to write new code???

  56. I need code that can control stepper motor, buzzer and LED (green and red), inerval btn motor and the two ie. (red led and buzzer) is 30sec. bt red led and the buzzer are all on at the same time

  57. How to rotate the stepper motor to 180 degree for forward and reverse in left direction and forward and reverse in right direction..I tried ,but it rotates only one direction ..

Leave a comment