It will send you a text message when mains power fails and when it switches on again.

This project is inspired by a proposal that was sent to us by one of readers, who has a holiday residence in a remote area where power outages (e.g. during thunderstorms) are quite common. One time he arrived at this place and found out that the climate control was switched off by a power failure. Instead of enjoying a well-deserved time off, he couldn’t do more than cleaning up all the mould that had grown during the absence of ventilation and heating. To make sure that this was the first AND the last time that this kind of incidents ruined his holidays, he designed a remote alert when the power fails, so he can call one of the neighbours to check if everything in his home is okay. Since he already had a remote burglar alarm installed, all that he needed was a circuit that monitored the mains voltage and mimicked a switch on an unused input of the alarm system.
To make this project more versatile, independent of the presence of an alarm system like he has, we decided to design a circuit that contains a cell phone module and sends you a text message when the mains power fails, and a next message will be sent as soon the power is restored. The circuit also monitors the voltage of the backup battery that is needed during the absence of mains power and sends a text message when this voltage is low.
Hardware and software development went hand in hand in the design of the power failure detector. The microcontroller was designed to be proven hardware that could also be used with the Arduino IDE and its libraries. However, the Arduino IDE was not used during development.
The Arduino IDE and its libraries are very beginner-friendly, because they facilitate software development and hide many "Black Magic" from us. This comfort has a price, we can hardly see how the scarce resources in the microcontroller are used. At the end we get a program that requires most of our flash and uses almost all of the RAM without us seeing why.
The software was written in C, here C99, and the AtmelStudio 7 IDE was used. The aim of the development was to obtain the smallest possible firmware for the microcontroller, which provides all basic functions. At the start of the project, originally an AVR with 8kByte Flash and 512 Byte RAM was planned. Later in the project, a proven Atmega328P was chosen which is equipped with more Flash (32kByte) and more RAM (2048 Byte). Therefore, the microcontroller still offers space for later expansions.
The software provides the following functions:
  • Sending a text message when a power outage is detected
  • Sending a text message when the network returns
  • Sending a text message when the backup batteries should be replaced (3.9V)
  • Sending a text message when the backup batteries are empty (3.7V)
  • In the event of a power failure after sending the text message: battery power saving
  • Configuration of the phone number for alerting via text message
  • Storage of the configuration in the EEPROM
On power grid networks there are occasional switching operations, i.e. one line is switched off and another is switched on, and other causes for very short interruptions of the supply voltage and thus the zero crossings are briefly interrupted. To make sure that no false alarm is triggered here, it is checked whether a next zero crossing will occur within 250ms. With this criterion, it is not necessary to distinguish between 50Hz and 60Hz power frequency.
IC2, the trusted ATMEGA328, is the heart and brain our power failure detector. It monitors presence of the mains voltage via optocoupler IC3, checks the voltage of the external battery BT1, and controls and communicates with the M95 GSM modem via its serial interface (with RTS/CTS handshake). It can also determine whether  it’s powered via the mains voltage or the external battery by checking the status output of IC5. An AVRISP (-ish) interface can be connected to K1 to program the microcontroller with the latest version of the firmware.
It also controls LED3..LED5 that reflect the status of the cell phone module. LEDs 3 to 5 are connected to the pins of the microcontroller. The functions are assigned as follows in the firmware:
LED3, red : Indication of battery status
LED4, orange: Indication whether mains frequency detected
LED5, green: status display of the modem
Red LED off: The batteries are okay or power failure
Red LED Flashing:    The batteries are low and should be replaced ASAP
The red LED lights up:          The batteries are empty, it is no longer possible to send a text message in the event of an error
Orange LED off: Power failure
Orange LED blinks:   follows mains frequency (50Hz or 60Hz)
Green LED off: Modem not started or power failure
Green LED flashes 1Hz:        Modem logged in, but no phone number configured          
Green LED flashes 10Hz:      Modem Initialized but not logged into the network
Green LED lights up: Modem registered and ready for operation
The Netlight output of the modem switches LED2  which indicates the state of the modem:
LED2                         State
Off                             not running
64ms on/800ms off    not synchronized with network
64ms on/2000ms off  synchronized with network
64ms on/600ms off    GPRS data transmission
The ATMEGA328 is running on 2.8V to stay well below the maximum voltage (3.3V) for the input pins of the cell phone module. LDO IC4 regulates the 2.8V power supply for the microcontroller.
Mains voltage is connected at K2 and this connection is used both for powering our circuit during normal operation and monitoring the presence of voltage zero-crossings on the grid.
MOD1 is a 110/230VAC to 5VDC converter, which is a compact all-in-one and safe replacement for classic power supply designs, NTC R6 limits its inrush current at power on. The 5VDC output of MOD1 is filtered with common-mode filter L1 and several decoupling capacitors. IC1 is an LDO which regulates it to the 4.4V needed for powering the M95 cell phone modem. But of course: when the mains power fails, an external 4.5V battery BT1 will have to take over until the mains power is restored – or at the very least until a text message is sent to warn you that there is a power outage.
IC5 is called ‘dual 2.6A, 2.5 to 5.5V fast ideal diodes’, a device that switches between two power sources: either the 4.4V output of IC1 or 4.5V battery power. As long as the output voltage of IC5 is higher than 3.5V, the input at pin 2 (with a threshold of +/- 0.5V) of the LTC4413 will keep the batteries switched off.
Optocoupler IC3 conducts every half period of the mains voltage, and the signal on its collector triggers interrupts in the microcontroller while the mains voltage is present. R8 and R9 are connected in series because the peak voltage of our 230V grid is 325V, which is more than the maximum voltage for  most standard resistors. For 110V mains voltage one of these resistors can be replaced by a jumper wire.
The diagram around the cell phone module is largely copied from Quectel's hardware manual. The SIM card connector (SIM1) is protected with R10..R12 and D2, C8..C11 suppress HF interference on the SIM card connections.
The maximum current of the modem can be as high as 1.6A during a transmit burst, for this reason C19 is added as an extra buffer for the power supply of the modem. This capacitor is absolutely necessary for the correct operation of the modem when the circuit is powered by batteries during a power failure, normal alkaline cells can not deal with these short peak currents. Without this capacitor the M95 would automatically switch off when its supply voltage drops below 3.3V.
Configuring the system
To start up the software, a SIM card with a deactivated PIN must be used. If the SIM secured with a PIN, the software cannot start. The easiest way to deactivate the PIN –if necessary- is to put your SIM card in a cell phone and switch the PIN off in the security settings menu of the phone.
The module tries to connect to the cell phone network if the mains voltage is OK or the battery level is not below a minimum voltage. It is therefore recommended to supply the board with mains voltage for the first commissioning. If no mains voltage is detected and the board is powered from the batteries, the LEDs are no longer activated to save power, which makes the configuration more difficult.
If the login to the cell phone network is successful, LED5 lights up. From this point on it is possible to call the SIM's phone number to test that the SIM has correctly logged into the network and is not blocked by the network.
However, we cannot yet receive an alert from the system. No valid destination number is entered in the EEPROM of the microcontroller as delivered. This prevents the system from unintentionally sending text message messages to an unknown number. This state is indicated by the green LED which should flash every second.
For setting the destination number, it was decided to configure via text message. To do this, a text message with a specific text must be sent to the phone number of the SIM card used. For this purpose, the text "Config Remote " is used and is the phone number of the device that is to receive the text messages. Only "+,1,2,3,4,5,6,7,8,9" are valid characters for the phone number. If the text message has been received and successfully processed, an text message with the content "Stored new Line ID in EEPROM" is sent back to the newly stored phone number. The system is now ready for use.  The green LED stops blinking after this text message has been sent successfully.
In addition, the status of the system can be queried remotely via text message. To do this, a text message with content "Request Status" must be sent to the phone number of the SIM card used. The system then returns a text message with content "Mains frequency is with Hz. Battery has mV and is " if a destination number was successfully stored.
To reset the configuration of the system, jumper JP1 must be set on the hardware itself during startup. To do this, the system must be disconnected from the power supply, i.e. disconnected and the batteries removed. Then the jumper JP1 must be closed and the system must be plugged in again. After this restart the green LED should flash. The jumper must be removed to prevent the system from being reset to the factory settings at the next start. For safety reasons (mains voltage on the PCB!), this should only be done with mains power is disconnected. The system can now be reconfigured as described.
Backup batteries and power savings
Since the circuit uses chemical cells (batteries) as backup power during mains failure, it is important to reduce energy consumption as much as possible. If we operate the microcontroller with 2.8V and 8MHz through an external resonator it needs 4mA. Thus the microcontroller would discharge 1500mAh batteries in 1500 mAh/4mA=375 hours, i.e. 15.625 days. Most important, we have the modem as a large-scale user in the system, it requires 300mA peak for this application (sending text messages). We also see this value when the modem logs into the network or tries to log in. This results in a current consumption from the chemical cells of 304mA for our application, and backup batteries would be exhausted after 4.9 hours.
Since we do not want to replace the batteries after every power failure, it is important to keep power consumption low. As a first step, we can reduce the power consumption of the processor by switching off parts that are not needed. For this purpose the Power Reduction Register (PRR) is used for the ATMEGA328P. This can save about 10% (0.4mA) of the processor power.  Furthermore the system is set to IDLE mode, in this mode the processor core is stopped until an interrupt occurs. With the current version of the firmware, about 50% energy savings are possible, and a current consumption of 2mA can be achieved while the software is still active in the microcontroller.
If a power failure occurs, the modem remains the largest consumer. With about 300mA (worst case), the modem would discharge the batteries in just a few hours’ time. To prevent this from happening, the modem is completely switched off after the power failure text message has been successfully sent. This means that the modem consumes only a few µA and the lifespan of the cells is significantly extended.
The last point is to disable the status LEDs when the system is powered by batteries, these LEDs need 2mA to 3mA each. Since the power failure detector is usually used in a place where you are not in case of a fault, it is not very useful to try to signal something with the LEDs in case of a power failure.

BOM 160372-1

R1 = 10 kΩ, 1%, 0.1W, 50V
R2 = 110 kΩ, 1%, 0.1W, 75V
R3,R19 = 470 Ω, 1%, 0.1W, 50V
R4 = 43kΩ, 1%, 0.1W, 50V
R5,R13,R20,R21 = 1 kΩ, 1%, 0.1W, 50V
R6 = NTC 80R B57236S0800M000  Epcos
R7 = 4k7, 1%, 0.1W, 50V
R8,R9 = 47 kΩ, 5%, 0.6W, V 250V, axial leaded
R10,R11,R12 = 22 Ω, 1%, 0.1W, 75V
R14,R15 = 100k, 1%, 0.1W, 50V
R16 = 470K, 1%, 0.1W, 50V
R17 = 1M, 1%, 0.1W, 50V
L1 = common mode filter ACM4520-231-2P-T
L2 = BLM21AJ601SN1D, 600R@100MHz, size 0805
C1,C2,C4,C5,C7,C13,C16,C17,C18,C22 = 100 nF, 50 V, X7R, 0603
C3,C19 = 1000uF, 10V, radial can SMD
C6,C12 = 100 µF, 16 V, 2312
C8,C9,C10,C11,C15 = 33pF, 50 V, C0G/NP0, 0603
C14 = 10pF, 50 V, C0G/NP0, 0603
C20,C21 = 1U, 10V, X5R
D1 = 1N4007, 1000 V, 1 A
D2 = protection diode SMF05CT2G
LED1 = LED, yellow 60 mcd, 2 V @ 20 mA
LED2 = LED, green, 50 mcd, 2.1 V @ 20 mA
LED3 = LED, red, 15 mcd, 2.1 V @ 20 mA
LED4 = LED, orange, 200 mcd, 2 V @ 20 mA
LED5 = LED, green, 50 mcd, 2.1 V @ 20 mA
T1 = 2N7002 N-channel SOT-23
IC1 = LDO, adjustable MIC29302WU TR
IC2 = 8-bit Microcontroller ATmega328P-AU EPS160372-41
IC3 = CNY65 optocoupler 
IC4 = LDO 2.8V MCP1700T-2802E/TT 
IC5 = LTC4413 Ideal diode controller
MOD2 = M95 GSM Module (Quectel)
X1 = 8MHz ceramic resonator Murata CSTCC8M00G53-R0
SIM1 = Micro-SIM socket Molex 78723-1001
ANT1 = Antenna SMA, angled helical
ANT1 = SMA Connector
JP1 = Jumper, 1x2, vertical
n/a = Jumper, 2 way, 2.54 mm
F1 = Fuseholder 5 x 20mm Fuse, TH
F1 = Fuse holder cover MC000833
F1 = fuse 315mAT, 5 x 20mm (230VAC mains)
F1 = fuse 620mAT, 5 x 20mm (110VAC mains)
K1 = Pin header 2 x 3 ways pitch 100mil
K2 = Terminal block 7.68 mm, 2-way, 630 V
BT1 = battery holder 3 x AA 
PCB 160372-1 V2.1