Invobot : Artificially intelligent

Lets make machines intelligent

Exact Circuit and MicroC code for stepper motor

Posted by Ib / Vce(sat) on June 29, 2006

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 = 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);
}
}

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 = 0×80 (1000000)makes my 8th pin of the PORT “B” that’s b7 pin or the 40th pin go high.The next line PORTB = 0×40(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.

74 Responses to “Exact Circuit and MicroC code for stepper motor”

  1. Anonymous said

  2. Harrynee said

    thanks buddy..this is really helpful..u doing a gr8 job!

  3. :)

  4. RITWICK said

    as iam a begginer plz tell how to enter programs in microcontrollers

  5. madan said

    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.

  6. Surfer said

    Funny Stuff …

  7. madan said

    I wish it really was surfer :) . so how is amsterdam man?

  8. PRATAP said

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

  9. madan said

    change the delay to change the speed. that’s all to it.

    delay(20ms);

  10. Rishi said

    Hey, you dint ground your ULN correctly.
    I guess it should be like, PIN 8 to GND and PIN 9 to high, i.e. 16V.
    I tried your circuit, it is not working.
    This is what I am sayign is correct :
    http://electronics-diy.com/stepper_motors.php

  11. madan said

    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

  12. The circuit has been modified, the circuit now is CORRECT!! :) . Thanks rishi for bringin it to my notice.

  13. vishal said

    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..

  14. madan said

    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?

  15. Iceman said

    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

  16. aswadi said

    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…

  17. Madan said

    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.

  18. rohit said

    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…

  19. madan said

    @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

  20. rohit said

    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

  21. mukul said

    madan …dont u think u have to declare i first … int i;

  22. madan said

    @Mukul
    Well do i need to include that? :)

  23. mukul said

    for me … it says …an undeclared identifier[i] in expression
    in mikroC only

  24. mukul said

    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….

  25. madan said

    @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.

  26. Muhib said

    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).

  27. Isuru said

    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

  28. Isuru said

    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…

  29. mukul said

    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.

  30. madan said

    @mukul
    use a relay to drive motor, and the uln to switch relays.

  31. Aditya said

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

  32. Diego said

    Which libraries do I have to use to run this code?
    Thanks

  33. Diego said

    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.

  34. Kushal said

    Hey Madan, I was just asking if this code can be put in the MPLAB software and be made to work?
    Regards
    Kushal

  35. Apiz said

    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.

  36. Apiz said

    should i change this code to make both run but at the different direction?

    TRISB = 0×00;
    PORTB = 0×00;

  37. madan said

    it already has code to run in either directions. Just put them in diff functions and call when needed

  38. jayshri said

    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.

  39. susheel said

    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…

  40. Rajib said

    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

  41. Adnan_Merter said

    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

  42. geetha said

    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.

  43. Chetan said

    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

  44. [...] And here is the electronic connection I used for the ULN2803 you can find here. [...]

  45. shahin_198 said

    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).

  46. shahin_198 said

    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).

  47. aravinth s said

    @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

  48. Cyril said

    What about half stepping or microstepping. how can it be done? Does any one have code for that?

  49. biti said

    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.

  50. fabian said

    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

  51. gaurav said

    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

  52. naveen said

    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

  53. haitham said

    Unable to compile it in MicroC

  54. vivekz said

    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

  55. ssri said

    hi i need circuit and how to give pulse for 4 wire stepper motor

  56. ssri said

    hi
    i need circuit and how to give pulses for 4 wire stepper motor

  57. ehtesham said

    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

  58. ehtesham said

    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

  59. sibashis said

    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

  60. Elias said

    sir
    i new in stepper motor. I dont know how to derive a stepper motor using ULN2003.How i find out the stepper motor wire configuration.

  61. Awais said

    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

  62. thando said

    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!!!

  63. Awais said

    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

  64. sandesh said

    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

  65. ajeesh said

    plz send me the code 2 rotate 2stepper motors simultaneously & independently

  66. ope rawman said

    pls ineed help on circuit diagram using 2 stepper motor with 16f877a via uln2003

  67. ope rawman said

    ineed help on circuit of 2stepper motor with 16f877a via uln2003

  68. saran said

    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…

  69. somesh said

    I want to know circuit diagram of 28 v dc stepper motor with input equal to 0.01 v to 15 volts

  70. Amit said

    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.

  71. santosh said

    Hello sir i need the program in arm7tdmi based pograming

  72. M Zuhaib said

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

  73. Amjid Niazi said

    A lot of thanx dear…….
    Nice efforts…….

  74. Amjid Niazi said

    has someone control 2 dimentional motors through mouse using 59c51?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>