Shop OBEX P1 Docs P2 Docs Learn Events
Using a BS2 to control relays — Parallax Forums

Using a BS2 to control relays

jjohns63jjohns63 Posts: 9
edited 2009-02-06 02:16 in BASIC Stamp
Hi all, I'm hoping someone can help me out. I'm completely stuck. I've got a BS2 running the program below. Pins 1-5 are wired to transistors, driving several relays. Only 3 relays are ever engaged at a time. Each relay draws 79mA when engaged and the transistors have an amplification factor of ~180. The problem is that my program will completely restart, seemingly at random. Here's a diagram of my circuit, the circuit is repeated for each pin, 1-5:
Rube_circuit.PNG

Any suggestions will be most welcome. Let me know if you have any questions.
' {$STAMP BS2}
' {$PBASIC 2.5}

INPUT 14
Counter     VAR     Word

HIGH 15
Counter = 0

DO UNTIL Counter=10
  DEBUG "LOOPING", CR
  IF IN14=1 THEN
  Counter = Counter + 1
  ELSE
  Counter = 0
  ENDIF
  PAUSE 10
LOOP

DEBUG "start!",CR
LOW 15
PAUSE 1000             ' Wait a second before starting

HIGH 3                 ' Extend piston #1,
DEBUG "START PISTON #1", CR
PAUSE 1000             ' Wait a second for the piston to move

HIGH 2                 ' Spin motor,
DEBUG "START MOTOR", CR
PAUSE 7000             ' Wait several seconds for the bulb to unscrew

LOW 2                  ' Stop motor,
LOW 3                  ' Retract piston #1,
DEBUG "STOP MOTOR, PISTON #1", CR
PAUSE 1000             ' Wait a second for the piston to move

HIGH 4                 ' Extend piston #2,
DEBUG "START PISTON #2", CR
PAUSE 1000             ' Wait a second for the piston to move

HIGH 3                 ' Extend piston #1,
DEBUG "START PISTON #1", CR
PAUSE 1000             ' Wait a second for the piston to move

HIGH 1                 ' Spin the motor in the other direction
DEBUG "START MOTOR (REV)", CR
PAUSE 16000             ' Wait several seconds for the bulb to screw in

LOW 1                  ' Stop motor,
LOW 3                  ' Retract piston #1
DEBUG "STOP MOTOR, PISTON #1", CR

HIGH 5           ' Start train
DEBUG "START TRAIN", CR
PAUSE 7000         ' Wait for train to complete run
LOW 5           ' Stop train
DEBUG "DONE", CR

STOP

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-02 06:25
    I can't really read your diagram, but this sort of problem is almost always due to inadequate power (run down batteries) or noise spikes on the power supply from whatever you're controlling.

    Without a better and more complete diagram, it's hard to tell what's going on.
  • remmi870remmi870 Posts: 79
    edited 2009-02-02 06:54
    is that a pnp? it seems that the problem is your running the relays off of your 5v vdd and your most likely overloading your voltage regulator, besides running a seperate power supply for the relays (with common grounds) you should also think about putting a diode across the relay to protect your stamp. fyi, 4 relays plus the stamp will draw close to 300 ma, and your battery or power supply may be struggling.
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2009-02-02 06:56
    jjohns63,

    Looks like you need a current limiting resistor on the base of your transistors. Even though the current required for the Relays are relatively small, you still need a current resistor on the transistor base. A bipolar transistor is a current device and has a fixed voltage of about 0.6V ... driving it directly with 5V can damage it or cause excessive current drain from the Stamp which may be causing your resets.

    If the relay draws 79mA, a good rule of thumb is to double this value in the calculations. So the transistor needs to drive 158 mA in this case.
    If your Hfe or gain is 180, then the base current requirement would be about 0.88 mA. 158mA / 180 = .87...mA

    Since the Stamp is capable of driving the I/O at 5V the current limiting resistor value can be determined with the following formula....

    R = ( Vsource - Vbe ) / Ibe

    Where:
    Vsource = 5V (Voltage from the I/O)
    Vbe = 0.6V (Voltage across the Base Emitter transistor junction)
    Ibe = 0.88mA ( Current requirement across the Base Emitter transistor junction)


    R = ( 5V - 0.6V ) / 0.88mA
    R = 4.4V / 0.88mA
    R = 5000 Ohms .... It's ok to use the closest standard value of 4.7k Ohms in this case.



    Edit:

    You can use a PNP transistor only if the supply to the transistor is at the same voltage potential as vdd. That said
    you should not use the regulated vdd from the Stamp to drive your relays. A separate external regulator should be used
    to power your relays via the transistors.


    Check this link...
    www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol1/col/nv6.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 2/2/2009 7:09:06 AM GMT
  • jjohns63jjohns63 Posts: 9
    edited 2009-02-02 15:45
    Thank you all for your suggestions! I am using a NPN transistor and I do have a diode across the relay coil.

    Beau, if I replace the Vdd with another voltage source, do I need to use a regulator, or could i just use a 9V battery?


    EDIT:

    I'm using Fig B here: ourworld.compuserve.com/homepages/Bill_Bowden/r_ctrl.htm. I'm using Fig B because my relay coil voltage is 5V.

    Post Edited (jjohns63) : 2/2/2009 4:11:34 PM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2009-02-02 16:42
    jjohns63,

    If your voltage sources are different, use Figure A from the link that you provided.... Figure B even states that it "...can be used when the input voltage is the same as the relay coil voltage."

    Edit:
    Although take what you read with a grain of salt... I'm not sure I totally agree with some of the writers comments in that article.

    .... use "figure A", it's a popular configuration used in many applications.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 2/2/2009 4:53:35 PM GMT
  • jjohns63jjohns63 Posts: 9
    edited 2009-02-02 16:46
    OK, but the input is the I/O pin on the BS2, correct? then the +V on the diagram is what was Vdd but should be another voltage source?

    So my relays are 5V rated coils, therefore I should be able to use Fig B since the I/O pins are 5V. Or am I misunderstanding the diagram?
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2009-02-02 16:59
    I would not use "Figure B" ... the text is misleading. "Figure A" is your better bet... it will work with same voltage supplies or different voltage supplies.

    Yes, the input (transistor base) through a current limiting resistor goes to the BS2's I/O.

    Yes, the V+ should be another voltage source. It can be the same potential as Vdd (+5V), just coming from an External 5V regulator instead of the one that the BS2 provides. The reason is because the BS2's regulator simply can't handle a great deal of current, especially multiple relays at 100 mA or so.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • jjohns63jjohns63 Posts: 9
    edited 2009-02-02 17:08
    OK, that sounds good. I'll give it a try tonight. I thought it might be drawing too much current but didn't know what the limit was. It was working before so I was completely lost as to why it wasn't working now.

    I'll post here if it works or not, but I won't know for several hours.
  • jjohns63jjohns63 Posts: 9
    edited 2009-02-03 01:12
    Awesome!!! jumpin.gif It works great! Thanks so much for your help! smilewinkgrin.gif

    Edit:

    OK, I spoke too soon. I had several successful runs but after moving my circuit, it seems that it has stopped working consistently. I get random resets, same behavior as before. The reset doesn't always happen in the same place in the program. It seems that if I shorten my trigger switch wires, the program runs fine.

    Post Edited (jjohns63) : 2/3/2009 2:12:18 AM GMT
  • ercoerco Posts: 20,256
    edited 2009-02-03 02:45
    Your figure A with a 2N2222 and a 330 ohm bias resistor never disappoints. Sketch attached.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."

    Post Edited (erco) : 2/3/2009 3:00:17 AM GMT
  • jjohns63jjohns63 Posts: 9
    edited 2009-02-06 02:16
    It looks like my BS2 was bad. I tried with another one I had sitting around and everything works perfectly. I've got a replacement on order with Mouser.

    Thanks for all your help everyone.
Sign In or Register to comment.