I want to detect 240Vac mains using the GPIO. Here is my plan (with questions below)...

  1. Convert the 240Vac down to 5Vdc via a USB charger. This should give me Mains Power safely to both me and my home by just plugging in a standard cheap USB charger (5v DC, up to 1A output). Cut the USB cable and expose the wires, now I have 5v and GND lines.

  2. Make a voltage divider to drop from 5v to 3.3v and into the GPIO pin. Something like this

Question 1: Should I put anything now in series (such as a resister and/or something else) to prevent too many Amps entering the GPIO. The USB outlet could supply up to 1A which is about 2000 times what the GPIO would tolerate I think. However I'm not sure that the RPi would draw that current? Excuse the electrical ignorance but could the plug "drive" a high current without me asking for it... Like a current source or something?

Question 2: Can anyone tell me if my plan is OK and if I could do it better, safer (for me or the RPi) or easier?

4 Answers 4


It looks perfectly sensible to me.

You might as well include a 1k series resistor before the GPIO you plan to use. That would limit the current to a safe 3.3 mA if the GPIO is set as a low output by mistake.

  • The voltage divider already includes a 10k resistor in series with +5V, limiting the current to a safe 0.5mA in case of a misconfiguration. Commented Dec 21, 2017 at 13:15

Working with 240VAC is a risk, so pay attention to carefully isolate all 240V wires before giving voltage!

A possible solution, safe for the raspberry inputs, is to add an optocoupler enter image description here

  1. connect the 240VAC to a capacitance of 100nF/630v in series with a safety resistor of 3900ohm 1/4W. The capacitance has an equivalent resistance of 33k and dissipate very little power. The Resistor is needed to limit the peak current during switch on.
  2. use a common Photocoupler like the pc817
  3. Use internal pullup configuration to save the collector pullup resistor (!)

enter image description here


The voltage divider will act as a series resistance, so there's no need for anything else.

Note that USB chargers can keep the output at 5V for quite some time (several seconds) after the power is lost. If you need to react to a power loss quickly (e.g. switch on alternative power), this may not work as you expect. If this is the case, a small 240V LED or neon light + a photo-resistor may be a more suitable solution for mains power sensing:

enter image description here


A 1k input resistor would limit current to 240v / 11000 ohms = 21ma this http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/raspberry-pi/gpio-pin-electrical-specifications says it would be best to limit current to 16ma. If you want to observe that limit the 1K resistor should instead be 240/.016 = 15k total or a 5K resistor if I am remembering Ohm's law correctly.

  • 2
    This answer seems to suggest that the OP connects his RPi directly to mains voltage, which is pretty suicidal for both the OP and the RPi. Commented Dec 21, 2017 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.