I want to make my Raspberry Pi to detect AC power outage and send E-mail/SMS accordingly.

The Pi I have is model B not the B+.

Now I have read other posts but my request is different.

The idea is that PI will be like network monitor device it will do all sorts of things but what I want to start with to get a grip on developer boards is to detect power outage.

The idea is that Rapsbery PI will have external power battery for sustained working time like this one https://www.adafruit.com/product/1566, so I need to figure out how to use GPIO pins to check AC outlet power outage. In my mind this should be trivial, somekind of device that drops power from 220V to 3.3V or 5V so that I can connect it to PI GPIO and ground and check whether there is incoming power, if not the power is down -> send SMS.

Now I have a bit of knowledge in programming but I have 0 knowledge in devboards, wires and soldering.

So if anyone would be so kind do direct me in needed direction as to what I need to do to make this happen I would be really grateful.

Another side question is what programming framework are you using? Im used to work in Visual Studio .net but prob. I will need some other kind of framework for PI applications?

10 Answers 10


I am always astounded by questioners who state things like "should be trivial" and then display little understanding.

My main concern is your implication that you are planning to directly monitor mains voltage. DO NOT even attempt to do this as it is potentially dangerous.

It would not be difficult to power the Pi from a battery and monitor the mains charger.

The device you mention is not the best for this purpose. I suggest you look at UBEC devices (available at low cost on eBay) and couple this with a suitable battery and charger. If the charger does not supply a suitable power indicator output (many do) it is relatively simple to add this. Note you will need to include suitable protection to prevent damage to the Pi GPIO input. Examining the B+ circuitry should give some idea how of one possible solution.

  • Please do not post answers stating what is possible and what is not. I can't imagine that creating small device that converts 220V to 3.3V which i can then connect to GPIO and check whether there is current going in or not is exceptionally hard... Nov 3, 2014 at 11:40
  • 1
    @user3360710 There are such things as 220 V AC to 5 V DC converters. You could also combine a 220 V - 12 V AC transformer with a 12 V - 3.3. V DC converter.
    – goldilocks
    Nov 3, 2014 at 14:59
  • Did I miss something? Did the OP say he wanted to power the Pi when the power went out... or monitor if the power went out with the Pi, regardless how it is powered...?
    – Piotr Kula
    Apr 22, 2015 at 21:14

Here is a simple one...

Why not set up two pi's, one on the circuit that has power and have it send a heartbeat, then another that is on a UPS circuit, when the heartbeat stops, the power is gone...


Milliways is right - if you have "0 knowledge in devboards, wires and soldering", you should stay far away from mains power. He means well.

I noticed that you're intending to do power pass-through via the USB battery pack from Adafruit - how about monitoring the charging input (using a 3-way cable or splice) from your USB 5V power supply to the battery pack?

If there is an AC outage, the power supply will be off, and you should see the 5V become 0V (or thereabouts) there.

You should probably bring the 5V down to 3.3V to protect the GPIO pins.


Take a crummy old 5V phone charger or something. Wire it to the input side an optocoupler with an appropriate resistor. Connect the output side of the optocoupler to a pair of GPIO pins (I'm more familiar with ESP8266 than Pi). Plug the 5V charger into the mains power. If the mains goes out, the charger will no longer activate the optocoupler and the output side of the optocoupler will go from closed to open.


Check out the PiUPS at http://www.piups.net/. It connects to the PI on the GPIO header and supplies power to the Pi over the GPIO bus. You connect you 5V power to the PiUPS and it supplies 5 V either from the main power or battery backup with the main power goes out.

They provide the source code to their CPP driver and say this in an FAQ:

Can I react to the signal when voltage has dropped or battery voltage is low?

Yes, indeed! The Pi UPS software package contains a tool that can be installed on the Raspberry Pi reading the operating status of the Pi UPS via the GPIO port.

enter image description here enter image description here

  • This would be excelent solution but I need other GPIO pins for other sensors as well, like 3G, Temp, Humidity, display etc. Nov 5, 2014 at 6:27
  • The PiUPS passes through all the GPIO pins. It must use one or two to communicate with the Pi. Nov 5, 2014 at 12:57

I agree with all other answers, MAINS IS DEADLY!

With that warning aside, a viable solution would be a opto coupler/isolator.

A opto coupler separate the mains from the more sensitive GPIO pins on the Raspberry Pi. It contains a LED and a photo sensitive transistor. Between the halves is there no electric connection, just photons (light).

An example on the schematic can you find in the reference. OBS the value of R85 could be another for your AC and is depending on the opto couplers LED's forward voltage, it need to be calculated

Ref.: https://electronics.stackexchange.com/questions/50782/ac-detection-for-microcontroller


if all you wish to do is monitor if mains power is there or not, the simplest way is to connect a mains voltage relay to the mains, and monitor open closed status of one of the relay poles.


Use an APC UPS. Some models come with a USB cable to alert when the power is out, and would serve as your backup battery. Install apcupsd and configure notifications on the Pi.


This can be accomplished by using an old router or switch plugged into raspberry pi, powering the pi with a USB battery plugged into AC, and monitoring the network interface with a node app.
A turn key solution: http://raspi-ups.appspot.com/en/index.jsp

  • Please be aware that some USB power banks don't support pass-through, so while charging the USB power bank cant supply power on the out ports.
    – MatsK
    Sep 5, 2017 at 15:05

Have a look at this IC used for AC power monitoring. https://www.mouser.com/new/maxim-integrated/maxim-max44298-monitor/

  • 1
    Hello and welcome to the site. You might find it useful to take the Tour and find out how things work around here. Link only answers should be avoided and you should include any relevant information from the link in your answer. Also product recommendations should be avoided unless you disclose your affiliation
    – Darth Vader
    Dec 13, 2017 at 17:46

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.