# How can I use a GPIO input pin for a 12v circuit?

I want to know with the raspberry pi if the button is clicked.
I want ot use the button of the GPIOZERO python library.
The power plug is a 12v power plug.
Can this circuit work?
(I'm a beginner.)
Thanks

• You need a voltage divider. – Ole Wolf Sep 30 '20 at 14:16
• Can I use resistor instead ? I add it on the sheme. – PacCol Sep 30 '20 at 14:39
• PacCol, not to be rude, but did you investigate what voltage divider means? It involves a resistor (or two, actually). – goldilocks Sep 30 '20 at 14:54
• Yes I looked on Wikipedia but I don't see how to use it... I will do further research. – PacCol Sep 30 '20 at 15:07
• Unless you have a common Gnd you DO NOT have a circuit so it will probably do nothing. If it did you would have destroyed the Pi. You have been warned. – Milliways Sep 30 '20 at 22:48

When the button is pressed the red side of the button will be at 0V. But when the button is released it will go to 12V and fry your RPi. Also 0V for the power plug can differ from 0V for the RPi. If they differ too much that fries the RPI as well.

So first thing you have to fix is to connect the GND of the power plug and RPi so they both agree what 0V is.

Next you need a voltage divider (just 2 large resistors in series) going from the red side of the button to GND. Picking the right resistors you would get a 3.3V signal in between the two resistors and you can connect that to a GPIO pin. But that would also create a leak across the button. A small current would flow even with the button off. So not really the best solution.

A better solution would be to add an octocoupler in series with the button. Adjust the resistor and/or diode in the circuit to offset the voltage loss through the octocoupler. The other side of the octocoupler is then connected to GND and a GPIO pin and the GPIO pin is set to input with pullup.

• Like this ? Circuit --> inventions.ovh/circuit.jpg – PacCol Sep 30 '20 at 16:01
• Except for all the resistors being the same that looks right. But I really recommend using an occtocoupler. – Goswin von Brederlow Sep 30 '20 at 16:08
• How can I choose the resistors ? – PacCol Sep 30 '20 at 16:14
• The ratio between the two resistors determines the voltage in the middle. And to reduce the current flow you want large resistors. To get 3V you need a 1:3 ratio, e.g. 100kOhm + 300kOhm. Doesn't have to be exact. 100kOhm + 260kOhm for example will give you 3.3V. The formula is `12 / (X1 + X2) * X1` and anything between 3V and 3.3V would be perfect. MEASURE the voltage before you connect the Raspberry Pi. If you swap the two resistors you will get 9V. If you picked the wrong ones you might get anything between 0V and 12V. – Goswin von Brederlow Sep 30 '20 at 16:25
• Thanks for your help ! – PacCol Sep 30 '20 at 16:28