Connecting a 12 V Illuminated Toggle Switch to a 5 V Circuit

The previous page discussed the inability to rewire the insides of a 12 V illuminated toggle switch to make it compatible with 5 volts.

The toggle switch either provides 12 V when switched on, or nothing (not ground) when switched off. We want to connect this to a 5 V microcontroller input to detect the position of the switch. If the 12 V switch were wired directly to the microcontroller input, the 12 V power source would damage the 5 V microcontroller.

The other desired feature is to illuminate the 12 V LED atop the toggle switch, regardless of the position of the actuator. This isn’t as simple as connecting the LED ground to a microcontroller output pin, because the LED will light up slightly or moderately even if the pin is in a tri-state (input) or 5 V (output high) state. So, we’re going to need some circuitry to convert the microcontroller output signal to ground (LED on) and disconnected (LED off).

Interfacing 12 V to 5 V

My first attempts at a 12 V interface consisted of some combination of voltage dividers and either an ordinary diode or a zener diode. That probably would work, but I decided to go with a classic transistor level-converter instead:

Higher to lower voltage level shifter circuit on breadboard

Higher to lower voltage level shifter circuit on breadboard.

The common wire of the switch is connected to the input. The microcontroller is connected to the output.

When the toggle switch is in the off position, no current flows into the 10 kΩ resistor that connects to the base pin of the 2222A NPN bipolar transistor. That means the transistor is in the off position, which means the only thing electrically connected to the output is the 22 kΩ resistor that is wired to 5 V. The microcontroller will see the 5 V supplied by the resistor.

When the toggle switch is in the on position, current flows from the 12 V source, through the 10 kΩ resistor, through the base lead of the transistor. The transistor turns on, thus connecting the output pin to ground. The transistor output provides a much greater current source than the weak 22 kΩ resistor, causing the microcontroller to see GND.

Assuming the circuit output is connected to a CMOS-based microcontroller input pin, the circuit uses no power when the switch is in the off position. In the on position, it uses about:

(12 V power source - 0.6 V base drop) ÷ 10000 Ω = 0.00114 A = 1.14 mA
+ 5 V ÷ 22000 Ω = 0.00023 A = 0.23 mA
= 1.37 mA

The downsides to this circuit are:

Interfacing 5 V to 12 V

Now that the microcontroller can read the pilot toggle switch position, I want to be able to independently control the lighting of the switch’s LED. That way, the LED can blink or illuminate even in the off position, to alert the user that it is now time to toggle the switch.

Lower to higher voltage level shifter circuit on breadboard

Lower to higher voltage level shifter circuit on breadboard.

An output pin of a microcontroller (or anything else from the 5 V circuit) is connected to the input. When the circuit input is GND or disconnected, no current will flow through the 1 kΩ resistor to the base of the 2222A NPN bipolar transistor. The transistor is off, which means the output sees a 12 V signal from the 22 kΩ pullup resistor.

If the toggle switch’s LED ground wire is connected to the output, the LED will not light when the output is 12 V. That’s because the other wire of the LED inside of the toggle switch is always connected to 12 V. 12 V on both sides of the LED does nothing. In order for the LED to illuminate, it needs 12 V on the anode and GND on the cathode.

When the circuit input is 5 V, then current flows through the 1 kΩ resistor, through the base of the transistor, which turns on the transistor. When the transistor turns on, it outputs ground. Now the LED sees GND on the cathode (because the relatively weak 22 kΩ resistor connected to 12 V cannot compete with the transistor ground source) and 12 V on the anode. Thus the LED in the toggle switch illuminates.

There is some slight trickery going on to get the LED to turn on and off regardless of the position of the toggle switch. You have to swap the switch’s 12 V and common wire. That is, wire 12 V directly to the switch’s common wire, and have the switch’s 12 V wire act as common. In doing so, the anode of the LED is always connected to 12 V, and only the ground wire determines whether the LED turns on or off.

Finished Pilot Switch Interface Board

The pilot switch was being shipped to me when I first designed the model rocket launcher controller PCB. As such, I hadn’t included any level-shifting circuitry into the main PCB, because I didn’t know about the problems with the switch.

Level-shifter circuit board shrink wrapped.

Level-shifter circuit board shrink wrapped.

In order to connect an out-of-the-box factory-standard 12 V illuminated pilot toggle switch to a 5 V circuit board, I soldered a complete level shifter onto a plated five-hole-bus perf board. I didn’t include either of the 22 kilohm pull-up resistors because the microcontroller has one built in and the toggle switch LED doesn’t need one.

Since the level-shifter would be floating around loosely in the project box, there is a concern that it will make accidental electrical contact with other boards or bare connectors, causing resets or component damage. So, I covered the board with 3/4-inch diameter 3M heat shrink tubing from Mouser Electronics (#5174-1343, $2.68 for 4 feet) and shrunk it in place with a hot air gun.

Printed markings on illuminated toggle switch

Printed markings on illuminated toggle switch.

Two wires from the level-shifter connect to the switch. The LED Ground wire connects to the switch terminal that has a ground symbol printed next to it. The 12 V Switch State wire attaches to switch terminal that has a plus symbol printed next to it. The remaining switch wire attaches to a 12 V power source.

Although it is a bit annoying to add a couple of transistors and resistors to interface to a 12 V switch, the illuminated safety-cover pilot’s toggle switch works well and looks awesome.