Stepper motor control

Discussion in 'Embedded Systems and Microcontrollers' started by KansaiRobot, Apr 26, 2015.

  1. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Hello everybody and thank you always for the help you have given me in my past projects.

    Today I am starting a new project which is: To control a stepper motor from a PIC (the one I have 18F2550)

    Now, I am reading a lot about the theory behind stepper motors so I kinda catch some concepts.

    In the past I controlled a DC motor through PWM using a H-bridge. Surprisingly I did not use PWM for the input pins but for the voltage of reference. The inputs were simply 1 or 0. It worked quite well.

    Now I am sure I am going to have a lot of questions regarding stepper motors and I will appreciate your help but just to start let me ask you this question:

    1) Is PWM somehow useful for stepper motors?? I am reading and it seems the speed of the motor is changed through the delays you give while powering in and out the coils in the stator with 1 or 0, ergo digital inputs so it seems something like PWM would not be needed at all.

    Thanks a lot for your help
    Last edited: Apr 27, 2015
  2. tracecom

    AAC Fanatic!

    Apr 16, 2010
    Google PWM + stepper. Lots to read.
  3. MaxHeadRoom


    Jul 18, 2013
    Stepper motors are just that, they require a series of stepped pulses to the winding's the faster the pulse the higher the rpm.
    A driver typically amplifies the pulses together with a 1 or 0 direction indication pulse.
  4. ScottWang


    Aug 23, 2012
    KansaiRobot likes this.
  5. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Thank you for your reply. I am not really sure it has anything to do with what I asked (PWM) though:oops:

    but related to stepper motors it was useful. I have some questions though. You are using the CD4017 which is a counter and the ULN2003 to control the motor. (I haven't analyzed the NE555 but I suppose it is some kind of timer). So since this motor is unipolar you dont need a H-bridge or something, just these darlington transistors. That is quite understood.

    However, I see the sequence forward and backward as : A-> -A-> B-> -B-> -B-> B-> -A-> A right?
    The motor I am using though recommends AB-> -AB -> -A-B -> AB- for going forward.

    Is this just a matter of choice of step modes??

    Also (and very important) I am going to be using a motor that operates with 24V. Should I use the ULN2002A instead? ( I read in the datasheet that this is designed for this)
    Last edited: Apr 27, 2015
  6. ScottWang


    Aug 23, 2012
    The testing circuit was used to let you know how the stepper motor working, and if you change the input pulse to pwm then it will be a pwm stepper motor testing circuit.

    NE555 Clock Generator and PWM adjustable circuit.

    The ne555 is a pulse generator, you could treat it as a system delay of uC, CD4017 as the Output port, you could adjust the duty cycle of 555 as using a pwm, and you can set different delay time of uC that it as the duty cycle of pwm.

    Yes, that's right.

    You have to choosing the type of stepper motor to suit your application.

    Uln2002A was designed for higher voltage input then the uln2003.

    The uln200x series darlington driver providing 500 mA for total output or each output, but you better not use it over total 100 mA, so if your current of 24V stepper motor is <= 100 mA then it's ok, otherwise you should using some other bjt to replace it as mje3055 for each channel.

    The package is difference.

    ULN200x series darlington driver -- you can check the end of the several pages.

    Arduino Hardware PWM for stepper motor drives.
    KansaiRobot likes this.
  7. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Thank you very much for your helpful post.
    I think I was going to burn something!:confused: (I cant read datasheets to save my life!)

    Reading the documentation of the motor I have, it says:

    Driving Circuit:  Drive by SLA7033M
    On Drive at DC24V Current 0.7A/Phase
    1-2 phase excitation

    So this means that the ULN2002 is not for this right? I am now going to read the datasheet of the SLA7033M to see what is this thing

    EDIT. And now I see that maybe a SLA7052M could be used! but my store only has the SLA7026!
    Last edited: Apr 27, 2015
  8. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Well I couldnt find the SLA7033M anywhere, not even on the makers' site!
    So I think I am going for the SLA7026M.
    Right now will start how to wire this to control the motor...
  9. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Doesn't give much of useful readings.. but I found this page Motor Controller/StepperMotor.html

    where it says "These are the main reasons for using PWM. It is not used to control the RPM. "

    So I guess RPM is not used as with DC motors to control the speed...but to drive a 12V motor with 24V (what for ? I have no idea)
  10. Alec_t

    AAC Fanatic!

    Sep 17, 2013
    Sure you can use PWM to control DC motor speed.
    The drive coils of a stepper motor have inductance, which resists sudden changes of current. So if you want your motor to step fast you need to 'speed up' the current rise rate. You can do that by increasing the drive voltage above the rated voltage (e.g. to 24V), but you have to control the average current draw of the coils to prevent coil burn-out, hence the use of PWM.
    KansaiRobot likes this.
  11. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Wow, that just blew my mind... I am going to use the holiday (tomorrow is holiday here) to process it :(

    EDIT: May 22, I finally understood what you said thanks :)

    Anyway, I am planning to build the following circuit:


    Now, I am not really sure what C5 should I use. (in one datasheet, there is no need for this capacitor so maybe i will use none?) and for the others I am not sure if I should use polarized capacitors or not...
    Also somewhere I saw a sign similar to a resistor but inside a rectangle... I wonder what is this...

    The inputs A, ~A , B and ~B , I am going to connect them directly to my PIC, and I suppose I am going to provide 1s and 0s there... not sure where to put PWM yet :confused:

    I have only few days left to do this and I am a bit overwhelmed....

    Thanks always for your help, any recommendation or advice warmly welcomed

    Edit: Also I am thinking some way of putting a LEDs between the PIC output and the driver inputs... So far I have put leds a resistor and ground, wonder if it will work here too
    Last edited: May 21, 2015
  12. Alec_t

    AAC Fanatic!

    Sep 17, 2013
    I'd use a 100uF 35V electrolytiv for C5. The other caps aren't polarised types.
    KansaiRobot likes this.
  13. ScottWang


    Aug 23, 2012
  14. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Alright, I haven't implemented the board yet, probably will do it in a couple of days, but now I am trying to write the software. (I am a software guy after all :p)
    I suppose my first attempt will be a very simple one and I will post the code here (welcome any advice) and it won't have any way to regulate the speed, just go back and forward.

    I suppose regulating speed will involve modifying the delays between changing phases...

    I haven't been able to understand what PWM has to do with this except to "regulate voltage"..
    Any tutorial or document on this in the web that you recommend?

    Thanks a lot
  15. ScottWang


    Aug 23, 2012
    The PWM related with regulated voltage that It's depends on the duty cycle.
    Examples: Vcc=5V
    pwm 10%, Vo = 5V*10% = 0.5V.
    pwm 30%, Vo = 5V*30% = 1.5V.
    pwm 50%, Vo = 5V*50% = 2.5V.
    pwm 75%, Vo = 5V*75% = 3.75V.
    pwm 100%, Vo = 5V*100% = 5V.
  16. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Thank you for your reply., I understand the concept of PWM and how it implements a "analog" voltage using only ons and offs of a fixed voltage, in your example 5V.

    What I don't understand is, where do I put this signal in the whole circuit I am building...? if you see my code in the next post, it seems all I need is 1's and 0's....
    Last edited: Apr 30, 2015
  17. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Okay, I draft a program like the following:

    Code (Text):
    2. /*
    3.  * File:   main.c
    4.  * Author: KansaiRobot
    5.  * In this first attempt we are trying to build the sequence of activation for the inputs of
    6.  * a stepper motor controller.
    7.  * According to the datasheet for
    8.  * 2-phase (full step) operation this should be:
    9.  * AB->~AB->~A~B->A~B  and the opposite direction if going backward
    10.  *
    11.  * For half-step operation this would change to
    12.  * A->AB->B->~AB->~A->~A~B->~B->A~B->A
    13.  * Created on 2015/04/30, 12:04
    14.  */
    16. #include <stdio.h>
    17. #include <stdlib.h>
    18. #include <p18f2550.h>
    19. #include <delays.h>
    21. #pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
    22. #pragma config WDT = OFF //Disable watchdog timer
    24. /*  I am going to use the pins:
    25.  *          RB0  --> in A
    26.  *          RB1  --> in~A
    27.  *          RB2  --> in B
    28.  *          RB3  --> in~B
    29.  *  For the control lines that are going to go to the SLA7026M
    30.  * I only need to control the sequences
    31.  *
    32.  */
    34. #define IN_A    LATBbits.LATB0
    35. #define IN_NA   LATBbits.LATB1
    36. #define IN_B    LATBbits.LATB2
    37. #define IN_NB   LATBbits.LATB4
    39. #define TRISB0 TRISBbits.TRISB0
    40. #define TRISB1 TRISBbits.TRISB1
    41. #define TRISB2 TRISBbits.TRISB2
    42. #define TRISB3 TRISBbits.TRISB3
    45. int main(int argc, char** argv)
    46. {  int count=0;  // to count forward and backward
    47. // First we define the PORT B as output
    48.     TRISB0=0;
    49.     TRISB1=0;
    50.     TRISB2=0;
    51.     TRISB3=0;
    53.     while(1)
    54.     {
    55.        //First Forward
    56.       for(count=0;count<=250;count++)
    57.       {  //AB->~AB->~A~B->A~B
    59.      // Step 1  AB
    60.           IN_A=1;
    61.           IN_NA=0;
    62.           IN_B=1;
    63.           IN_NB=0;
    65.       //HERE DELAY
    67.      //Step 2 ~AB
    68.           IN_A=0;
    69.           IN_NA=1;
    70.           IN_B=1;
    71.           IN_NB=0;
    73.       //HERE DELAY
    75.        //Step 3 ~A~B
    76.           IN_A=0;
    77.           IN_NA=1;
    78.           IN_B=0;
    79.           IN_NB=1;
    81.       //HERE DELAY
    83.        //Step 4 A~B
    84.           IN_A=1;
    85.           IN_NA=0;
    86.           IN_B=0;
    87.           IN_NB=1;
    89.       //HERE DELAY
    91.       }
    93.       // Then Backward
    94.       for(count=0;count<=250;count++)
    95.       {//AB->A~B->~A~B->~AB
    97.      // Step 1  AB
    98.           IN_A=1;
    99.           IN_NA=0;
    100.           IN_B=1;
    101.           IN_NB=0;
    103.       //HERE DELAY
    105.      //Step 2 A~B
    106.           IN_A=1;
    107.           IN_NA=0;
    108.           IN_B=0;
    109.           IN_NB=1;
    111.       //HERE DELAY
    113.        //Step 3 ~A~B
    114.           IN_A=0;
    115.           IN_NA=1;
    116.           IN_B=0;
    117.           IN_NB=1;
    119.       //HERE DELAY
    121.        //Step 4 ~AB
    122.           IN_A=0;
    123.           IN_NA=1;
    124.           IN_B=1;
    125.           IN_NB=0;
    127.       //HERE DELAY
    129.       }
    132.     }//while 1
    134.     return (EXIT_SUCCESS);
    135. }
    Now, you see there is some part where I put " //HERE DELAY" and I am wondering about this.
    How much delay is the appropriate (I suppose that later I will change this dynamically with the values from a potentiometer so in that case "appropriate ranges") for controlling a stepper??

    I read somewhere some code use Delay100TCYx(45) and other use Delay100TCYx(100). I don't know the Tosc of them but I am supposing this (the later) is something like 0.01s at 4MHz.
    I myself am using the internal oscillator at 1MHz....
  18. ScottWang


    Aug 23, 2012
    Assuming that the current of stepper motor driver is enough to drive.

    You have to figure out what's the meaning of number in Delay100TCYx(xx), if you don't know, maybe you can try it from 1 as 1, 5, ,10 ,20, 50...
  19. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Thank you for your reply. I more or less figure it out already the meaning of these functions, it involves calculations with the frequency of the oscillator and the fact that each instruction takes 4 cycles. What I would like to know (and maybe I should just experiment) is how much delay in ms is typical for a stepper...

    Now the current of the driver is another thing I have to think later :confused: (sigh)
  20. ScottWang


    Aug 23, 2012
    Although you can using pot and adc and software through pwm to control the speed, but when the duty cycle of pwm more width is more dangerous, specially in low frequency.

    You could measure the output port to check the real frequency for each step without any connection to bjts.