Page 1 of 1

Controlling a relay with a basic stamp

Posted: Sun Jan 09, 2005 6:22 pm
by Mike
I want to control a relay with a basic stamp. Is there enough current output from the chip to close the contacts? If so, do I just connect it from an input/output pin to ground? Or, if I need a transistor between the stamp and relay? If so, how do I connect that?<p>Thanks!

Re: Controlling a relay with a basic stamp

Posted: Sun Jan 09, 2005 8:28 pm
by jimandy
You might look at this site:<p>http://www.rentron.com/PICX6.htm<p>for your info.

Re: Controlling a relay with a basic stamp

Posted: Sun Jan 09, 2005 11:03 pm
by philba
well, it really depends on the relay but usually the answer is no. If its not a 5 V coil (or 3.3V) or has a coil resistance below 250 ohms then you need a driver circuit. The good news that even if the relay can't be driven directly, its really easy and cheap to drive a relay from a microcontroller. <p>A relay coil will have a resistance and thus pull a certain amount of current. I have used a 5V relay in several projects with PICs. On this particular model of relay, the coil resistance is 65 ohms. Ohms Law tells us that the current through the coil will be 80 mA (5/65) which is much too high for a micro.<p>I use a cheap NPN transistor as a driver - a 2N2222 in one case but just about any small signal transistor with an Hfe of 50 or better will work. Here is the schematic of the circuit I used - http://www.geocities.com/phil1960us/relay.jpg (the funny box is eagle's idea of a relay coil). Rly-dn is the signal from the micro.<p>You need to determine the current of the source signal and then determine how much current gain you need to drive the relay. Once you do that then you can pick the base resistor value. I used 1.6 mA as the value from the PIC (this is completely arbitrary but well within the capacity of the PIC or basic stamp). With a minimum gain (Hfe) of 50, 1.6 mA gets me 80 mA to drive the relay coil. The base resistor sees 4.3V (5V minus the NPNs voltage drop of 0.7V). Ohm's Law tells us that the resistor should be 2687.5 which is very close to 2700, a standard value. To be completely safe, I should have gone to a lower value (like 2K) but 2.7K works fine because the 2N2222 has a higher Hfe in reality.<p>Don't forget a diode across the coil to prevent the emf kickback from destroying the transistor.<p>Phil<p>[ January 09, 2005: Message edited by: philba ]</p>

Re: Controlling a relay with a basic stamp

Posted: Mon Jan 10, 2005 4:49 am
by dyarker
I aggree with philba, plus some additional food for thought:<p>It's easier to assume when starting a project, that relay drivers will be used. The 1N400x diodes are okay for spike suppression, but aren't real fast. The transistor and the base resistor are an additional layer of protection between spikes and your microcontroller. Also, with the transistor, your choise of coil voltages is wider.<p>For example if the unregulated supply voltage is higher than like 10V, and use 12V relays.<p>Cheers,

Re: Controlling a relay with a basic stamp

Posted: Mon Jan 10, 2005 5:51 am
by jimandy
In a similar situation I used a mosfet (2n7000). Eliminates the base resistor needed in the BJT approach. I'm sure Hill & Horowitz would be "Horowfied" that there wasn't some sort of isolating resistor there but I've had no problems in real life with that approach. And, yes, I know, the mosfet is more expensive.

Re: Controlling a relay with a basic stamp

Posted: Mon Jan 10, 2005 7:59 am
by philba
<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by jimandy:
In a similar situation I used a mosfet (2n7000). Eliminates the base resistor needed in the BJT approach. I'm sure Hill & Horowitz would be "Horowfied" that there wasn't some sort of isolating resistor there but I've had no problems in real life with that approach. And, yes, I know, the mosfet is more expensive.<hr></blockquote><p>I didn't suggest a FET because of the price issue - small signal NPNs are typically about $.10. FETs are definitely nicer otherwise (as long as they have a small Rds).<p>By the way, H&H have a lot of pages on the emitter follower bipolar case. No base resistor, load between emitter and ground. Only works where coil V = base V (i.e. 5V coil with a 5V micro). I like this config for driving LCD display backlights (LED) and other non-inductive loads. <p>For a small relay, you don't have to worry too much about the speed of the diode. Use what ever you've got. I would worry, however, if it was a motor or larger relay. schottkys would be the right approach there. <p>As with anything, there are about 10 ways to do it right and 100 ways to do it "almost" right. The problem is that "almost right" is very hard to distinguish from right.<p>Have fun<p>Phil

Re: Controlling a relay with a basic stamp

Posted: Mon Jan 10, 2005 1:29 pm
by Mike
Thanks to everybody.<p>I'm going to use philba's circuit because I have all the parts.<p>I assume I just control it with HIGH X, with X being the number of the pin it on?<p>Another thing I want to do is display the output on an LCD. I have a parallel LCD. Is it simple to connect and control it through a basic stamp?

Re: Controlling a relay with a basic stamp

Posted: Mon Jan 10, 2005 4:13 pm
by philba
I don't know PIC Basic (well, I didn't inhale, anyway) but that sounds somewhat correct. A high level on the pin connected to the driver circuit will cause the relay to close (or open for NC contacts).<p>LCD - this isn't a trivial thing to do from scratch in Basic but not impossible. However, I would bet your basic has direct support for standard character LCDs. I know several do. check your manual.

Re: Controlling a relay with a basic stamp

Posted: Mon Jan 10, 2005 5:51 pm
by hp
It depends on what basic stamp you have. All the basic stamps other than the BS1 have native parallel lcd support.<p>Harrison

Re: Controlling a relay with a basic stamp

Posted: Wed Jan 12, 2005 5:20 pm
by Mike
Its a basic stamp 2.<p>How would I connect the LCD to it?<p>About the relay, I built the circuit, and all it does is make the relay buzz. It's a 12V relay. Is that too high even for a transistor?<p>Another option I have is to use a solid state relay. the one I have is a 3-32VDC input, and 120VAC switch contacts. Are these things reliable enough to trust with this circuit? I just don't know if I completely trust it. What it's turning on is the power for a thermoelectric cooler power supply.<p>I have access to some X10 equipment, maybe, if I have enough of it, I'd be better off using that?

Re: Controlling a relay with a basic stamp

Posted: Wed Jan 12, 2005 7:48 pm
by hp
Are you sure your code for the basic stamp 2 is correct? What happens when you pull the transistor's base resistor to 5v? Does the relay stay on?<p>Solid state relays can only switch AC. If you want to switch the power supply's AC input then a solid state relay should be fine. It would also be quieter and more efficent in the aspect that it won't require as much current to turn and stay on.<p>Look in the basic stamp 2 manual for lcd info. I believe it has a sample schematic and a code snippet for controlling a parallel lcd.<p>Harrison<p>[ January 12, 2005: Message edited by: hp ]<p>[ January 12, 2005: Message edited by: hp ]</p>

Re: Controlling a relay with a basic stamp

Posted: Wed Jan 12, 2005 8:13 pm
by dyarker
Mike,<p>How much current does the relay coil need, and what is the resistance of the base resistor?

Re: Controlling a relay with a basic stamp

Posted: Wed Jan 12, 2005 10:04 pm
by philba
No, a 12V relay wont work this way. You need to give 12V to the coil. Well actually, it's probably like 9V but 5 is way to low. The 2N2222 can handle 12V no problem.<p>All is not lost. In the circuit I posted, where it says +5V (above the relay coil), connect +12V instead. make sure you have a common ground. You probably should recalculate the current gain needed but I bet it will work since the current needs tend to go down as the coil V goes up. Try it but do check the transistor to see if its getting hot. <p>One issue to consider is the diode. The 1N4001 I had in the circuit is probably ok but I'd want a bit more headroom than 50V. Maybe use a 100V or higher diode - what ever is cheap. EMF kickback can produce some pretty hefty spikes even from smallish relay coils.<p>A solid state relay is good solution, too. All you need to do is directly connect it to the BS pin. But, check the current capacity of the SSR (as well as the mechanical one) to make sure it can handle draw of the cooler.<p>[ January 12, 2005: Message edited by: philba ]</p>