Electronics 101 help needed - transitors as a relay

for all subjects/topics not covered by the other forum categories
Post Reply
User avatar
sweh
Posts: 2166
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Electronics 101 help needed - transitors as a relay

Post by sweh » Sun Apr 05, 2020 6:33 pm

As has been previously established, my knowledge of electronics is pretty minimal. I know Ohms law and resistors. Capacitors...kinda.

Now transistors. I've never used these before. But I'm thinking...

What I want to do is make a door magnet remote controllable via an ESP8266. Traditionally I'd use a relay, so the output of a digital pin (eg D7) would be connected to the signal input of the relay, and this would close the circuit and so energise the electromagnet.

The electromagnet draws around 400mA at 12V. I'll probably be powering it from a 9V battery, so I was thinking a standard 2N2222 sounds (from the specs) as if it'd do the job.

If I've googled correctly....

Am I right in thinking the left leg (looking at the flat side) is the "Emitter" and should be connected to the -ve side of the 9V battery, The middle leg is the base and should be connected to pin D7, and the right leg is the "collector" and should be connected to the black wire of the electromagnet, and then the electromagnet red wire should be connected to the +ve side of the battery.

Or am I totally off-base on this?

And since we're talking about transitors, what's the difference between NPN and PNP?
Rgds
Stephen

User avatar
Diminished
Posts: 281
Joined: Fri Dec 08, 2017 9:47 pm
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by Diminished » Sun Apr 05, 2020 6:57 pm

I think you want something like this:

https://reviseomatic.org/help/e-MOSFET- ... Switch.gif

The base resistor is necessary to avoid blowing up the transistor. The diode is also necessary ... to avoid blowing up the transistor.

In grossly oversimplified terms, NPN and PNP are opposites in a sense. An NPN transistor switches on when the base is fed with a voltage that is more positive than the emitter's voltage, and a PNP one switches on when the base is fed with a voltage that is more negative than the emitter's voltage.

As for which leg is which: it depends on how it's packaged. Transistors always come in a package having a particular code (there are some examples here), so you'll need to find the right package code for the particular device you're using, then find the pinout for it.

Some multimeters have transistor testers which will allow you to determine which legs are which without faffing around with data sheets.

User avatar
roland
Posts: 3782
Joined: Thu Aug 29, 2013 9:29 pm
Location: Born (NL)
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by roland » Sun Apr 05, 2020 7:14 pm

You can use this diagram for you relay:
Schermafbeelding 2020-04-05 om 20.01.39.png
Like Ali already mentioned, the diode and the resistor are needed to protect the transistor but also the output port of the ESP8266.
If the transistor is "active" the voltage between base and emitter is about 0.7V (datasheet specifies 0.6V min and 1.2V max @ 15mA base current. The base current must be less than the output current of the ESP8266 port otherwise you'll damage that one. If you have any doubts you can also use a simple TTL ic or a dedicated driver ic between the ESP8266 and the resistor as extra protection for you ESP.

I have a similar setup for a rotating beacon connected to a GPIO port of a Raspberry Pi. And it works for years now. Every hour the beacon goes on for 10 seconds and when a server is down it goes on for 30 seconds.

The pin out of the transistor depends on the package:
2n2222a-transistor-npn-2n2222.jpg
2N2222.jpg
FPGAtom: 512 KB RAM, Real Time Clock and 64 colours
MAN WOMAN :shock:

User avatar
1024MAK
Posts: 9906
Joined: Mon Apr 18, 2011 5:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by 1024MAK » Mon Apr 06, 2020 12:30 am

sweh wrote:
Sun Apr 05, 2020 6:33 pm
What I want to do is make a door magnet remote controllable via an ESP8266. Traditionally I'd use a relay, so the output of a digital pin (eg D7) would be connected to the signal input of the relay, and this would close the circuit and so energise the electromagnet.

The electromagnet draws around 400mA at 12V. I'll probably be powering it from a 9V battery, so I was thinking a standard 2N2222 sounds (from the specs) as if it'd do the job.

If I've googled correctly....

Am I right in thinking the left leg (looking at the flat side) is the "Emitter" and should be connected to the -ve side of the 9V battery, The middle leg is the base and should be connected to pin D7, and the right leg is the "collector" and should be connected to the black wire of the electromagnet, and then the electromagnet red wire should be connected to the +ve side of the battery.

Or am I totally off-base on this?

And since we're talking about transitors, what's the difference between NPN and PNP?
There are two main classes of transistors. One class are bipolar transistors. NPN and PNP transistors are bipolar types. The other main type are field effect transistors (FET). I will leave FETs for another time.

In the past NPN and PNP types were made out of germanium semiconductor material. Now they are made from silicon semiconductor material.

A 2N2222 is a NPN general purpose amplifier transistor.

Bipolar transistors are current amplifiers. The DC gain (or amplification factor) is called the hFE (also known as β). If the voltage between the base terminal and the emitter terminal is greater than about 0.6V to 0.65V, a small current flow between the base terminal and the emitter terminal will result in a current flow between the collector terminal and the emitter terminal (assuming the available voltage is sufficient between theses terminals). The current that flows is proportional to the base current and the hFE of the transistor.

Different transistors have different hFE figures. The exact hFE value for a particular transistor type is normally specified within a range of values, or as a typical value, as the manufacturing process results in variations in hFE. The hFE figure is also dependent on the temperature of the transistor. And it also varies within the normal specified range of collector current.

A P2N2222A transistor has a hFE of between 35 and 300.

Assuming that you have a NPN transistor with a hFE of 50, if 2mA of current flows into the base terminal, then if the voltage on the collector terminal is high enough, 100mA will flow through into the collector terminal. The current following out the emitter terminal will then be 102mA.

If the voltage at the collector terminal is higher than needed for 100mA to flow, then the transistor will find itself with a voltage difference between the collector terminal and the emitter terminal. Voltage x current = power. This power will be converted to heat. If the amount of heat converted is too great, the transistor will self destruct. So transistors have to be run within the power dissipation levels specified by the manufacturer. High power transistors are often fitted on to heatsinks that help remove the heat from the semiconductor junction inside the transistor.

If the conditions are set up correctly, a transistor can be made to operate in a linear part of its characteristic, hence they can then reliably amplify signals.

In digital circuits, heat is not normally too much of a problem, as the transistor is either fully off (no current or very little current flows via the base terminal, hence no current or very little current flows via the collector terminal) or the base current is high enough to saturate the transistor. This means that the transistor turns on as hard as it can, but due to the configuration of the circuit, the collector current can never get to the product of the base current x hFE. The result is that the voltage between the collector terminal and the emitter terminal is only about 0.1V to 0.4V.

With a NPN transistor, to turn it on, the voltage across the base and emitter has to be positive on the base terminal with respect to the emitter terminal and the voltage across the collector and emitter has to be positive on the collector terminal with respect to the emitter terminal.

With a PNP transistor, the voltages are all the opposite polarities. So the voltage across the base and emitter has to be negative on the base terminal with respect to the emitter terminal and the voltage across the collector and emitter has to be negative on the collector terminal with respect to the emitter terminal.

There are other characteristics with transistors, including the maximum frequency. Did I mention that the hFE is also dependent on the frequency if you are trying to amplify AC signals?
Also the maximum current, maximum voltages, and maximum reverse voltages, the switch on time and the switch off time.

Here is a data sheet for a P2N2222A transistor

Now back to your requirements...

The load you want to switch is an electromagnet that draws around 400mA at 12V.

The data sheet says
Collector Current − Continuous (IC) 600 mAdc
IC means I for current (I is the symbol for current) and C meaning the current flowing via the collector terminal.

So your load is below this figure:-)

Now for the hFE value, look at ‘Figure 3. DC Current Gain’ in the data sheet. We will assume that the temperature of the transistor is 25°C. Along the bottom it lists ‘IC, COLLECTOR CURRENT (mA)’. Find the two lines marked 300mA and 500mA. We want the one in the middle of these, for 400mA. Where the 25°C intersects the vertical line, read across to the left hand scale.
I make this value to be 70. So the gain (hFE) will typically be around 70.

Because individual transistors may have slightly different hFE values, we play it safe and assume the gain will be about 50.

So if the collector current is 400mA, 400 ÷ 50 = 8
So 8mA needs to flow into the base terminal to saturate the transistor so that it turns fully on, giving us the lowest voltage between the collector and the emitter terminals.

Let’s just check the power level in the transistor. Assuming the collector to emitter voltage is 0.3V, power = V x I, so that’s 0.3 x 400mA. Which is 0.12W or 120mW. Well within the manufacturers maximum rating.

Now we need to find out how much current the ESP8266 can supply via one of its GPIO pins. I found a datasheet where it says this is 12mA. It’s output high voltage is 0.8 x the supply voltage (which I am assuming will be 3.3V). So that’s 2.64V

We need a resistor to limit the current into the base terminal of the transistor (otherwise it will cause damage). So assuming the base voltage will be about 0.7V, then the voltage across the resistor for the base will be 2.64V - 0.7V = 1.94V

To work out the resistor value, we use V ÷ I = R
So 1.94V ÷ 8mA = 242.5Ω

The nearest standard value is 220Ω.

You may want to do the calculations in reverse to confirm that you are happy that using a 220Ω is safe.

Hinted at in an answer above was the need to protect the transistor with a diode. When the current to an inductor, such as an electromagnet is interrupted, the magnet field starts to collapse. This induces a high voltage across the terminals of the inductor. In simple terms, the inductor does not want the current flow to stop, so it tries to keep the current flowing.

If we did not take account of this, the high voltage would shoot way above the maximum rated voltage of the transistor, destroying it in the process.

Hence a suitable silicon diode needs to be fitted between the positive power rail to the electromagnet and the collector terminal of the transistor. It MUST be connected so that normally it DOES NOT conduct. Or in electronic terms, it must be connected so that it is reverse biased. In practice this means that it is connected in parallel with the electromagnet wiring.

Once the diode is fitted, when the transistor switches off, the electromagnet becomes a source of electricity energy, or current, the current flows in the same direction, so the electromagnet terminal that is connected to the transistor becomes positive with respect to the other electromagnet terminal. The diode is now forward biased, so it conducts. The voltage across the diode is then clamped to the diode’s forward voltage drop (around 0.7V to 1.1V typically).

The diode used should be able to handle the current from the inductor. Typically you will find a 1N4001, or one of its higher voltage ‘brothers’ used (1N4002 through to 1N4007).

For the transistor connections, look at the drawings in the datasheet. But keep in mind that if you got the transistor from eBay, it is likely to have been made by a different manufacturer. Hence the lead-out cannot be guaranteed. With the plastic TO92 encapsulations, there is no single standard. However the base terminal is normally the middle lead.

Here are some other data sheets from other manufacturers: link, link, link, link.
Note, not all manufacturers data sheets show the transistor the same way round!

I think I have some eBay cheap 2N2222 transistors in TO92 plastic encapsulations somewhere. Tomorrow I will try to dig one out and then post a photo showing it’s lead out connections.

I hope this helps and does not blow too big a hole in your brain :lol:

Mark

User avatar
sweh
Posts: 2166
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by sweh » Mon Apr 06, 2020 2:27 am

This is all fascinating stuff... I have the components needed so I could build this. But I'm now wondering if sticking with a relay is easiest for me. The sort designed for arduino's have the resistor/diode protection built in, so the relay magnet won't back-feed into the arduino.

I understand _that_ and can explain it. I like things I can explain :-)
Rgds
Stephen

julie_m
Posts: 141
Joined: Wed Jul 24, 2019 9:53 pm
Location: Derby, UK
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by julie_m » Sat Apr 11, 2020 2:23 am

Imagine you have a large waiting room, full of fat people, with a number of open doorways leading into a narrow corridor; on the opposite side of the corridor is a gym with a particularly vicious PE instructor.

You stand at the end of the corridor and shout "Free ice cream!"

The fat people try to come from the waiting room, along the corridor and towards the supposed goodies. However, the corridor is so narrow, and the people take so long to change direction, that you wind up with more of the fatties in the gym than coming along the corridor.

You shout "Ice cream's run out!"

The fat people stop trying to come out of the waiting room and down the corridor, and the flow into the gym also ceases.

That's basically how a transistor works. You draw some charge carriers from the base (corridor), they get replaced from the emitter (waiting room); and while all that's happening, a whole bunch more charge carriers find their way to the collector (gym) instead. With an NPN transistor, the charge carriers are electrons; with a PNP transistor, the charge carriers are holes. Basically, places where an electron should be but isn't, that generally act as though they were a positively-charged particle. Electrons and holes annihilate one another on contact. Holes only exist in semiconductors, beyond the iron fist of Georg Simon Ohm; in metals, if there is a charge to be carried, electrons will be doing it. So if you're trying to pull holes out of somewhere, you'll actually have to push in electrons; and if holes are going into something, that means electrons are coming out of it.

User avatar
1024MAK
Posts: 9906
Joined: Mon Apr 18, 2011 5:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by 1024MAK » Sun Apr 12, 2020 2:54 pm

Here are the connections for one of the cheap 2N2222 plastic TO92 cased transistors that I got from a seller on eBay.
F2760876-3D4D-42F7-B2CE-AB31810CEC68.jpeg
2N2222A being tested
2N2222A transistor pin-out.jpg
2N2222A transistor pin-out
Suggested circuit:
NPN transistor used as a switch_.png
NPN transistor used as a switch
Parts:
R1 220Ω 5% 250mW / ¼W carbon film resistor
C1 100nF 50V ceramic capacitor
D1 1N4001 diode
TR1 2N2222A or PN2222A or P2N2222A NPN bipolar transistor

Mark

User avatar
Diminished
Posts: 281
Joined: Fri Dec 08, 2017 9:47 pm
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by Diminished » Sun Apr 12, 2020 3:57 pm

Off-topic, but I've never seen one of those component analysers before. That looks like a handy bit of kit.

User avatar
1024MAK
Posts: 9906
Joined: Mon Apr 18, 2011 5:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by 1024MAK » Sun Apr 12, 2020 5:03 pm

So here is a practical demonstration. I don’t have an electromagnet like the OP describes, so I have used a 12V 184Ω relay instead.

I used a 220Ω metal film resistor and a 1N4007 diode as I found these first, so did not continue looking to see if I had any 220Ω carbon film resistors or the lower voltage diodes.

The 1kΩ resistor and orange LED are only there to provide a visual indication.
25219D39-309A-4AFD-A1FD-CD63003C3340.jpeg
Overview
846C497D-3892-43C7-87E3-CB3DBD91EB2E.jpeg
Close up view
85722309-189F-4EED-BC7C-96C0523B230C.jpeg
Close up view from the right
173BB630-A733-4AA3-BDF8-6374E41F39C0.jpeg
Close up view from the left

(Link to Video on You Tube)

Mark

User avatar
1024MAK
Posts: 9906
Joined: Mon Apr 18, 2011 5:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: Electronics 101 help needed - transitors as a relay

Post by 1024MAK » Sun Apr 12, 2020 5:11 pm

Diminished wrote:
Sun Apr 12, 2020 3:57 pm
Off-topic, but I've never seen one of those component analysers before. That looks like a handy bit of kit.
That semiconductor tester (Atlas DCA55) was bought from Peak Electronic Design Ltd. A respectable U.K. company.
I’ve had that one since July 2002!

I also have their Atlas ESR capacitor tester which tests ESR as well as capacitance.

If you are in need of good quality testers, I do recommend them :D

You’d have to rip them from my hands, as no way would I let them go otherwise :mrgreen:

Both were purchased long before I became aware of cheap component testers from eBay or Amazon.

Mark

Post Reply

Return to “off-topic”