Help needed! I am using an Arduino Nano and a general purpose npn (2n2222a) transistor to provide a 12v signal to a low current 12vdc load in an automobile. The Arudino code is set to pulse D2 at bootup/startup for .5 second. The PWM output setting was 255 (since switched to digitalWrite). However, the switched output measures only 4.5v (expected close to 12v). What am i doing wrong? Applying 12v+ directly to the black box works. The purpose is to simulate a temporary button push in the car so an auto function is enabled.
The problem is that you are using an NPN transistor as a high side switch.
The voltage at the emitter of the transistor will always be about 0.7V below the voltage at the base. The "high" output from the Arduino is 5V. The emitter will be at about 4.3V.
Have a look at the Wikipedia "emitter follower" page. The emitter follower is also called a common collector circuit. That explains the "problem" in more detail. It is only a problem in so far as it is not the correct solution for your task.
You need to use a "common emitter" circuit.
This usage is commonly called a "low side switch."
That would look like this:
If your black box really needs its input pulled up to 12V, then you would do something like this:
The resistor values and transistor part numbers are place holders. The values of resistors you will need depends on the transistors you use. The transistor selection will depend on how much current you need to deliver to the black box.
I see in a comment that your black box only needs a few milliamperes. In that case, the shown transistors should work just fine. The resistor values may be a little too high, but I think they'll be OK.
A couple of additional points:
- The Arduino has PWM outputs, not PCM.
- The output voltage for your black box will be pulsing if you use any PWM value lower than 255. Your black box probably won't like that, and your voltmeter won't read a correct voltage.
The 2N2222 is an NPN device. Skipping all of the physics involved, that means that (in your application) it will sink current as a low-side switch, but not source current as a high-side switch.
If you want a high-side switch, you should find a PNP transistor. In this case your Nano pin will sink current from the transistor's base terminal.
I would recommend you stick with the 2N2222, but make two changes:
Connect your "black box" between the positive terminal (+) of your 12V supply, and the collector terminal of the 2N2222.
Reduce the size of your base resistor from 10Kohms to 1Kohms
Connect the Arduino pin output to pin 1 through 330 ohms resistor, pin 2 to ground of Arduino, connect pin 4 to Vcc of 12 V and finally connect pin 3 to the black box load.
Try this configuration
Since black box only needs few milliamps as you said May be the transistor is not saturated since base current has to go through the black box load opto-coupler s have no base current instead they use photons
Here is the circuit diagram
5 V supply looks like the Arduino pin D2 and lamp is the black box load. This is 4N35 example
You can use a relay if you want
Assuming you only need to turn on and off the blackbox
Refer more here