Searching \ for '[PIC]: Using a 2N3904 to switch a relay' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/microchip/ios.htm?key=relay
Search entire site for: 'Using a 2N3904 to switch a relay'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Using a 2N3904 to switch a relay'
2003\10\20@135924 by dave cunningham

flavicon
face
I'd like to preface this question by saying that while I can program
fine my electronics knowledge goes little beyond the absolute basics.

I want to use a pic to switch an inductive load on/off and it was
suggested (on usenet) that the following should work

                                 VCC (5VDC)

                                  |
                                  |
                             .----o
                             |    |   5V Coil
                      1N4002 -   _|_   Relay
                             ^  |_/_|-
                             |    |
                             |    |
                             |----o
                                  |
                                  |
                 6.8K             |
                 ___            |/
         o------|___|---o-------|   2N3904
    PIC Output          |       |>
                       .-.        |
                       | |        |
                  6.8K | |        |
                       '-'        |
                        |         |
                       GND       GND


I've assembled the circuit (with the input being a PIC pin pulled up to
5V via a 10K resistor), however the relay switches (on) as soon as power
is supplied to the circuit and the output state of the PIC pin has no
effect on the relay state.

As I've said at the start, my electronics knowledge is basic in the
extreme but I've been googling a bit to try to understand how the above
circuit should work (& why it doesn't). I've found a similar (to my
mind) relay switching circuit using a BC377 rather than 2N3904 and
missing the 6K8 resistor between ground & the transistor base. Following
that I removed the ground resistor & replaced the second resistor with a
2K6 one (using calculations provided with the BC377 circuit), but this
doesn't work either!

Could anyone explain how the above circuit works and how the values of
the resistors are calculated? Should it in fact be functioning as in
when connected to a pic as I've described?

Any suggestions of suitable foundation electronics books would also be
appreciated!
--
Dave Cunningham                                  PGP KEY ID: 0xA78636DC

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\10\20@142421 by Herbert Graf

flavicon
face
{Quote hidden}

       Well, the circuit is pretty basic. First off, ignore the 6.8k resistor to
ground, it's not really needed.

       Now, to simplify things: think of the transistor as a current mode switch.
When your input current into the base the collector and emitter are
practically shorted together. When there is no current flowing into the base
the switch is off, and the emitter and collected can be considered open.

       Your PIC's output pin outputs a voltage. The 6.8k resister in series with
it acts as a "safety" to ensure that not too much current flows into the
base (think of a base as a diode going to ground).

       The value of the resistor is not as straightforward to calculate. What you
WANT to do is ensure the transistor is saturated. You have to get the
transistor's datasheet to figure out it's saturation current for your case
(it will depend on the beta of the transistor and your load current), then
go a little high to ensure it's definitely in saturation. So say you
calculate the saturation current is 1mA:

       The value of the resistor would then be: R=V/I=((5V - 0.7V(which is a diode
drop))/1mA ~= 4300ohms = 4.3k

       Now, consider this the BIGGEST resistor you can use. There is no reason to
go this high unless you need to (to save power or other reason). To be safe
go lower. Your lower limit is defined by the max current a PIC pin can
source. Say it's 20mA, then the SMALLEST resistor you can use is:

       R=V/I=4.3V/20mA = 215 ohms.

       So, the SMALLEST resistor you can use is 215ohms. I don't recommend going
that low either since there is also a "per device" current output that you
might violate on the PIC.

       So, what do I recommend? Don't bother with the calculations! :) Why?
Because MOST of the time selecting something like a 1k or 2k resistor will
"work". If you want go through the calculations to ensure you are OK, but
most of the time I just go with a 2k.

       Obviously a 2k won't work in every case, and in fact if you are dealing
with different power supplies 2k will likely be no go. But it usually is.

       Back to your circuit. Disconnect the base and see if the relay still turns
on. If it does then you've likely got a bum transistor, either that or
you've hooked it up wrong. If you've confused the base with the collector
then your symptoms would match.

       Hope this helped, TTYL

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\10\20@143043 by gtyler

flavicon
face
Bet you've got the transistor connections wrong!

George

----- Original Message -----
From: "Herbert Graf" <spam_OUTmailinglistTakeThisOuTspamFARCITE.NET>
To: <.....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU>
Sent: Monday, October 20, 2003 8:24 PM
Subject: Re: [PIC]: Using a 2N3904 to switch a relay


{Quote hidden}

resistor to
> ground, it's not really needed.
>
>         Now, to simplify things: think of the transistor as a current mode
switch.
> When your input current into the base the collector and emitter are
> practically shorted together. When there is no current flowing into the
base
> the switch is off, and the emitter and collected can be considered open.
>
>         Your PIC's output pin outputs a voltage. The 6.8k resister in
series with
> it acts as a "safety" to ensure that not too much current flows into the
> base (think of a base as a diode going to ground).
>
>         The value of the resistor is not as straightforward to calculate.
What you
> WANT to do is ensure the transistor is saturated. You have to get the
> transistor's datasheet to figure out it's saturation current for your case
> (it will depend on the beta of the transistor and your load current), then
> go a little high to ensure it's definitely in saturation. So say you
> calculate the saturation current is 1mA:
>
>         The value of the resistor would then be: R=V/I=((5V - 0.7V(which
is a diode
> drop))/1mA ~= 4300ohms = 4.3k
>
>         Now, consider this the BIGGEST resistor you can use. There is no
reason to
> go this high unless you need to (to save power or other reason). To be
safe
> go lower. Your lower limit is defined by the max current a PIC pin can
> source. Say it's 20mA, then the SMALLEST resistor you can use is:
>
>         R=V/I=4.3V/20mA = 215 ohms.
>
>         So, the SMALLEST resistor you can use is 215ohms. I don't
recommend going
> that low either since there is also a "per device" current output that you
> might violate on the PIC.
>
>         So, what do I recommend? Don't bother with the calculations! :)
Why?
> Because MOST of the time selecting something like a 1k or 2k resistor will
> "work". If you want go through the calculations to ensure you are OK, but
> most of the time I just go with a 2k.
>
>         Obviously a 2k won't work in every case, and in fact if you are
dealing
> with different power supplies 2k will likely be no go. But it usually is.
>
>         Back to your circuit. Disconnect the base and see if the relay
still turns
{Quote hidden}

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\10\20@143836 by Olin Lathrop

face picon face
dave cunningham wrote:
{Quote hidden}

Looks mostly reasonable.  The diode only needs to be 1N4001, which may be
a little easier to find.  However, the 1N4002 will work fine, it's just
specified to a higher voltage than necessary.

> I've assembled the circuit (with the input being a PIC pin pulled up to
> 5V via a 10K resistor), however the relay switches (on) as soon as power
> is supplied to the circuit

That's because of the pullup resistor.  Why is it there?  Are you using
the RA4 open drain output?  The rest of the circuit is designed for a
totem pole output.

> and the output state of the PIC pin has no
> effect on the relay state.

There is something wrong in your code or the wiring.  If the PIC pin is
actively driving low, the relay should be off.  What is the PIC pin
voltage when the relay is supposed to be off but isn't?

> I've found a similar (to my
> mind) relay switching circuit using a BC377 rather than 2N3904 and
> missing the 6K8 resistor between ground & the transistor base. Following
> that I removed the ground resistor & replaced the second resistor with a
> 2K6 one (using calculations provided with the BC377 circuit), but this
> doesn't work either!

I would lose the 6.8Kohm resistor to ground.

> Could anyone explain how the above circuit works and how the values of
> the resistors are calculated? Should it in fact be functioning as in
> when connected to a pic as I've described?

Let's lose the resistor between the base and ground, assume you are using
a totem pole output (not RA4 open drain output) PIC pin to drive the "PIC
Output" node on your schematic, and assume a 5V supply to the PIC.

The 2N3904 is only rated for 200mA collector current, so this must be a
small relay.  If not, get a bigger transistor.  I would use 2N4401 here,
which can do up to an amp.

The only value to figure out is the single base resistor.  You want enough
current to flow thru the base to keep the transistor solidly on at the
maximum relay current.  This transistor has a small signal gain of 300, so
let's only require 100 of it so that we have plenty of margin.  The max
collector current is 200mA.  200mA / 100 = 2mA base desired base current.
The base will be at about 700mV when on, so that leave 4.3V accross the
base resistor.  From Ohm's law, 4.3V / 2mA = 2.15Kohms desired base
resistance.  There's enough slop in the assumptions so the 2.2Kohms or
2.0Kohms will work fine.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\10\20@144500 by Byron A Jeff

face picon face
On Mon, Oct 20, 2003 at 06:47:46PM +0100, dave cunningham wrote:
> I'd like to preface this question by saying that while I can program
> fine my electronics knowledge goes little beyond the absolute basics.

That's cool. The only way to learn is to do.

{Quote hidden}

Everything but the 6.8K pulldown looks fine. It sets up a voltage divider that
also limits the amount of current to the base of the transistor. While some
may argue that is keeps the transistor off until the PIC output can be
configured, if you're going to do it increase the value of that pulldown so
that mode current can be available for the transistor base.

I'd initally test with that pulldown off. All of my relay circuits function
fine without it.

>
> I've assembled the circuit (with the input being a PIC pin pulled up to
> 5V via a 10K resistor), however the relay switches (on) as soon as power
> is supplied to the circuit and the output state of the PIC pin has no
> effect on the relay state.

Now that's odd. If the PIC output is low, then the transistor should be
off and the relay should release. To test, simply ground the base of
the transistor. The relay should turn off. Then try tying the base of the
transistor to 5V using a 6.8K pullup. Relay should then turn on fully.

If those two conditions work, then tie the base of the transistor to the PIC
output (BTW which port pin are you using for your output?) and retest. It
should work OK now.

> As I've said at the start, my electronics knowledge is basic in the
> extreme but I've been googling a bit to try to understand how the above
> circuit should work (& why it doesn't). I've found a similar (to my
> mind) relay switching circuit using a BC377 rather than 2N3904 and
> missing the 6K8 resistor between ground & the transistor base. Following
> that I removed the ground resistor & replaced the second resistor with a
> 2K6 one (using calculations provided with the BC377 circuit), but this
> doesn't work either!

OK so you did test it.

>
> Could anyone explain how the above circuit works and how the values of
> the resistors are calculated?

The transistor is acting as a switched amplifier. This means that if you
provide current through the base/emitter junction, then an amplified amount
of current is allowed to flow between the collector emitter junction. The
PIC output is supposed to be the switch. The purpose of the resistor is to
in fact limit the amount of current that can be pulled through the base.

OK I'm just going to throw out some numbers. The 2N3904 has a maximum
continuous collector current of 200ma. Relay coils offer a resistance between
50 to 500 ohms depending on the model, so let's be conservative and say that
it's 50 ohms and therefore draws 100 ma of current across it. So there will
be 100ma of current flowing between the collector and the emitter (Ice) of the
transistor.

So you go to the datasheet that indicates that the beta (the amplification)
is 30 for Ice = 100ma. That means that the base current will get amplified
30 times. So the base current requires is 100ma/30 -> 3.3ma.

Now two things comes into play here. One is the fact that the base emitter
voltage is 0.6V. The second is that Roman Black taught me that you should
drive the base upwards of 10 times harder than the minimum specified to make
sure that the transistor turns on solid. Since that's 33ma and overwhelms the
PIC's output driver, let's back off and say that we'll use 5 times the base
current: 16.5 ma.

So here's the string we're trying to drive:

Pic output ---R-----BE------GND
 5V         4.4V  0.6V      0V

So the resistor has to drop 4.4V @ 0.0165A. So we can compute the value:

V = IR --> R=V/I ->  R = 4.4/0.0165 -> 266 ohms.

So now we see why there may be an issue. That 266 ohms is a lot less than
both the 2.6K and the 6.8K that you've been testing with.

So grab a 270 ohm and retest.

>Should it in fact be functioning as in
> when connected to a pic as I've described?

Looking at these numbers above, it probably should never turn on.

>
> Any suggestions of suitable foundation electronics books would also be
> appreciated!

I hope this helps. I always use resistor values 1K and less. It's worked
quite well for me.

BAJ

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\10\20@151854 by Dave Dilatush

picon face
Dave Cunningham wrote...

>I've assembled the circuit (with the input being a PIC pin pulled up to
>5V via a 10K resistor), however the relay switches (on) as soon as power
>is supplied to the circuit and the output state of the PIC pin has no
>effect on the relay state.

Hmmm.  Why are you using the 10K pullup resistor?  Are you using
the open-drain PIC output (RA4) ?

Some suggestions:

1) Double-check (then triple-check, if necessary) the pinout of
the transistor to make sure you've got it connected right.  If
you take a 2N3904 in the plastic TO-92 case and hold it such that
the flat side is toward you and the leads are pointing downward,
the connections will be E - B - C from left to right.

2) Are you sure your PIC pin has been properly configured as an
output?  Remember, on many PICs some of the pins are analog
inputs until configured otherwise; and clearing the associated
TRIS bits isn't enough to make them digital outputs.  Use a scope
or a DVM to monitor the PIC pin while the code toggles it HIGH
and LOW, and confirm that it's functioning right.

3) As far as I can see, the circuit should work as you've got it
drawn, though I would concur with others' comments about the 6.8K
resistor from PIC output to transistor base being possibly too
large (though this wouldn't be causing the symptoms you're
seeing) and about the 6.8K resistor from base to ground being not
very necessary.

4) If you **ARE** using RA4 as the PIC output driving this
circuit, be aware the 10K resistor is too big; it probably won't
give you enough base current to properly drive the transistor
(again, this wouldn't be the cause of the symptoms you're
seeing).

Hope this helps a bit...

Dave D.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\10\20@155556 by John N. Power

flavicon
face
     > From:   dave cunningham[SMTP:davespamKILLspamUPSILON.ORG.UK]
> Sent:         Monday, October 20, 2003 1:47 PM
> To:   .....PICLISTKILLspamspam.....MITVMA.MIT.EDU
> Subject:      [PIC]: Using a 2N3904 to switch a relay

> I want to use a pic to switch an inductive load on/off and it was
> suggested (on usenet) that the following should work
     > ...
> I've assembled the circuit (with the input being a PIC pin pulled up to
> 5V via a 10K resistor), however the relay switches (on) as soon as power
> is supplied to the circuit and the output state of the PIC pin has no
> effect on the relay state.

Hmmm. Looks like a programming problem. Are you sure that you set
the output pin to output via the TRIS register? As long as the pin is an
input (which is the default state immediately after power up), the 10K
pullup will turn the transistor on, and writing to the output latch will not
affect this.

John Power

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\10\20@162730 by Kenneth Lumia

picon face
Dave,

First, determine if it is a hardware or software problem.
Remove the resistor from the PIC pin and see of jumpering
the resistor to ground and then to +5 causes your relay to
function (simulate the PIC).

As others have said, the resistor on the base may be
a bit too large, it depends on how much current the relay
needs.  Note that although this may be a problem later, it is
not the issue here.  Since the relay is engaging, it means that
the base is getting enough current and the transistor is turning
on (may not be saturated).  My guess is that you wired
the transistor incorrectly.  Check the pinout.

Ken


{Original Message removed}

2003\10\20@203200 by dave cunningham
flavicon
face
In message <vnQ5fvHC$Bl$EwbA@127.0.0.1>, dave cunningham wrote

>I'd like to preface this question...

Thanks to all who replied. As was pointed out the problem was that I had
the input to the transistor coming from the open collector pin, the
circuit I originally posted works fine when input is from a standard
pin.

--
Dave Cunningham                                  PGP KEY ID: 0xA78636DC

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\10\21@041323 by Simon Davies

flavicon
face
>Any suggestions of suitable foundation electronics books would also be
>appreciated!
>--

The Art of Electronics by Horowitz and Hill got me through electronics at
university and the first couple of years in the real world

Simon

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

2003\10\21@072514 by Olin Lathrop

face picon face
dave cunningham wrote:
> Thanks to all who replied. As was pointed out the problem was that I had
> the input to the transistor coming from the open collector pin, the
> circuit I originally posted works fine when input is from a standard
> pin.

The relay should still have gone off when the open drain pin went actively
low.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

More... (looser matching)
- Last day of these posts
- In 2003 , 2004 only
- Today
- New search...