9
\$\begingroup\$

I am trying to use a simple Arduino circuit to turn an AC lamp on and off. This is the circuit I was about to try which I got from a website.

Enter image description here

I don't understand why a transistor is required there. I thought we already have a 5 V output which we can directly connect to the relay.

\$\endgroup\$
5
  • \$\begingroup\$ But I have already got some best answers I think .. Thanks \$\endgroup\$ Sep 29, 2021 at 15:11
  • \$\begingroup\$ More solutions than you ever wanted here: electronics-tutorials.ws/blog/relay-switch-circuit.html - Scroll down to "Logic Controlled Relay Switch Circuit" - N-channel enhancement-MOSFET would be my personal preference. \$\endgroup\$
    – Ed Randall
    Sep 29, 2021 at 17:14
  • 1
    \$\begingroup\$ @EdRandall Thanks a lot for that... Those are really informative for me such beginners.. \$\endgroup\$ Sep 29, 2021 at 17:18
  • 1
    \$\begingroup\$ This is a super FAQ. Why are such questions not immediately closed as a duplicate? Why do we need to answer the same questions over and over again? \$\endgroup\$ Sep 30, 2021 at 7:02
  • \$\begingroup\$ @PeterMortensen Sounds like a question for meta. \$\endgroup\$
    – Mast
    Sep 30, 2021 at 17:37

5 Answers 5

10
\$\begingroup\$

The average microcontroller like on your Arduino has only limited ‘strength’ on the port pins. Typically it might be 2..20mA. Your relay might require 60mA at a guess so you require some means of amplifying the port pin. A transistor or mosfet is commonly used.

Note your diagram is missing a resistor in series with the transistor as the transistor only requires around 0.7V to turn it on. The resistor drops the port pin voltage from 5V down to 0.7V.

\$\endgroup\$
19
  • \$\begingroup\$ THanks a lot.. But I didnt follow the resistor part... :( Please suggest the value if you dont mind . So its in series to the base?? Aruduino Pin -> Resistor -> Transistor Base, is it correct friend?? \$\endgroup\$ Sep 29, 2021 at 6:46
  • 1
    \$\begingroup\$ A resistor may as well not be needed, if the pin has an internal pullup. \$\endgroup\$
    – fraxinus
    Sep 29, 2021 at 8:05
  • 7
    \$\begingroup\$ The resistor is needed to limit the base current in the transistor. Otherwise you'll risk damage to the gpio port. It's really more of a current issue than a voltage one (but the ohm law exists…) \$\endgroup\$ Sep 29, 2021 at 13:14
  • 3
    \$\begingroup\$ fraxinus you are mistaking the input pullup resistor with the output current limiting resistor. \$\endgroup\$ Sep 29, 2021 at 13:14
  • 1
    \$\begingroup\$ @Kartman It's not splitting hairs - this answer will be here for a long time, and so long as it contains the phrase "transistor or mosfet" it continues to contain an inaccuracy that can add confusion to learners of electronics who will read this and walk away with the idea that transistors and mosfets are different types of thing, which they are not. It's like saying you can create music with a musical instrument or a piano. Or that trees can produce fruits or apples; that you can get to work using a vehicle or a car, etc. \$\endgroup\$
    – J...
    Oct 1, 2021 at 11:48
20
\$\begingroup\$

Ok, here's (almost) everything you need:

schematic

simulate this circuit – Schematic created using CircuitLab

The Arduino uses an ATMEGA MCU (most of the AVRs have similar specs so it's not critical) so the datasheet has the relevant informations:

  • At 5V supply the output port HIGH voltage is at least 4.2V, with 20mA load

  • Still a 5V, the output port LOW voltage is at most 0.7V, with 20mA load

Since they quote the current at 20mA it's safe to assume that the current is available (up to 400mA total for the chip, as in the absolute maximums).

From the BC547 datasheet you can see that:

  • Maximum current capability is 100mA (the size of your relay coil)

  • DC current gain (hFE) at 2mA is at least 110

  • Base emitter saturation is typically 0.7V

  • Base emitter on voltage could go low at 0.55V (this is a beginner's trap)

Now, for the calculations: you want to put some mA in the base with the GPIO high: so you have a 4.2V source, the base resistore and the B-E junction (which is substantially a diode for this purpose, and drops 0.7V)

You then leave with at lease 3.5V on the resistor to drop. The proposed 1k resistor limits the current to about 3.5mA (minimum). That, multiplied by the hFA gives a potential 350mA of collector current which is enough (as in, if you try to pull more than 100mA the BJT dies). So even a slightly larger resistor will work (not too much however)

Now, for the above mentioned trap: if you are really unlucky you could have an AVR with a very high low output (0.7V) and a BJT with a very low VBE (0.55V). So the relay could actually trigger when the GPIO is low!

For that reason it's useful to add another resistor between base and emitter to shunt the excess current. The calculation is not immediate (think a resistor in parallel to the B-E junction diode). However a 10k resistor is useful and traditional for many decades.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Good answer, I'd just add that in rugged designs, the base should also have a pull resistor so that the BJT is in a defined state when the MCU is starting up or resetting. The shunt resistor you mention would act as such. \$\endgroup\$
    – Lundin
    Sep 29, 2021 at 13:55
  • 1
    \$\begingroup\$ For MOSFETs it's quite important since they have an high impedance gate and a tristated output is essentially random; a BJT doesn't suffer from that issue: no base current, no collector current. However the VBE is usually lower that the VGSth so this other problem is typical \$\endgroup\$ Sep 29, 2021 at 14:00
  • \$\begingroup\$ @LorenzoMarcantonio Thats really an excellent answer man.. Thanks a lot \$\endgroup\$ Sep 29, 2021 at 14:01
  • \$\begingroup\$ Yeah but you can't really rely on the MCU having some well-defined state during reset or power up. Could be some threestate, could be floating, could be input... yeah it's unlikely that something from the MCU side would actually drive the pin high as needed for NPN, but there's also EMI. \$\endgroup\$
    – Lundin
    Sep 29, 2021 at 14:06
  • \$\begingroup\$ +1 - for including the schematic - This should be the accepted answer..! \$\endgroup\$ Sep 29, 2021 at 14:51
6
\$\begingroup\$

Let me try to simplify:

  • A - Check the minimum current needed to turn ON the relay in datasheet.
  • B - Check the maximum current the MCU GPIO can source in the datasheet

If B>A then you don't need a transistor, otherwise to source extra current required by the relay you need transistor.

\$\endgroup\$
3
\$\begingroup\$

There are relays which don't need a transistor to be operated: for instance the HE3300 reed relays need as little as 10 mA and can be driven directly by a microcontroller pin. Note that such relays are in turn rather limited w.r.t the power their contacts can switch (for HE3300 that would be 10W max).

\$\endgroup\$
0
\$\begingroup\$

In addition to limited current capacities of outputs: Sinking significant current, even within specification, will create more heating of the microcontroller (not good for reliability); also, there might be a limit to total output (sink) current for the whole microcontroller, limiting the amount of relays that can be directly controlled by it.

Also, driving significant current spikes through the ground connection of the MCU can potentially create signal integrity problems (forcing a bus ground in a place where it might not be desirable), especially if the MCU has some analog I/O and no separate AGND.

Also, relays and motors are considered devices that create back EMF spikes and other effects which, while you have to mitigate them with devices like freewheeling diodes and/or snubbers in any case, you want to keep somewhat isolated from a sensitive LSI part like a MCU.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.