-2

Hoping someone can help me with connecting my 12V push button LED circuit to a PI GPIO. I want to be able to sense when the button is pressed and the LED lights up using python.

From what I've read I know I will need at least one resistor to bring down the 12V to 3.3V and I need to connect to a common ground.

I will power the Pi with a separate USB input, so I can avoid buying any transistors or stepdown devices. I think I only need one or two resistors - maybe 1K and 1000K ohm???

Not sure where to insert into current circuit and what size resistor(s) to use

Connect 12V LED circuit to Pi GPIO

EDIT: Here is the actual LED/button combination Amazon LED/button

And yes - it does work fine with the 12V wall wart. Lights up when circuit closes. I think I can get the voltage down to 3V with a couple resistors in series so GPIO will be able to trap button presses

Here's a Circuit Lab diagram of what I'm trying - not sure about resistor values yet or if I should add another one down to Ground like you show?

current attempt

dbmitch
  • 381
  • 5
  • 19
  • "I have a button that hosts the LED." - this belongs in the Question. You need to supply details of the device you ACTUALLY have - but I doubt it is usable (without significant additional circuitry. – Milliways Feb 15 '22 at 02:09
  • No - sorry not at all - I just want to use the Pi to detect when the outside button circuit is closed (by it being pressed). The GPIO on the Pi can be monitored with code and do something when the button is pressed. – dbmitch Feb 15 '22 at 18:14
  • OK - sorry for the confusion... you edited your question & added the schematic - which answered the Q in my comment, so I deleted my comment :P – Seamus Feb 15 '22 at 18:22
  • Your diagram is helpful, but it omits a detail. I think I understand your question now & will post an answer shortly. In the meantime, please take a look at [this related Q&A](https://raspberrypi.stackexchange.com/a/127437/83790) & let me know if you're averse to using Zener diodes? – Seamus Feb 15 '22 at 18:37
  • It's not that I'm against diodes it's just that I have a whack of resistors - no diodes on hand. – dbmitch Feb 15 '22 at 18:38
  • 1
    DO NOT do this. You will put 12V on the GPIO and damage the Pi. You NEED a divider. If you are lucky the series resistors may limit the current to a sufficiently low level, but still risky. – Milliways Feb 15 '22 at 21:36
  • Thanks @Milliways - I think I figured out what you meant by a divider when I saw your circuit diagram and the example I used on the resistor calculation page. Makes sense - I was never able to get voltage down to less than 6V using the series method I showed above. – dbmitch Feb 15 '22 at 21:54

2 Answers2

2

The illuminated pushbutton switch may be a DPST switch - as shown in Schematic A below. But even if it's SPST, it doesn't change the fact that the LED must have a current-limiting resistor. The form of the switch (SPST v DPST) doesn't change the interface requirements either. A part number or schematic of the switch would enable us to sort that out, but again, I don't feel it makes a huge difference unless it's a very specialized switch.

The schematic below (Schematic A) shows two options for the interface - Option I & Option II:

1. Option I: Zener Diode

A zener diode limits the voltage at the GPIO pin to 3.3V. It will maintain this 3.3V limit over a wide range of power supply voltages. This reduces the risk of damaging your RPi - and the circuitry behind the GPIO pins is very fragile.

2. Option II: Voltage Divider

A voltage divider will also work - although over a smaller range of input voltages than Option I. You have indicated this is your preferred solution due to parts availability - and that's fine, but you should be clear on the tradeoffs you're making.

Schematic A:

schematic

simulate this circuit – Schematic created using CircuitLab

Voltage Divider Calculations for Option II:

The RPi's GPIO pins operate from a 3.3V supply; damage is almost guaranteed (see warning) if input voltages exceed that level. Consequently, the resistor values needed to set the voltage to be input to the RPi must be determined with some accuracy. This is a critical step; an error in this calculation may destroy your RPi. Fortunately for our RPi hardware, Ohm's Law, and the Wikipedia page on voltage dividers, provide guidance for calculating these resistor values:

Vout = Vin * R3 / (R3 + R4)

Vin = 12 V
Vout = 3.3 V Choose a value for R3, and calculate R4.

IOW, there are numerous combinations of R3, and R4 that will give the needed 3.3V - or close enough to it. For example:

Choose R3 = 10K & solve for R4:

R4 = R3 * 1 / ((Vin / Vout) -1)

R4 = 10,000 * 1 / ((12 / 3.3) - 1)

R4 = 3,793

And so you see that the process is to try to find a combination of resistors in your stock that will give the required value.

Finally on the subject of resistor selection for a voltage divider interface, there is one other important topic to address:

Voltage Thresholds for Logic HI (1) and Logic LO (0)

RPi GPIO are all digital IO, meaning there are only two valid states: High (1) and Low (0). But voltage, resistance and current are clearly creatures from the analog domain. This means that, as part of our voltage divider design, we must consider the voltage thresholds imposed upon us by this binary system.

These voltage thresholds are currently defined in the "Official" documentation. They are summarized as follows:

Binary Logic Level Worst-Case Voltage
Low / LO / 0 Vin ≤ 0.6 Volts
High / HI / 1 Vin ≥ 1.6 Volts

"Worst case" here means that for any (defined) value of VDD IO, the thresholds in the table above are valid. For the case at hand, the designer may use a value for R4 as low as ~1.8K. Since playing at the margins is usually risky business, the following values seem reasonable:

  • R3 = 10K
  • 2.7K ≤ R4 ≤ 3.3K .

3. Option III: Potentiometer

A functional equivalent alternative to the voltage divider is to use a potentiometer with the wiper connected to the GPIO pin - see Schematic B below:

Schematic B:

schematic

simulate this circuit

4. Option IV: External Voltage Clamp

The clamping diodes shown in Schematic C below offer valuable risk reduction if a resistive divider, or a potentiometer, is used. You've indicated a preference for resistors only, but for the sake of completeness I'll include this option. A pair of clamping diodes connected across the GPIO input will provide additional protection. Note that the GPIO circuitry on the RPi already includes a pair of clamping diodes, but as they are elements of the integrated circuit (parasitic FETs most likely), they have a very limited current-carrying capacity.

Schottky diodes are much favored over p-n junction diodes for voltage clamps. An external Schottky diode offers superior performance (and protection in this application) for three reasons:

  1. Schottky diodes can carry more current & thereby reduce the risk of damage to the RPi's GPIO circuitry

  2. Schottky diodes have a significantly smaller forward voltage drop than a p-n diode

  3. Schottky diodes have very low effective capacitance, and as a result are extremely fast - speed matters in device protection.

Schematic C:

enter image description here


Seamus
  • 18,728
  • 2
  • 27
  • 57
1

Thanks to all for your help and suggestions

I managed to wade my way to a solution using examples and a resistance calculator - and with the suggestion from @Milliways on going to the GPIO in between two resistors and grounding the second resistor.

Initial power to LED is 12.31V and using the suggested 3:1 or 4:1 ratio from the voltage drop formula, I used R1=1MΩ and R2=330kΩ

Output to GPIO when LED is on now measure 2.34 V which should be safe for the Pi GPIO and a large enough number that my program can sense the change when LED is switched on by button.

My ugly - working test breadboard layout and resistor connections are shown below. The LED has been pulled from the big button and I just used the small side switch on side of LED to test - same one that button pushes spring down on to.

I will try to post the actual working circuit diagram so as to not scare anyone

breadboard layout and connection to LED button

[resistors2

And here's a pic with me holding down the light with the voltage reading in background. Tricky to do with timed photo and holding leads and button down...

voltage reading

EDIT - FINAL CIRCUIT BOARD LAYOUT

Updated picture of circuit - this looks closer to what mine should look like connected to Raspberry Pi (voltage divider calculator) and a Raspberry Pi Forum example

Example Finished Circuit

EDIT 1 - I had to change the resistors as 2.3V was not producing reliable push button detection. Changed from R1=10k, R2=2.3k to R1=6.8K R2=2.2K to give Vout = 3.0V

EDIT 2: CHANGED RESISTORS Final working circuit on falstad

Final Circuit getting 3.0V to GPIO

dbmitch
  • 381
  • 5
  • 19
  • I guess I'm not very good at translating my breadboard circuit to the actual circuit diagram. It appears to look like what I'm doing with my tangle of wires. Which part do you think is wrong? I just haven't connected the last wire to GPIO - but it shows 2.34V steady. – dbmitch Feb 17 '22 at 18:33
  • This looks like my scenario https://forums.raspberrypi.com/viewtopic.php?t=189743#p1193311 and this appears to show Gnd to 12V circuit as well https://www.allaboutcircuits.com/tools/voltage-divider-calculator/ – dbmitch Feb 19 '22 at 04:11