loading
Picture of Save your home with Arduino
IMG_0724.JPG
130719_0000.jpg
Just recently, there was a cold front that messed with power all over. There was a tornado watch in Canada, even. There were some really cool clouds, then some thunder, then a whole lot of lightning, thunder, and rain. Lots of rain and constant rumbling as the sky opened its stored water and charge. Just after a brown-out knocked out the TV and internet (it tripped a GFCI that I didn't see until the day after) I saw a leak in my basement.

It was no small dribble. It was a veritable deluge. (Click the video below to see the deluge.) That may be a bit of an exaggeration, but there was water coming into the house and unsteady power. I tried patching the leak with hydraulic cement, but it couldn't stop the flow because as the water stopped, it pooled up outside and built up pressure. (Instructable for basement repair pending?)

With a partially blocked flow running into the sump, I needed to know if the power died in the middle of the night because if the sump pump stopped, I'd need to empty it with buckets. The pipe into the sump was dumping a lot of water in addition to the leak. No fun, but better than flooding. Carefully directing the little river through the basement to the sump and jamming a bit more hydraulic cement in the crack, I went upstairs.

With the flow slowly breaking the patch I made, I quickly programmed an Arduino to beep when the power went out. It took like 10 minutes because I had to call my friend for the analogRead values. It's 0 to 1023 for voltages 0 to 5V. I know, I know, I could have found it in the examples, but I was without internet and I was desperate and not thinking clearly. I should probably get outside sometimes …

The Arduino never actually woke me in the middle of the night. The power stayed, but it was a lot of peace of mind.

And then the next day I found that the internet box where the fiber-optic comes in was plugged into a tripped GFCI. Oh, the embarrassment. I promptly got on the internet and made an instructable after checking my webcomics.

I didn't end up running wires down two flights of stairs to trigger an alarm when hypothetical flood water shorts some wires. I wanted to, but I just let the sump pump deal with it because it was late.

(I'm entering this in the Arduino contest because, wow, this was the most useful thing I did with it. Vote for me or whomever you think is cool. So me?)
(Too bad the battery-powered contest just ended. This would be pretty good for that, too.)
IMG_0722.MOV(402x714) 7 KB
 
Remove these adsRemove these ads by Signing Up

Step 1: Materials

1 Arduino (Uno, Mega, Duemillanove, Due (I'm so jealous of you), whatever you have. Even an ATmega or ATtiny because it can save your house.)
1 breadboard
1 wall-brick for around 9V DC and a corresponding jack to connect it to a breadboard.
1 3.3V voltage regulator (Alternative below)
Battery holders for 6-20V. I used 4 AA. 9V works well, too.
1 speaker or buzzer
1 capacitor—pretty big capacitance, preferably. I had a 22µF and it was okay. More on this later.

If you don't have a 3.3V regulator, you can make a voltage divider with 2 resistors. Easy math and everyone has resistors, right? http://en.wikipedia.org/wiki/Voltage_divider

Step 2: The Circuit

Picture of The Circuit
Build this circuit with this code on the Arduino:

/*
Power Outage Detector
By Chris Chungbin
*/

int in=0;

void setup()  {
}

void loop()  {
  in = analogRead(A0);
  if (in<400)  {
    while (1)  {
      tone(8, 440);
      delay(500);
      noTone(8);
      delay(500);
    }
  }
}

Things to note:
1) The 3.3V regulator isn't actually connected to the power of the Arduino; the batteries power the Arduino alone.

2) The grounds of everything are connected. That includes the battery negative, the regulator ground, the Arduino ground, and a speaker lead.

3) The capacitor. I can never decide which side of the voltage regulator to put it, but it works here. Note polarity.

4) The batteries are on Vin. This is automatically regulated to 5V in the Arduino, though it may drop if the voltage is under 6 or 7 volts. I'm cutting it close, here.

5) I didn't comment on anything in the code. This is me coding fast to make something fast. It's nice and simple, though.

So here's the idea: The battery-powered Arduino sits there analogRead-ing pin A0. The Arduino is powered solely by the batteries so it can still do things after mains power dies. The wall-brick power gets regulated to 3.3V so it's less than the Arduino's power (to prevent frying pins (which is actually kind of hard to do)). If the regulated power drops below around 2V, it means mains power just died. Bad stuff. Once bad stuff happens, the code jumps into a while (1) loop (FOREVER!) and beeps on the speaker on pin 8, hopefully waking you and saving your house from flooding or whatever happens when the power dies.

Step 3: Implementation

Picture of Implementation
Okay, you've built it and plugged the battery and wall power in (preferably in that order). I recommend using a surge protector to avoid frying things no matter how unlikely that is. It probably doesn't even matter.

If you plugged in the battery power first like you should, the Arduino will keep beeping (even if you plug in the wall power). Once the wall power is in, reset the Arduino and it will stop. Pull the plug and it should beep at you. If not, are your batteries good? The right pins?


An interesting note: while I had this running, there was a really short brown-out/black out. Just a slight flicker. I was in the basement when it happened, but when I got upstairs, one clock lost time. One. The Arduino didn't detect it, but I know why. The stove clock in the kitchen and alarm clock in my room must have rather large power supply capacitors and low current draw. The stove is gas with an electric oven, so the power consumption could be really high, but it wasn't on; just the clock part. The short outage didn't let the capacitor discharge enough to matter. The microwave must have a smaller capacitor on it's control circuit which dropped below the controller's required voltage long enough, maybe because it drew some more current for the capacitor size as well.

Why didn't the Arduino see it? Two things: the wall brick capacitor (probably crazy big), the capacitor on the breadboard (not all that big, actually). The 9V power supply probably wobbled a little bit, and the 3.3V regulated power probably didn't even notice it. It's cool when science works.


The power never died completely, fortunately. Keep this in mind next time you need to watch your power. Vote in the Arduino contest. And keep making.

you would need a voltage regulator that outputs only 40ma, correct? Can you point me to the one you used? Thanks.

Abnormal Force (author)  mcollins45511 month ago

The current of the voltage regulator shouldn't matter. When you set the pin to input mode it gets switched to a high resistance inside the chip (I think it's like 1MOhm or something absurdly high, but don't quote me). Since current = voltage/resistance, the high resistance keeps the current low.

If you want to avoid frying the pin if it somehow gets set to ground, you can put a 220Ohm (or thereabouts) resistor between the regulator and the Arduino and the current will stay at 15mA.

http://www.adafruit.com/products/2165 or https://www.sparkfun.com/products/526 or pretty much any 3.3V regulator will do.

ahh. Thanks for explaining. Couldn't you also just use a 5v regulator since the pin accepts up to 5v? Thanks again.

Abnormal Force (author)  mcollins45511 month ago

I think I might have found 3.3V first in the big pile of electrical bits I have. 5V should also work.

rt11guru1 year ago
Besides having an alarm, you can also use it to test which outlets are on which circuit breaker. Plug it into an outlet, flip breakers until it yells and you've got your breaker matched to the outlet.
Abnormal Force (author)  rt11guru1 year ago
Ooh, I hadn't even thought of that. The speaker was pretty loud so you might hear it from across the house, too.