# Why is a transistor required when connecting relays to an Arduino? [duplicate]

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.

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.

• But I have already got some best answers I think .. Thanks Sep 29, 2021 at 15:11
• 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. Sep 29, 2021 at 17:14
• @EdRandall Thanks a lot for that... Those are really informative for me such beginners.. Sep 29, 2021 at 17:18
• 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? Sep 30, 2021 at 7:02
• @PeterMortensen Sounds like a question for meta.
– Mast
Sep 30, 2021 at 17:37

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.

• 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?? Sep 29, 2021 at 6:46
• There’s tutorials a plenty out there as what you ask is super common. At a guess I’d make the resistor 1K. The tutorials should cover how you can calculate tge resistance required. Sep 29, 2021 at 7:34
• A resistor may as well not be needed, if the pin has an internal pullup. Sep 29, 2021 at 8:05
• 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…) Sep 29, 2021 at 13:14
• fraxinus you are mistaking the input pullup resistor with the output current limiting resistor. Sep 29, 2021 at 13:14

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

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.

• 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. Sep 29, 2021 at 13:55
• 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 Sep 29, 2021 at 14:00
• @LorenzoMarcantonio Thats really an excellent answer man.. Thanks a lot Sep 29, 2021 at 14:01
• 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. Sep 29, 2021 at 14:06
• +1 - for including the schematic - This should be the accepted answer..! Sep 29, 2021 at 14:51

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.

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).

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.