Detect if a button is being pressed from an external circuit

Hello.

Im trying to get an arduino to detect if a button is being pressed or not (look at the image below).
The pushbutton and LED is not being powered by the arduino but instead by the power source that also powers the arduino.

My idea was to connect the negative side of the switch (after the LED) to a transistor that would then be connected to an arduino pin, but I am worried that the full 12v or so will flow through the transistor and into the arduino pin.

Ive searched online for a while but with no look as I don't quite know what terms to search for in this instance.

Any help is greatly appreciated,
James

I attached a second image showing my thoughts on how to go about this

(excuse the crudity)

12V directly into your Arduino will fry it, but it can be done.

Connect a [u]Voltage Divider[/u] (two resistors) to the junction of the switch and the resistor that goes to the LED. (Choose resistors that add up to around 10K.)

With the proper resistors, you'll get about 5V when the switch is on, and when the switch is off, the resistor & LED, and the resistor in the voltage divider will "pull down" the input to ground.

An alternative to a voltage divider is [u]protection-doide circuit[/u]. (I'd change the resistor in those circuits to between 1K and 10K.)

DVDdoug:
12V directly into your Arduino will fry it, but it can be done.

Connect a [u]Voltage Divider[/u] (two resistors) to the junction of the switch and the resistor that goes to the LED. (Choose resistors that add up to around 10K.)

With the proper resistors, you'll get about 5V when the switch is on, and when the switch is off, the resistor & LED, and the resistor in the voltage divider will "pull down" the input to ground.

An alternative to a voltage divider is [u]protection-doide circuit[/u]. (I'd change the resistor in those circuits to between 1K and 10K.)

What if I just change the transistors collector to the 5V on the arduino? will that still supply the 12v to the pin as thats the voltage at the gate?

If not ill stick with the voltage divider.

Thanks for your help!

I think this will ensure 5V levels only.

Use the transistor.

NPN transistor, emitter to ground. Collector to Arduino input using INPUT_PULLUP.

Base to switch in original circuit, via 22k resistor.

CrossRoads has moved the switch around, unnecessarily, and the base resistor would (FWIW) continue to bias the LED with some current. Shame on you CrossRoads!

You do not need to have the input pulled HIGH when an event occurs, your code can always be written to account for an input going LOW when the event is true.

Thanks for the replies!

Unfortunately I do not have any NPN transistors (only PNP transistors) so it looks like I will go with the voltage divider option.

Incase anyone is wondering why I am powering the button and LED with the 12v and not the arduino power, its because the button is being placed on the other side of a room which is too far for the arduino to power.

James.

(why no NPN transistors? You should probably order some general purpose npn transistors, just for stock - NPN is much more useful than PNP - and general purpose, nothing-special npn transistors are a couple of cents each in lots of 100. Even if you don't need them now, you'll need them sooner or later.

James1402:
In case anyone is wondering why I am powering the button and LED with the 12v and not the Arduino power, its because the button is being placed on the other side of a room which is too far for the Arduino to power.

That makes even less sense than not having NPN transistors.

Let's say this was a 1W white LED which requires 300 mA, and you were feeding it from a 12V supply, Dropping 9V at 300 mA, you would want a 30 ohm resistor. I doubt even reasonably light gauge wire would have more than three ohms resistance which would reduce the current by only 10%. If you did in fact power it from 5V, you would want 6 ohms resistance and you could simply measure the wiring resistance and use it as part of that 6 ohms.

But you would not be expecting to draw 300 mA from the Arduino anyway, so that (the wire length) is clearly not the concern. And to operate a common (white) LED at 20 mA from 5V, you use a 90 ohm resistor so wiring resistance is inconsequential.

Nope. It doesn't make sense.

DrAzzy:
(why no NPN transistors? You should probably order some general purpose npn transistors, just for stock - NPN is much more useful than PNP - and general purpose, nothing-special npn transistors are a couple of cents each in lots of 100. Even if you don't need them now, you'll need them sooner or later.

You make a good point, just ordered some there!

Paul__B:
That makes even less sense than not having NPN transistors.

Let's say this was a 1W white LED which requires 300 mA, and you were feeding it from a 12V supply, Dropping 9V at 300 mA, you would want a 30 ohm resistor. I doubt even reasonably light gauge wire would have more than three ohms resistance which would reduce the current by only 10%. If you did in fact power it from 5V, you would want 6 ohms resistance and you could simply measure the wiring resistance and use it as part of that 6 ohms.

But you would not be expecting to draw 300 mA from the Arduino anyway, so that (the wire length) is clearly not the concern. And to operate a common (white) LED at 20 mA from 5V, you use a 90 ohm resistor so wiring resistance is inconsequential.

Nope. It doesn't make sense.

So your saying I could power the LED directly from an arduino pin thats approx. 2.5 meters away? I was told elsewhere that this was not a good idea as it could damage the pin of the arduino.

The Pins on the Arduino are for "signals". These Signals are used in digital logic to state that the pin is on logic level state "1" or logic level state "0".

To turn these signals into peripheral device controllers, we need a few things. 1) we need to know how much current your peripheral device will draw 2) we need some muscle (already mentioned... get some NPN transistors) 3) we need some control over signal currents (get some resistors)

Item 1 means, find your devices data sheet (IE; LED's specifications and look at how much current it needs.)

Item 2 means, if the load wants less than 400 mA, you can use most "common" small signal transistors. IEl 2N2222A If it needs more than that, then you need to start searching for a "power" transistor (engineers usually reach for a logic level mosfet at this point) It doesn't hurt to order some STP16NF06L from Digikey at this point anyway.

Item 3 means, protect your parts. If you use a npn style transistor like 2N2222A, you want a value like 1000 ohms between the pin and the base pin on the transistor. If you use a Mosfet, to protect you from "yourself", add a 150 ohm resistor from the pin to the gate and a 10,000 Ohm resistor from the gate to GND.

See drawing (yeah, I know I called the gate a "base", sue me)

James1402:
So your saying I could power the LED directly from an Arduino pin that's approx. 2.5 meters away? I was told elsewhere that this was not a good idea as it could damage the pin of the Arduino.

Hang on! I am saying that you need to tell us what you really want to do. I am saying that the resistance of the wire (if it is only the other side of the room) is not the problem. the question is - what sort of LED do you want to use; for what current is it rated? Then we can tell you whether it is practical to use the 5V from the Arduino or whether it is critically necessary to connect to the 12V as you originally described.

And as usual, exactly what do you want to do and why? What is the purpose of the exercise anyway?

The Pull Down in pwillard's FET circuit is not optional, but should be on the left hand side of R4.

...as you might have guessed... some verbage on the drawing is a bit off... I was rushing it.

pwillard:
The Pins on the Arduino are for "signals". These Signals are used in digital logic to state that the pin is on logic level state "1" or logic level state "0".

To turn these signals into peripheral device controllers, we need a few things. 1) we need to know how much current your peripheral device will draw 2) we need some muscle (already mentioned... get some NPN transistors) 3) we need some control over signal currents (get some resistors)

Item 1 means, find your devices data sheet (IE; LED's specifications and look at how much current it needs.)

Item 2 means, if the load wants less than 400 mA, you can use most "common" small signal transistors. IEl 2N2222A If it needs more than that, then you need to start searching for a "power" transistor (engineers usually reach for a logic level mosfet at this point) It doesn't hurt to order some STP16NF06L from Digikey at this point anyway.

Item 3 means, protect your parts. If you use a npn style transistor like 2N2222A, you want a value like 1000 ohms between the pin and the base pin on the transistor. If you use a Mosfet, to protect you from "yourself", add a 150 ohm resistor from the pin to the gate and a 10,000 Ohm resistor from the gate to GND.

See drawing (yeah, I know I called the gate a "base", sue me)

Appreciate your time in explaining that to me as well as including the image. It has really helped a lot. However, I'm using the pin to detect weather the switch (which would be in series with the diode) is open or closed.

Paul__B:
Hang on! I am saying that you need to tell us what you really want to do. I am saying that the resistance of the wire (if it is only the other side of the room) is not the problem. the question is - what sort of LED do you want to use; for what current is it rated? Then we can tell you whether it is practical to use the 5V from the Arduino or whether it is critically necessary to connect to the 12V as you originally described.

And as usual, exactly what do you want to do and why? What is the purpose of the exercise anyway?

The Pull Down in pwillard's FET circuit is not optional, but should be on the left hand side of R4.

I think I am making this ALOT more complicated than it is, so Ill try explain it again.

I have a switch which is located on the other side of the room where the arduino is located. This switch is used to arm/disarm a room security system and it has a LED (which has a 20mA max limit) in series with the key switch to show if the switch is open or closed.

However, since its about 1.5 meters away from the arduino I assume this is too far for it to power the LED + provide enough voltage back to the pin to give it a digital HIGH or LOW value.

I tried creating another schematic (below) which might explain it better.

(BTW, the reason its a 12v source was because that was the voltage supplied by the power source I was using to power the arduino (its what I had laying around). The switch isn't powered by a separate power supply, just not powered by the arduinos 5v reg)

EDIT: I only realised now that I forgot the pull up resistor on pin 2 in the diagram

Ah, my misunderstanding that you were trying to control the LED.

Maybe you want something more like this: (attached)

James1402:
EDIT: I only realised now that I forgot the pull up resistor on pin 2 in the diagram

That circuit will work just fine except that R2 should not be there. You do not need a pull-up resistor on pin 2 because you use the internal pull-up however you probably should add a 10k pull-down from the transistor base to ground in case of leakage in your long wiring.

The resistor R1 for a 20 mA LED would be 12/.02 = 680 ohms.

That will certainly work - 10 mA to the LED. If however the 12V supply voltage significantly exceeds 12V, it will pull the Arduino input over 5V and being a quite "stiff" voltage divider, could feed quite a few mA into the protection diode.