# Read the state of a 12v illuminated switch from an Arduino Uno

I've got an Arduino Uno, and I want it to be able to read the state of a 12v "Illuminated Switch" in a way that allows the switch to properly illuminate. I have an external 12v, 1.5A power supply that I can use to light up the switch. The switch is listed as SPST, but has three connectors on it, due to the built-in LED and resistor. The three connections are POWER, ACC, and GND, and the first two short when the switch is closed.

The Arduino has a digital input mode called INPUT_PULLUP that enables a built-in 20k pullup resistor on a digital input line. A typical non-illuminating switch would simply short the pin to ground, and the software can then read HIGH for open and LOW for closed.

This is my idea for reading the 12v switch using an NPN transistor, but I'm not an EE and I'm not sure where the resistors go or what strength they should be. The NPN transistor I have handy is a 2N222A (Vceo 40, Ic 600, power dissapation 625 mW), but I'm open to swapping that with something else. The resistors I'm less sure of. I think there should be R1 between ACC and the base, because there's +12v flowing there when the switch is closed. And I also think I need a pull-down resistor R2 on the base. Am I on the right track here, and if so, what resistance should R1 and R2 be?

The illuminated switch is a Radio Shack 275-0010, if you need more detail on that. I'm looking for an answer that will show a reliable, minimal way to read the state of this particular switch on the Arduino, including the correct values for any resistors needed on the circuit. Thanks!

You are pretty much on track, resistor value is not really critical here, you can use 10K for both resistors, that will work fine.

simulate this circuit – Schematic created using CircuitLab

You can make a thevenin equivalent circuit for the open and closed switch situations, for the closed switch:

simulate this circuit

When you press the switch, you will be driving the base with around 1mA, that should make the transistor drive the 20K pullup low very comfortably (in this case, the transistor should be able to drive hfe*ib through the collector, which will be at least a hundred mA)

When you release the switch:

simulate this circuit

You connect the base to ground through 10Kohm, ensuring you turn off the transistor. Quite simple and effective.

• I ended up essentially doing this, although I upped R1 to 47k. Also I added an additional 1k resistor on INPUT_PULLUP to protect the Arduino in case it ever went into OUTPUT mode on that pin by mistake. Works fine, thanks! May 17, 2016 at 14:57

You can use this optically isolated circuit:

When you receive 12V on left side, LED glows and sends a HIGH to arduino GPIO. When 12V is not there, LED is OFF and you get a LOW on arduino GPIO.

If 12V supply has a separate GND, better use that one on left side. Use common GND only if there is already a common GND between arduino and that 12V supply to begin with.

R-C circuit on the right side will take care of debouncing so you don't have to do in your code. If you are comfortable with code, you can get rid of the capacitor. You can also change the resistor from 390K to 10K.

• Op is using an input with pull-up enabled. This will never trigger the input, as it will always be High. Apr 10, 2016 at 21:33
• I can turn off PULLUP if need be. What are the risks of tying together the Arduino ground and 12v ground? The Arduino is getting power via USB, and the 12v power is an extra power brick I have laying around. Apr 10, 2016 at 21:40
• @emackey if you use the Opto isolator, you don't need to tie them together. If you don't use the isolator, then you have to tie the two grounds together, so that the two circuits have the same Ground reference voltage. The risks is ground loop/noise, which isn't really an issue unless you are doing a lot of analog readings or audio. Apr 10, 2016 at 21:55

You could directly connect Switch's GND (which is the Ground/Cathode end of the led) to the NPN transistor you want. Optionally, if you see the input floating or switching often, add R(Opt), a pulldown to force the transistor off, when the led/switch is off. The 12V supply's ground and the Arduino's ground must be connected at some point. If you are powering the Arduino from the 12V supply, then they already are.

simulate this circuit – Schematic created using CircuitLab

This works, assuming the LED current is a typical 20mA or less.

AND the rocker switch uses an LED and not a miniature incandescent or other Bulb!

You will see little or no reduction in the led brightness. A 0.7V drop is hardly visible at 20 mA.

The formula for biasing the transistor, and biasing an led, is essentially the same. Source Voltage minus Forward Voltage, divided by Forward Current (or Resistance)

( VSource - VF ) / IF

With two diodes, the base diode and the led, add these forward voltages and assuming 20 mA forward current, just plug in the numbers. If red/orange, assume 2.1V, otherwise 3.2V for blue/white/green/etc.

(12V - (0.7V + ~2.1V) ) / 0.02A = (12 - 2.8) / 0.02 = 9.2 / 0.02 = 460Ω
(12V - (0.7V + ~3.2V) ) / 0.02A = (12 - 3.9) / 0.02 = 8.1 / 0.02 = 405Ω

• Interesting! The switch is indeed an LED switch with some unspecified built-in resistor, and I love the idea of re-using that to get the job done. Apr 10, 2016 at 21:35