Power outage sensor solution

hi there
Im a newbie, and started with arduino not long ago. I know a little electronics, and quiet a lot of programming.
I have summer house, and want a arduino that sense if there is a power outage.
I wanted a simple, cheap solution so I read somewhere that you could use a 5 volt adapter on a digital input pin. I have tested it, and it seems to work, altough it takes about 2 seconds for the 5 volt adapter (Nokia charger) to discharge efter the power out. But thats not a problem.

I want to run my arduino directly from a 12v lead acid battery, 75 Ah , which is constantly charged.
The other "stuff" is connected to a 240v inverter.

this is how its wired

My question is really if you guys think this is a good solution? Can I expect problems, partly from directly connecting a 75Ah lead acid battery and partly if the shared ground would cause me any problem?

the code is simply

/*
sense when no power from external power source (240 VAC -> 5 VDC) and then blink a warning led. 
 */


const int  digPin = 8;
int ledPin = 13;      // select the pin for the LED
int buttonState = 0;


void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
  //declare the digital pin INPUT
  pinMode(digPin, INPUT);
}

void loop() {
  // read the value from the sensor: 
  buttonState = digitalRead(digPin);  
  
  // turn the ledPin OFF if no externa power exists
  if (buttonState == 1)
    digitalWrite(ledPin, LOW);  
  else {
    blink();
    //Serial.print(buttonState);
    //Serial.print("\n");
  }
  
  // stop the program for for 1000 milliseconds:
  delay(1000);                  
}

void blink() {
    if (digitalRead(ledPin) == HIGH)
      digitalWrite(ledPin, LOW);
    else
      digitalWrite(ledPin, HIGH);
}

thanks
Torbjörn

Your proposal sounds reasonable to me.

Another method would be to use a simple 120ac coil relay. Wire the relay coil to your 120vac power and then wire the contact common terminal to arduino ground and the normally open contact to a input pin. Enable the internal pull-up for the input pin. Then when the AC power is removed the pin will read as a HIGH and if AC power is avalible the pin will read as a LOW.

Lefty

You're going to need a resistor on that red LED.

You're going to need a resistor on that red LED.

How did I miss that, me being the resistor Czar and all. XD

Yes, please add a series resistor to your red led immediately, 200-500 ohms. As it is your poor output pin 13 is screaming in agony anytime you turn it on and you will soon lose all functionality of that pin.

Lefty

I have done exactly what you are proposing with a 5v power supply and it worked well. However, what I noticed is that a wall-wart type power supply is not terribly well regulated and the voltage supplied was often quite a bit over 5V. I worried about this dirty power somehow causing problems with my Arduino.

So I inserted an opto-isolator. I went with the SFH620AA opto-isolator (I bought a pack on Ebay for 16 cents a piece). In theory the SFH620AA can be hooked directly to line voltage (in the US at least) but for safety I hooked it to the output of the 5V supply. That has run for two years without problems.

The 2-second delay you experienced comes from the capacitors in the power supply draining down. If you put a 1K resistor across the power supply that will go away.

Running directly from a lead acid shouldn't be a problem. The only issue is if you accidentally create a short the battery will supply enough current to melt whatever is in the way. You might want to consider a fuse.

Please correct me if I'm wrong, but from what I understand, depending on the board, pin 13 already has a resistor. They put it there so you would only need an LED to begin using the Arduino.

baldeagle072:

[quote author=Coding Badly link=topic=50478.msg359878#msg359878 date=1296415137]
You're going to need a resistor on that red LED.

Please correct me if I'm wrong, but from what I understand, depending on the board, pin 13 already has a resistor. They put it there so you would only need an LED to begin using the Arduino.
[/quote]

I believe the resistor is only for the onboard LED

I believe the resistor is only for the onboard LED

That is correct. The pin 13 connector pin wires directly to the output pin on the controller chip. The on-board led and it's series resistor offers no protection from drawing too much current via the shield pin.

What's confusing the issue is that the older Arduino boards (NG and older) from several years ago did have a series resistor wired between the controller chip and the shield pin 13 connection and several of the example pictures on the Arduino site show plugging a led directly into the pin 13 and adjacent ground pin on the shield connector.

Please correct me if I'm wrong, but from what I understand, depending on the board, pin 13 already has a resistor

Older boards had a series resistor. Newer boards don't.

If you include the resistor but don't need, no harm is done. The LED may not be as bright as you expect but nothing is damaged.

If you do not include the resistor but need it, the pin and possible the processor will be damaged.

When in doubt, include a series resistor.

DCContrarian:
I have done exactly what you are proposing with a 5v power supply and it worked well. However, what I noticed is that a wall-wart type power supply is not terribly well regulated and the voltage supplied was often quite a bit over 5V. I worried about this dirty power somehow causing problems with my Arduino.

So I inserted an opto-isolator. I went with the SFH620AA opto-isolator (I bought a pack on Ebay for 16 cents a piece). In theory the SFH620AA can be hooked directly to line voltage (in the US at least) but for safety I hooked it to the output of the 5V supply. That has run for two years without problems.

Interesting! never heard of opto-isolators and going read up about it. I noticed ~6.5v from one of my Nokia charger, and the other 5V adapter was over 8V !
But what I understand here is that the opto-isolator doesnt regulate the voltage, but the "noisiness" that might do somthing nasty to my arduino board?
Should it always be 5V on a digital port? what is maximum voltage?

torb:
Interesting! never heard of opto-isolators and going read up about it. I noticed ~6.5v from one of my Nokia charger, and the other 5V adapter was over 8V !
But what I understand here is that the opto-isolator doesnt regulate the voltage, but the "noisiness" that might do somthing nasty to my arduino board?
Should it always be 5V on a digital port? what is maximum voltage?

The opto-isolator acts like a switch. You hook it to the 5V pin of the arduino and when it is switched on it connects that to the digital input. The voltage can never go above the supply voltage.