1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Stepper motor control with PIC16F84A

Discussion in 'Microcontrollers' started by netbug, May 6, 2004.

  1. Exo

    Exo Active Member

    Joined:
    Sep 18, 2003
    Messages:
    1,953
    Likes:
    1
    Location:
    Belgium
    You could use simple serial communications. Nigel's tutorials have code to do rs232 in software on a pic.
     
  2. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    I have a new problem. I am using 5 switches connected to a PIC. One of them is the START button, and the others are options (A,B,C and D) to a question that appears on LCD. Sometimes everything works fine, but some other times the system assumes that I pressed C option, when I simply pressed START button. Please note that START button and option buttons are in different ports, and that the C switch isn't connected in the board yet.

    Should this be a code error ? Or can this be a debounce problem ? You can find below the code of the first PIC.

    Regards,
    Pedro Cardoso

    TRISA = %11111 '5 pins as inputs
    TRISB = %00000101 'bits 0 and 2 as inputs

    ton var Byte ‘ Stepper time on
    toff var Byte ‘ Stepper time off
    j var Word
    tpd var Word ‘ Number of turns per day given by the stepper

    sentido var Bit ‘ Direction of rotation
    n_passos var Byte ‘ Number of steps
    max_passos var Byte ‘ Maximum number of steps
    start_stop var Byte ‘start/stop flag
    valor_enviar var byte ‘ Value to send to 2nd pic
    start_stop = 0

    max_passos = 200 ‘ Stepper with 200 steps
    PORTB = %00000000
    PORTA = %00000

    ' Wait for start button to be pressed
    loop_inicio:
    If PORTB.2 = 0 Then
    Goto loop_inicio
    Endif

    TRISB = %00000001
    portb=%00000000

    ' Start transmitted to second pic
    serout2 portb.1,16780,["S"]

    'Choose na option (A,B,C or D)
    loop1:

    'Wait till a button is pressed
    If PORTA.0 = 1 Then
    ton = 1 ' Provisorio
    toff = 0 ' Provisorio
    tpd = 600
    valor_enviar=1
    serout2 portb.1,16780,["B",valor_enviar]

    Else
    If PORTA.1 = 1 Then
    ton = 1 ' Provisorio
    toff = 1 ' Provisorio
    tpd = 800
    valor_enviar=2
    serout2 portb.1,16780,["B",valor_enviar]


    Else
    If PORTA.2 = 1 Then
    ton = 3
    toff = 3
    tpd = 1000
    valor_enviar=3
    serout2 portb.1,16780,["B",valor_enviar]

    Else
    If PORTA.3 = 1 Then
    ' ton = 42
    ' toff = 18
    tpd = 1200
    valor_enviar=4
    serout2 portb.1,16780,["B",valor_enviar]

    Else
    Goto loop1
    Endif
    Endif
    Endif
    Endif

    portb.1=0
    portb.2=0
    portb.3=0

    'Os botões, a partir de agora, ficam inactivos excepto o do sentido
    TRISA = %10000

    ton_seg = ton ' Provisorio
    toff_seg = toff ' Provisorio
    n_passos = 0

    ' Rotation direction (CW or CCW)
    If PORTA.4 = 1 Then
    sentido = 1
    Else
    sentido = 0
    Endif

    loop2:

    If sentido = 1 Then
    Gosub motor_run1
    Else
    Gosub motor_run2
    Endif

    ton_seg = ton_seg - 1

    If ton_seg = 0 Then
    If toff_seg <> 0 Then
    PORTB = %00000000 'Stop stepper
    For j = 1 To toff_seg
    pause 5 ' Provisorio
    Next j
    Endif
    Endif
    Goto loop2

    End
    motor_run1:
    PORTB = %00110000
    'mr1 -> loop motor run1
    mr1:
    'While PORTB <> %1001000
    'WaitMs 250
    pause 1 ' Sentido de rotação escolhido pelo 5º switch

    'Calcula os passos dados pelo motor
    If n_passos <> max_passos Then
    n_passos = n_passos + 1
    Else
    n_passos = 0
    Endif

    If PORTB = %10010000 Then
    PORTB = %00110000
    Else
    PORTB=PORTB << 1

    'Se houver carry põe o bit menos significativo a 1 (RB4)
    If PORTB = %10000000 Then PORTB = %10010000

    Endif

    If PORTB <> %10010000 Then Goto mr1
    pause 1
    'Wend
    Return
    motor_run2:

    PORTB = %10010000
    mr2:
    pause 1

    'Calculates number of steps
    If n_passos <> max_passos Then
    n_passos = n_passos + 1
    Else
    n_passos = 0
    Endif

    If PORTB = %10010000 Then
    PORTB = %11000000
    Else
    If PORTB = %00110000 Then
    PORTB = %10010000

    Else
    PORTB =PORTB>>1
    Endif
    Endif
    If PORTB <> %00110000 Then Goto mr2
    pause 1
    Return

    '1 minute delay
    delay_1min:

    i var Byte

    For i = 1 To 60
    pause 1000
    Next i
    Return
     
  3. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    I tested the code and the circuit under proteus, and it works fine. I discovered that if I touch the ground with my finger the circuit works fine, if I don't, it works poorly.

    So how can I solve a ground problem like this one ?? I am powering the circuit with those transformers that we use to charge a laptop or mobile phone. In this case I am using the power supply from my laptop. How can I make a stable ground from here ?

    Please try to answer fast because I want to finish this asap.

    Many thanks,

    Pedro Cardoso
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK

    Post your complete circuit diagram, don't leave anything out, and we can make suggestions - there's no requirement for a ground connection, so it sounds like you're perhaps picking up hum and interference?.
     
  6. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    I found that I left unconnect two input pins of the pic (because I had no more switchs), so sometimes pic assumed that on of these pics were high. I think I should have grounded these unused pins.

    Is there a way to do a small circuit that, if you connect external power, it will use this power; if you disconnect the external power the system will run on batteries without turnning off. Just like mobile phones or laptops do.

    Best regards,

    Pedro Cardoso
     
  7. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    What's best to do with unused pins is quite controversial, but the concerns are to do with keeping the lowest supply current. If you don't test the pins in your program, then being high or low makes no possible difference!.

    An obvious easy solution, is to make sure you DON'T check unused pins (and why would you?), and if you set the pins as outputs there's no possible problem anyway.

    A simple diode OR gate is all that's needed, and make sure that the incoming supply is higher than the battery voltage.
     
  8. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    I am not following your idea. Can you be more specific,please. I want to connect the power supply(lets say 12V) or use internal 9V battery. I think the decision must be taken before the regulator. How can I do this with only an OR gate ?

    I found a solution for this problem but requires an IC (MAX6326).

    http://www.maxim-ic.com/appnotes.cfm/appnote_number/1195

    I also thought of using a relay to switch between the two power sources.

    Best regards,

    Pedro Cardoso
     
  9. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    As I said, it's VERY simple to do, this is all you need! - your 12V (from a mains adaptor) connects to where it says 'mains in', and the 9V battery connects where it says 'battery in' (or the other way round, it obviously makes no difference). The output feeds to the input of the voltage regulator.
     

    Attached Files:

  10. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    I am using an ULN2803 to drive a stepper. I want to supply 5v to my stepper, but the output pins of 2803 reach 3V (max). I am connecting the pic to pins 1,2,3,4 of the ULN2803. Pins 9 and 10 of 2803 are connected to gnd and +5V. If I increase the pin 10 voltage to 12V, will I get 5 V at the output of the 2803 ?

    Best regards,

    Pedro Cardoso
     
  11. eblc1388

    eblc1388 Active Member

    Joined:
    Jan 25, 2005
    Messages:
    2,228
    Likes:
    18
    Location:
    UK
    NO. The ULN2803 does not give you any output voltage. It is just a open collector transistor with driving transistor (darlington connection).

    The function of ULN2803 is like a switch. Its output pin passes current to ground or open circuit, depending on the input.

    If you increase the pin10 voltage to +12V, then the ULN2803 can drive a load with +11V or more. So if you want to drive a stepper with 5V, then provide 5-6V for pin 10 and the common coil terminal of the stepper motor.
     

    Attached Files:

  12. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    Hi,

    Thanks for your answer. But the question is the stepper is rotanting very low speed(1 step every 250 ms), and even this way the torque is very low. As I measured the voltage between common (pin 10) and every output of the 2803, I thought that this low torque would be due to low voltage between common and output pin.

    Another question, my stepper is a 5V (I can't remeber the reference) motor from Astrosyn. The point is the 7805 gets very very hot, so I suppose the stepper is sinking to much current. Can I solve this problem using several 7805 in paralell ?

    Best Regards,

    Pedro Cardoso
     
  13. eblc1388

    eblc1388 Active Member

    Joined:
    Jan 25, 2005
    Messages:
    2,228
    Likes:
    18
    Location:
    UK
    Not knowing which stepper motor you are using , it is difficult to advise you on the power supply issues.

    Click the following link to get the Astrosyn stepper motor datasheet and tell us which model you are using in your circuit.

    http://www.astrosyn.com/docs/pms.pdf
     
  14. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Pedro (Netbug),

    I experimented with some 5v/1a stepper motors earlier this year... When I first ran them from a 7805 regulator, my regulator also got very hot and the steppers did not have very good torque... Also, when I tried to drive two coil windings at the same time, the 7805 would shut down...

    I suspect multiple 7805 regulators would work ok if your power supply can supply the extra power...

    I installed 5 watt current limiting resistors in series with both "common" coil windings and connected these "common" windings to the 12 volt (3 amp) source on the "input side" of my 7805 regulator and the steppers have excellent torque and speed now...

    Good luck with your project... Regards, Mike
     

    Attached Files:

  15. motion

    motion New Member

    Joined:
    Jul 14, 2003
    Messages:
    560
    Likes:
    0
    Location:
    Quezon City.PH
    Before answering this question. Let's sort this stepper motor problem first. Earlier, you said that you're getting about 3V at the output of the 2803. If this is the voltage of the pin w.r.t. ground and your power supply is 5V then the voltage being applied to the motor coils is only about 2V. This should explain why you are getting very low torque.
    My advice is to look and study carefully the rating (voltage & current) of the motor. Motors of this type have these values or volts/ohms rating on the sticker at the end cap. From them you can calculate the current requirements. Remember that the ULN2803 is only rated 500mA. I suspect the motor is rated higher than this given the low voltage rating.

    I would suggest to look for a higher voltage rated motor (about 12V or higher). This way, the current requirements will be lower for the same torque.
     
  16. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    eblc1388,

    I Can't really remember the reference, but I can assure you that I chose a 5V stepper with 1A max current(I can't remeber the exact value). The problem is I can't download the pdf from astrosyn, there is some error when I try to do it. The stepper I own is an hybrid stepper and can be found in L-series catalog.


    Mike, K8LH,

    Thank you for your idea. What was the value of the two resistors that you used ? 2.2 ohm ?

    Thanks,

    Pedro
     
  17. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Pedro,

    If your steppers are 1 amp, are you then driving each coil winding with two '2803 outputs?

    I almost used a '2803 for my steppers but I was worried about running two outputs simultaneously at the full 500-ma spec'... I guess I figured that tiny little DIP-18 chip would have to get real hot... I ended up using TIP-122 Darlington transistors and they don't even get warm...

    I used 5-ohm 5-watt resistors but they probably should be more like 7 or 8 ohms... Another advantage of the "current limiting" scheme is that when you first energize a coil, it gets a boosted voltage until the current ramps up enough to develop the voltage drop across the resistor... This provides excellent torque...

    Regards, Mike
     

    Attached Files:

  18. eblc1388

    eblc1388 Active Member

    Joined:
    Jan 25, 2005
    Messages:
    2,228
    Likes:
    18
    Location:
    UK
    I know. The darn website use a broken link. Try replacing the "co.uk" part of the link with "com" and it will work.

    Mike, K8LH:

    The losses in the transistors will be lower if the current limiting resistors is inserted between Ax & BX & 12V.
     
  19. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Thank you Sir... Regards, Mike
     
  20. netbug

    netbug New Member

    Joined:
    May 6, 2004
    Messages:
    51
    Likes:
    0
    Location:
    Lisbon - Portugal
    eblc1388,

    You're right the link is broken because astrosyn.co.uk is down.

    Mike, K8LH,

    When I bought the stepper I had the specs of the 2803 on my hand, so the stepper must be 500 mA (max). I am sorry for the confusion. But I measured the total current in circuit and it gave me something around 350 mA. The 2803 didn't even get warm, it is cold all the time. The heat problem is only with the 7805.

    I will try to put the resistors and I will post again with the correct data from the stepper.

    Pedro
     
  21. eblc1388

    eblc1388 Active Member

    Joined:
    Jan 25, 2005
    Messages:
    2,228
    Likes:
    18
    Location:
    UK
    You would lost around 1.5V across the 2803 at this current. Mike's advice of using two output pins to drive one coil is very good.

    Is 7805 on a heatsink and what is the input voltage to 7805? Have you added bypass capacitors on output pin of 7805 to prevent it from oscillating?
     

Share This Page