turning on relay from 5v signal

I asked how can I turn on a 12V relay from a ttl signal before and I got the circuit below as a response. I tested it and it works fine but unfortunately I tested it by supplying 12V into the 2.2k resistor. Now when I've connected it to my little cpu it doesn't work. When I switch on the appropriate pin it can only muster .67 of a volt. The pin has an internal pullup and does switch from 0 to 5V without the transistor connected. Did I get the wrong transisitor or something? It's a BC338 from jaycar. I dunno what these figures mean but in the catalog it says "Diss @25C 500mW, Vcb 30V, Ic 800mA Hfe 100, Hfe Bias 100mA"

Many thanks yet again Michael

o----o----- +12V | | --- --- ^ | | 1N4001 / \ | | Relay coil --- --- | | -----o | / IN |/

----\/\/\/-----| 2K2 |\ Misc. NPN -V| | | --o-- GND

Reply to
Michael C
Loading thread data ...

Change you cpu output to push-pull instead of open collector(drain) and it should work ok

Alan

--
++++++++++++++++++++++++++++++++++++++++++
Jenal Communications
 Click to see the full signature
Reply to
Alan

Do you mean pull it up to 5v with an external resistor?

Michael

Reply to
Michael C

You could do - use another 2k2 or 1k0 to +5v from the output of the cpu.

But you should be able to programme the cpu (normally) for push-pull output. ie it will either sink (low output) or supply (high output) current. If you cannot change the programming of the cpu then the additionla resistor is probably the easiest (only) option for you.

Alan

--
++++++++++++++++++++++++++++++++++++++++++
Jenal Communications
 Click to see the full signature
Reply to
Alan

Thanks for the reply. As far as I can tell the pins cannot be changed. It's an 8051 and some pins are either open drain or have pull ups. All you can do is set the pins to either a 1 or a 0 i think. What you suggested did work, using a 2k resistor (because I didn't have a 1k here). I was under the impression that the transistor would use an extrememly low current but I guess that's not the case? Even with a 2k pullup + the internal pullup it still only managed 3volts. I have one problem now that all the relays switch on during the reset stage at startup, is there any way to avoid that?

Cheers, Michael

Reply to
Michael C

Hi Michael

The 3v (measured at the micro pin) is OK. Basically you have approx

0.7v on the base of the transistor when it is turned on. That leaves 4.3 to be split accross the two 2k2 resistors. So the output pin of the micro should be about 3v.

There will be no way (easily) to stop the relays switching on as the micro will set it's outputs to hi/tri-state/open when it resets.

You can try two things to overcome this in your case.

1) a capacitor from the base of the transistor down to ground. This will slightly delay the turn on and turn off of the relay. Experiment with values to overcome the pwoer up turn on. 2) put another transistor inverter between the micro output and the relay driver you have now and invert your output signal in the micro. That way you need to output a low from the micro to turn the relay on.

HTH Alan

--
++++++++++++++++++++++++++++++++++++++++++
Jenal Communications
 Click to see the full signature
Reply to
Alan

They turn on because the base of the transistor is being pulled high by the pullup resistor before the micro has a chance to set the pin outputs low. Use a PNP transistor in place of the BC338. Connect the emitter of the PNP to the diode anode-relay junction, and the collector to ground. The base can be connected to the pullup resistor. This method ensures that when the micro is in reset, the relays are off, because the pullup turns the transistor off. To turn the transistor on, set the output pin of the micro to low, to turn it off, set the output pin high.

Reply to
dmm

the above circuit is for where the microcontroller can actually drive the pin positive sourcing atleast 2.5ma. if you've only got the internal pullup it's unlikely to work.

what's the processor you're using? I used a very similar circuit with an Atmel AVR 2313 (except I used 1K resistors), it worked fine.

with the AVR you need to put the pin in output mode (by setting the apropriate data-direction register bit) AIUI other microcontrollers behave similarly. The pull-up is intended for when you use the pin as an input.

Alternately you could try this:

o-----------o----o----- +12V | | | | --- --- | ^ | | \ 1N4001 / \ | | Relay coil / --- --- 1K5 \ | | / -----o \ | | / IN | |/ ---o--\/\/\/-----| 860R |\ Misc. NPN _V| | | --o-- GND

Which should work where the output can sink about 10Ma ant the 12V is really 12V

Bye. Jasen

Reply to
Jasen Betts

cpus also work with extremely low currents.

if you want low current compared to the CPU (ie. high resistance) you need to use a MOSFET...

--
Bye.
   Jasen
 Click to see the full signature
Reply to
Jasen Betts

I tried this but for some reason there is 3v drop across the transistor so the relay only gets 9v. It switches but it sounds like it is only just switching. I'm not sure it will work anyway because the uC will set it's outputs to 0 after reset I think

Michael

>
Reply to
Michael C

Did you try another inverter stage before the relay driver as I suggested?

Alan

--
++++++++++++++++++++++++++++++++++++++++++
Jenal Communications
 Click to see the full signature
Reply to
Alan

That sounds good, I'll give it a try. Although, is there some sort of chip that would do all this? I've got 3 relays.

Michael

Reply to
Michael C

You could use a ULN2003 or ULN2803 relay driver chip. However you still need to be able to drive them with input voltage/current so you would still need inverters before them.

You could even try using a 74LS04 hex inverter chip for the inverter and then a ULN chip as the driver.

Lots of different ways to go!

Alan

--
++++++++++++++++++++++++++++++++++++++++++
Jenal Communications
 Click to see the full signature
Reply to
Alan

As a follow up - you could probably even use a ULN2003 as both the inverter and the driver. It has 7 (ULN2803 has 8) drivers in the one package. Wire the output of one driver into the input of the second driver and put the relay between the output of the second driver and

+12v.

Both inputs should then be pulled to +5 with say a 2k2 and the input of the first driver connected to the output pin of the micro.

The ULN2003/2803 even has built in clamp diodes for your relays.

Alan

--
++++++++++++++++++++++++++++++++++++++++++
Jenal Communications
 Click to see the full signature
Reply to
Alan

Initially I tried the pnp transisitor because it appeared to be much easier. I just tried using the 2 npns and it worked perfectly, powering it all up I don't get a peep out of the relay. I think I'll have a look at your other suggestion as it looks like a neater solution. As these will be put together by hand it's much easier to solder in a 16 pin chip instead of 6 transistors, 9 resistors and 3 diodes. Jaycar sells the ULN2003 so I'll try to pick one up tomorrow. I was a bit confused about the datasheet though, do I just connect the port pin to the input of the uln2003? The datasheet shows some fairly complicated circuits on the input which would defeat the purpose of using the chip. (page 11 of

formatting link

Thanks for all your help, Michael

Reply to
Michael C

The complicated circuit on the input is acutally part of a TTL gate output circuit! It's shown for reference only.

Using a ULN2003 all you need to do is:

1) connect micro output to (say) pin 1 and pull up to +5v with say a 2k2 resistor 2) connect pin 16 to (say) pin 2 and pull up to +5v with say a 2k2 resistor. 3) connect pin 15 to one side of your relay 4) connect other side of relay to +12v (or whatever) 5) connect pin 8 to ground 6) connect pin 9 to relay power supply +12v (or whatever)

You have to use the pull up resistors because all the inputs are basically open collector/drain.

Using pin 9 of the ULN2003 connects the internal back EMF diodes as per fig 19 of the datasheet and therefor you don't need external protection diodes.

HTH

Have fun!

Alan

--
++++++++++++++++++++++++++++++++++++++++++
Jenal Communications
 Click to see the full signature
Reply to
Alan

hmmm, I thought most of that circuit shown probably wasn't needed but didn't know why. Usually datasheets are quite clear and easy for a hobbiest like myself to understand.

I'll see if I can get away with the pullup that's inside the cpu so I'll only need the one pullup, although I might just go with the transistors.

That makes sense, I was a bit confused as to whether this was the power for the chip or not and whether that voltage was going to appear on the outputs or not.

It certainly has, many thanks.

Michael

Reply to
Michael C

If you use transistors you only need two resistors and two transistor (plus a diode for the relay).

1) connect o/p of micro to base of transistor A and pull up to +5 with say 2k2 2) connect collector of transistor A to base of transistor B and pull up to +5 (or +12) with say a 2k2 3) connect collector or transistor B to one side of relay 4) connect other side of relay to +12 5) don't forget to connect both emitters to ground and put diode across the relay (the right way round!)

Alan

--
++++++++++++++++++++++++++++++++++++++++++
Jenal Communications
 Click to see the full signature
Reply to
Alan

I used an external pullup as Alan suggested and it worked quite well. Basically I did pretty much what you suggested in your diagram except connected the pullup to 5v instead of 12.

It's an 8051, specifically the phillips p89c668hba but I'll probably use the atmel AT89C2051 when I get a board made for it.

I've only got 2 options, I can set the pin at either 0 or 1 and it can be used as an input or output. Some pins have internal pullups and some don't but the one I'm using does. How a pin could only have 2 states and be used for both input and output had me confused literally for weeks when I first started using the 8051 a few years ago. :-)

Michael

Reply to
Michael C

After reading this thread I am beggining to think that neither understand the problem.

First you need to know how how much current the relay draws. Take note that there will be a surge current as the realy energises.

Second, you need to understand that your transistor has a limited current gain.

The resistor on the base of your transistor should limit the amount of current being drawn from the CPU. Try doing a test using the original circuit. Add a 10k pulldown resistor to the base of the transistor to ensure that it stays off (you should always do this anyway). Then use a multimeter to measure the current between the 2k2 resistor and 5V.

How much current? Does the relay turn on?

IF you can get hold of 2 meters, measure the current throught the relay coil too.

If the relay does not turn on, then lower the value of the 2k2 resistor. My initial guess is that 2k2 is probably an overkill and that several hundred ohms is going to be closer to the ball park, but in saying that, i do not know the specs of your CPU.

Most modern CPU's can source quite a bit of current. Read the datasheet on the micro to find out how much current it can source through the pin. If it cant supply enought current, you can use a darlington transistor, or 2 transistors in a darlington pair. This will provide more gain. More gain means a smaller base current can drive a larger source current.

Reply to
The Real Andy

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.