Stepper motor control

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
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:

MaxHeadRoom

Joined Jul 18, 2013
28,688
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.
Max.
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
To get to know the theory of stepper motor, go to study this -
Stepper Motor Forward And Reverse Testing circuit.

When you know the theory then you can using the software to control it.
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:

ScottWang

Joined Aug 23, 2012
7,400
Thank you for your reply. I am not really sure it has anything to do with what I asked (PWM) though:oops:
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.

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

However, I see the sequence forward and backward as : A-> -A-> B-> -B-> -B-> B-> -A-> A right?
Yes, that's 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?
You have to choosing the type of stepper motor to suit your application.

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

Also is the ULN2003AN similar. In the data sheet only says ULN2003A and my electronic shop sells the AN... are they different?
The package is difference.

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

Arduino Hardware PWM for stepper motor drives.
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
Thank you very much for your helpful post.
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 bipolar junction transistor to replace it as mje3055 for each channel.
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:

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
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...
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324

Alec_t

Joined Sep 17, 2013
14,314
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.
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
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:

schemeit-Controller.png

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:

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
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
 

ScottWang

Joined Aug 23, 2012
7,400
...
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
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.
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
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.
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:

Thread Starter

KansaiRobot

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

Code:
/* 
 * File:   main.c
 * Author: KansaiRobot
 * In this first attempt we are trying to build the sequence of activation for the inputs of
 * a stepper motor controller.
 * According to the datasheet for
 * 2-phase (full step) operation this should be:
 * AB->~AB->~A~B->A~B  and the opposite direction if going backward
 *
 * For half-step operation this would change to
 * A->AB->B->~AB->~A->~A~B->~B->A~B->A
 * Created on 2015/04/30, 12:04
 */

#include <stdio.h>
#include <stdlib.h>
#include <p18f2550.h>
#include <delays.h>

#pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
#pragma config WDT = OFF //Disable watchdog timer

/*  I am going to use the pins:
 *          RB0  --> in A
 *          RB1  --> in~A
 *          RB2  --> in B
 *          RB3  --> in~B
 *  For the control lines that are going to go to the SLA7026M
 * I only need to control the sequences
 * 
 */

#define IN_A    LATBbits.LATB0
#define IN_NA   LATBbits.LATB1
#define IN_B    LATBbits.LATB2
#define IN_NB   LATBbits.LATB4

#define TRISB0 TRISBbits.TRISB0
#define TRISB1 TRISBbits.TRISB1
#define TRISB2 TRISBbits.TRISB2
#define TRISB3 TRISBbits.TRISB3


int main(int argc, char** argv)
{  int count=0;  // to count forward and backward
// First we define the PORT B as output
    TRISB0=0;
    TRISB1=0;
    TRISB2=0;
    TRISB3=0;

    while(1)
    {
       //First Forward
      for(count=0;count<=250;count++)
      {  //AB->~AB->~A~B->A~B

     // Step 1  AB
          IN_A=1;
          IN_NA=0;
          IN_B=1;
          IN_NB=0;

      //HERE DELAY

     //Step 2 ~AB
          IN_A=0;
          IN_NA=1;
          IN_B=1;
          IN_NB=0;

      //HERE DELAY

       //Step 3 ~A~B
          IN_A=0;
          IN_NA=1;
          IN_B=0;
          IN_NB=1;

      //HERE DELAY
         
       //Step 4 A~B
          IN_A=1;
          IN_NA=0;
          IN_B=0;
          IN_NB=1;

      //HERE DELAY

      }

      // Then Backward
      for(count=0;count<=250;count++)
      {//AB->A~B->~A~B->~AB

     // Step 1  AB
          IN_A=1;
          IN_NA=0;
          IN_B=1;
          IN_NB=0;

      //HERE DELAY

     //Step 2 A~B
          IN_A=1;
          IN_NA=0;
          IN_B=0;
          IN_NB=1;

      //HERE DELAY

       //Step 3 ~A~B
          IN_A=0;
          IN_NA=1;
          IN_B=0;
          IN_NB=1;

      //HERE DELAY

       //Step 4 ~AB
          IN_A=0;
          IN_NA=1;
          IN_B=1;
          IN_NB=0;

      //HERE DELAY

      }


    }//while 1

    return (EXIT_SUCCESS);
}
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....
 

ScottWang

Joined Aug 23, 2012
7,400
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...
 

Thread Starter

KansaiRobot

Joined Jan 15, 2010
324
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...
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)
 

ScottWang

Joined Aug 23, 2012
7,400
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.
 
Top