I'm a bit stumped on how to switch on a solenoid using a transistor using my Arduino Nano's 5v Output. The solenoid runs on 12V, so I used an NPN transistor to connect the 12V source to the solenoid whenever Arduino sends current to the transistor's base. I connected all the grounds together to the Nano's ground pin, but the solenoid still doesn't switch.

I know the transistor works properly, because the solenoid switches when I use the battery to send base current instead of the Nano. I would think the Nano is capable of sending enough current to activate the Collector -> Emitter path but might be wrong. I'm also wondering if it's an issue with grounding the circuit, but I've made sure there is a common ground. What do you think I'm doing wrong? BTW the transistor I am using is D882P. I would appreciate any help.


simulate this circuit – Schematic created using CircuitLab

  • 1
    This is really an electronics question, because there is nothing specific about the Arduino here, except that it has a maximum output of around 20ma. You have a few problems here, including that the load should be on the "high" side (in series with the collector), there is no flyback protection diode, and that there should be a base resistor to limit current through base. Would you like me to migrate this question to Electronics Stack Exchange?
    – Nick Gammon
    Commented Aug 22, 2020 at 6:18
  • Please post your code too. Commented Aug 22, 2020 at 6:18
  • Example answer here: Can arduino be used as a switch?
    – Nick Gammon
    Commented Aug 22, 2020 at 6:22
  • It would probably be closed as a duplicate if I migrate it, I warn you. Try searching that site for ideas.
    – Nick Gammon
    Commented Aug 22, 2020 at 6:26
  • 1
    I have a page about driving devices from the Arduino on my own web site, if you want some ideas and lengthier discussions about resistor values. However Electronics Stack Exchange is a great site for general electronics questions.
    – Nick Gammon
    Commented Aug 22, 2020 at 6:30

1 Answer 1


Low-side switching an inductive load (such as a relay) using a microcontroller (such as Arduino), NPN BJT transistor (with full base resistor calculations), and snubber/flyback diode

Note: this answer demonstrates using the 2N3904 NPN transistor for all of the calculations, since that's what's in the schematic in the question. However, after writing the answer, I noticed the words at the end of the question state it is a D882P (also an NPN type) transistor. So, just follow my methodology here but use the appropriate values for your NPN transistor type, which you can obtain from the datasheet. A quick Google search for "d882p datasheet" reveals this as a potential datasheet.

2N3904 is an NPN transistor (datasheet), as opposed to a PNP transistor, so it must be connected directly to GND and switch the low side of the circuit only, so wire it like this instead:


simulate this circuit – Schematic created using CircuitLab

Notice I've added a diode, D1, to dissipate inductance-induced voltage spikes created when electromagnetic fields (a form of energy storage) collapse in the inductor each time you turn the switch OFF. This diode, due to its function (not type), is called a "flyback", "snubber", or "clamp" diode. Again, it protects the transistor and other components from inductance-induced voltage spikes from the relay coil/inductor. When a voltage spike occurs, it will simply travel around that loop through the diode, relay, and resistor R2 again and again and again until it has all been dissipated as heat in those components and there is no more voltage spike remaining.

I've also added a resistor R2 to limit current through the relay coil. You'll have to size this appropriately to get the correct current through your relay coil, based on the relay's datasheet. If your relay's datasheet says to hook up the relay to 12V directly, you can remove this resistor.

And you'll definitely need a base resistor (R1 in my circuit) or else you'll burn up your Arduino pin.

First, let's collect some necessary values from the Datasheet. These will be used in the calculations below:

  1. VBE(sat) = 0.65 to 0.95V.
    1. This is the diodic voltage drop from Base to Emitter when the transistor is "saturated", or fully ON.
  2. hFE_min = 30
    1. Notice in the screenshots below that I have crossed out all of the 2N3903 transistor hFE entries so they don't distract us from the 2N3904 entries, which is the transistor type I'm using for this example. I then chose the lowest hFE entry for this transistor, which is 30.
    2. This is the "guaranteed" minimum, or worst-case, DC current gain. The formula is simply: I_max_permitted_from_Collector_to_Emitter = gain * I_Base_to_Emitter. So, if your gain is 30, for example, and you drive a base current of 5mA from Base to Emitter, then the "guaranteed" maximum possible current allowed from the Collector to the Emitter will be 30 gain * 5mA = 150mA. Again, this is not what you will get into the Collector, this is what you could get before the transistor itself starts limiting the current into the Collector rather than your load limiting the current through the Collector.
  3. Ic_max_continuous = 200mA
    1. This is the max continuous current you can get from Collector to Emitter without overheating the transistor (assuming, I think, you have adequate cooling through conduction and possibly even convection and radiation via heat sinks--I need to brush up on the thermal and power equations myself). The datasheet may specify if a heat sink is required to achieve this current, but regardless, you should keep your continuous current down to about half this value or less if possible, as the device may reach crazy high temperatures of like 150 C or something with this current and no convective cooling or heat sink. For short bursts, hitting this current is just fine.

Here are some screenshots of where to find these values in the datasheet:

enter image description here

Notice in this next image I've crossed out all of the 2N3903 hFE lines so they don't distract us from the 2N3904 entries, which are for the transistor we care about in this example. Choose the lowest hFE value as the gain you will use in your estimates in order to have a "conservative" estimate, virtually guaranteed to fully turn on your transistor in all cases (ie: for all Collector current values you might like to switch on and off).

enter image description here

Now, let's calculate a Base resistor value for an NPN BJT transistor, assuming your goal is to fully turn on (saturate the Base-Emitter channel of) the transistor:

VBE(sat) has a diodic voltage drop of 0.65 to 0.95V, so your resistor would have to burn off 5 - 0.95 = 4.05V to 5 - 0.65 = 4.35V. Let's just use the lower 4.05V value so we will size for higher currents and faster turn-on speeds.

Your minimum hFE (gain) value for the 2N3904 transistor (not the 2N3903) is 30. Maximum continuous Collector current, Ic is 200mA, so let's increase that by a factor of 2 to 4 and size for 200mA * 3 = 600mA to 1) more fully saturate the Base to Emitter channel and get closer to full turn on under all cases, 2) get faster turn-on, and 3) overcome the fact that decreased gain occurs at higher Collector currents. In Figure 15. DC Current Gain, from the datasheet, for instance, shown below, it shows that the normalized gain decreases from ~0.28 at 100mA Collector current to ~0.13 at 200mA Collector current, which means that the gain of 30 found in the datasheet at 100mA Collector current indicates the gain at 200mA Collector current will actually be approximately 30 gain/(0.28/0.13) = 13.9 gain. Simply multiplying the Collector current we are sizing for by a factor of 2 to 4 (3 in our case) accounts for this fact, so we will use 600mA instead of 200mA in our maximum Collector current calculations below.

enter image description here

So, 600mA / 30 gain = 20mA Base to Emitter to get rock solid turn-on performance. Arduino can do up to 40mA per pin so this is Ok. Note that I recommend 30mA max per Arduino pin (for the ATmega328 microcontroller), and there is also a combined output limit for the ATmega328 microcontroller of 200mA for all pins combined, which you should keep in mind. I describe this on my website in my article "Arduino Power, Current, and Voltage Limitations" here.

V = IR, so R = V/I, so resistor, R that we need = 4.05V/0.020A = 202.5 Ohms. This is the minimum recommended base resistor size. Don't go too much smaller than 200 Ohms, if you go smaller than that at all. A 100 Ohm resistor, for instance, would allow a maximum base current of I = V/R = (I intentionally use the higher voltage value here) 4.35V/100 Ohms = 43.5mA, which could damage your pin.

Don't go larger than 600 Ohms or else you risk not turning on the transistor fully by not saturating the Base to Emitter channel. A 600 Ohm resistor would give you a guaranteed max Base to Emitter current of I = V/R = (I intentionally use the smaller voltage value here) 4.05V/600 Ohms = 6.75mA, which with a gain of 30 would give you a max Collector current (based on your relay coil resistance now) of 6.75mA * 30 gain = 202.5 mA. However, as indicated from Figure 15 shown above, a more realistic gain at 200mA Collector current might be only 13.9, which means your maximum allowed Collector current with a 600 Ohm resistor would be 6.75mA * 13.9 gain = 93.8mA. But, at this current your gain would actually be > 30, not 13.9, which leads back to the 200mA Collector current number. You can see this is a juggling game of numbers since the gain is NOT constant NOR linear, but rather is a function of Collector current, as shown by Figure 15 above. So, perhaps your actual maximum allowed Collector current with a resistor of 600 Ohms would be more like 130mA or something. The point is: any larger of a base resistor than 600 Ohms and you most likely won't be able to achieve the transistor's rated maximum continuous Collector current even if you wanted to. So, there's a reasonable R value range!: 200 Ohms to 600 Ohms, perhaps even leaning more towards the 200 Ohms side if you expect to have high momentary Collector currents anywhere near your maximum continuous Collector current rating of 200mA.

Additional notes on high-side vs low-side driving with NPN BJT or N-Channel MOSFET transistors, for clarity:

An NPN transistor can switch high-side voltages too--you just have to drive the necessary Base to Emitter current is all (and don't exceed any maximum ratings)! That's it! So...if you try to drive the high side, think about it: you turn it on with 5V so your voltage from Collector to GND can't exceed ~5V now because as current starts to flow through the collector, voltage would come up on the Emitter side until your base current no longer flows. Your system would be fighting itself. Even if you gave it 12V before the Collector you'd never see more than ~5V at the Emitter, assuming your Base drive voltage was 5V. If you can design around it, go for it, but generally the "easy" or "normal" way to do things is to use NPN to switch low-side voltages and PNP to switch high-side voltages.

The same goes for N-channel and P-channel MOSFETS. Use N-channel to switch low-side voltages and P-channel to switch high-side voltages, UNLESS you are doing some fancy driving (again), of N-channel to switch high-side voltages, as N-channel MOSFETs are more efficient and can switch higher currents than their equivalent P-channel pairs, due to some electron hardware mumbo jumbo stuff.

Keywords: transistor sizing; BJT transistor circuit calculations, equations, and datasheet; arduino and electrical engineering: transistor (BJT) sizing, equations, datasheet, and calculations example

Additional reading:

  1. [my answer] Electronics Stack Exchange: When is a MOSFET more appropriate as a switch than a BJT? [and a BJT more appropriate than a MOSFET]
  • 1
    They said they had a D882P transistor in fact (still looks like an NPN though).
    – Nick Gammon
    Commented Aug 22, 2020 at 6:25
  • Ah shoot, you're right. His image is deceiving. Commented Aug 22, 2020 at 6:28
  • @NickGammon, I went ahead with the calculations anyway. I provide the example. They can figure it out for their case. I needed the practice on these calcs anyway. I haven't done them in a while. I miss electronics and EE. Feel free to migrate this question if you see fit. Your call of course. Commented Aug 22, 2020 at 6:48
  • Ah shoot. I forgot my flyback/snubber diode too! I'm away from the computer now. Oh well. I'll update it later. Commented Aug 22, 2020 at 7:22
  • Thanks for the detailed explanation. Why does switching low side voltages with NPN work? Sorry, my understanding of electronics is very basic
    – I Like
    Commented Aug 23, 2020 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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