# Using a transistor for interfacing different voltages

I have some solenoid valves that I want to drive from a little beaglebone gpio. The beaglebone is a little ARM based microcontroller.

The gpio on the beaglebone outputs 3v3, and the beablebone itself runs on 5V. The solenoid valves run on 12V. I have some 2n2222a transistors and I thought there was a way to use these to run the 12V devices from the 3v3 GPIO in open collector mode (http://en.wikipedia.org/wiki/Open_collector). What I'm confused about is what this circuit should look like.

I know I need to hook the positive of the 12V supply up to the positive end of the solenoid valve. Then, I think I should hook the negative side of the solenoid valve up to the collector of the transistor. The gpio connects to the base of the transistor via a resistor, enabling me to use the transistor as a switch on the 12V line.

What I don't understand is:

1) Which ground should the emitter of the transistor go to? The one for the beaglebone (which comes off the 5V supply) or the one on the 12V supply?

2) How can I figure out what ohm value of resistor to put between the gpio and the base?

For reference, a datasheet for the 2n2222a can be found here: http://www.csus.edu/indiv/t/tatror/projects/met%20highway%20safety%20project%202010/npn%20transistor.pdf

• Can the GPIO in OC mode tolerate 12V? Commented Dec 14, 2013 at 5:49
• No, absolutely not - that's why I was trying to isolate the 12V from the GPIO by using the transistor. That wikipedia page about open collector mode talks about using a transistor to interface different voltages, so that's what I was trying to do here. Commented Dec 14, 2013 at 6:05
• Consider a 7407 then. Commented Dec 14, 2013 at 6:08
• What are the specs of the solenoid, how much current? Commented Dec 14, 2013 at 8:21

## 2 Answers

Actually I didn't notice this was about an open collector output until miceuz posted his comment, so for OC output I would use one of these

and the isolated version

If the GPIO can't provide enough current to the optodiode to get the appropriate output current in the transistor side then you'll have to add another transistor either to the input or output.

Note that resistor values may vary depending on the requirements

2) How can I figure out what ohm value of resistor to put between the gpio and the base?

A rule of thumb is that you have to provide 1/10 to 1/20 of the output current in the transistor base in order to saturate the transistor and get a low collector-emitter voltage drop (which means higher voltage output and less heat dissipation)

• In the first diagram, is the purpose of Q2 simply to invert the OC signal? Commented Dec 14, 2013 at 12:00
• @RedGrittyBrick I added that transistor for two reasons, the first one is that the driver will have a default output state that is off when no signal is applied in the input which is usually a good idea. The second reason is to be able to use a high value pull up resistance so that the GPIO doesn't have a hard time pulling it down and in the same time keep the consumption low when the circuit is off. Depending on the number of GPIO used, driver strength of the mcu and solenoid current requirement a circuit with just one transistor can be used too. Commented Dec 14, 2013 at 15:21

Normally, you connect the grounds of your MCU power supply (3.3 & 5V) and your solenoid power supply (12V), in a circuit like this:

simulate this circuit – Schematic created using CircuitLab

If this is undesired for some reason, then you'll have to employ some device to galvanically separate the MCU from the solenoid, eg an opto-coupler:

simulate this circuit

The resistor values should be calculated based on the desired current through the transistor / LED and the known voltages (forwarding voltage of the base-emitter diode / LED, GPIO high-level output). For both the transistor stage & the LED in the opto-coupler, a margin of 1:5..1:20 is desireable to keep the output saturated. So you calculate the output current needed, divide it by the data sheet typical hFE / CTR figures and multiply it by 5..20. The "pull-down" resistor should be sized to take a smaller fraction (~5-15%) of the calculated base current.

• Ah, ok, I see - you just parallel the ground connections. And you have a flyback diode around the solenoid. Thanks for sending this. How can I figure out what resistor values I should use? Commented Dec 14, 2013 at 6:41
• Are you sure this will work for a GPIO pin in open collector mode? Commented Dec 14, 2013 at 9:26
• @miceuz: of course, it'll not work. But there's really not much point in using open-collector mode here. The OP was planning to use it probably because he thought it was useful here. Commented Dec 14, 2013 at 11:43