Pages: [1]   Go Down
Author Topic: Arduino Mega 2560 + Detect Power Outage whit 3V-DC adapter  (Read 2579 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, first off all I want to say that I'am new to Arduino world and I'm loving it and greetings for everyone.
Hi have an Arduino 2560 and a Float switch with 2 wires.
Here is the photo:


So I have a red and a blue wire and I want to know when the switch is activated.
So where do I have to connect this 2 wires and how to read if the float is activated?
I connect 1 wire to ground and other to a digital or analogue pin?
-----
I also want to detect when I have a power outage. So I have a UPS with 4 battery's that can run the arduino with no problem when eletricity is gone. So I thought in this:
- Conect a adapter to the eletricity out of the UPS system. It is a DC-3V - 500mA adapter.
- Conect the plus of the adapter to arduino and read the value and the ground to the ground of arduino.
- When there is power arduino will read some value, and when there is a power outage arduino will read zero.
Picture:


So by doing this I will damage arduino because I have heard of 40mA limitation (the adapter is only 3V but it is 500mA) on the pins but on the other side I read also that some people could detect when there is a power outage by this way.
I need some help here to know what should I do to detect a power outage with this adapter and without destroying Arduino.


Thanks and greetings.
« Last Edit: March 05, 2012, 07:39:08 pm by vrgomes » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 438
Posts: 23725
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I wire to ground, 1 wire to an input pin.
Code:
// declare the pin used
int floatPin = 22;  // or whatever pin you use

void setup(){
// declare the pin as an input
pinMode (floatPin, INPUT);
// connect the internal pullup
digitalWrite (floatPin, HIGH);
}
void loop(){
// now read it, and see if pin is connected to ground
if ( digitalRead (floatPin) == 0 ){
// do the swith closed action
}
else {
// do the switch open action
}
}
« Last Edit: March 04, 2012, 08:48:14 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi crossRoads thank you for the fast answer. It looks simples  smiley-mr-green
I can use a digital Pin or an analog Pin?
For that code you post it looks like I need to conect to the digital pin, but if I could connect to the analog pin it whould be great. Is the code the same?

One last doubt. In this website of 10 Ways to Destroy an Arduino they say:
Quote
Shorting I/O Pins to Ground
HOW
Configure an I/O pin to be an output then set it high. Short the pin to ground. You have now created an overcurrent condition on the I/O pin and it will be destroyed.
http://ruggedcircuits.com/html/ancp01.html

I will also have 4 of these float switch.
Making that tipe of connection conecting one pin to the float switch and then to the ground I'm in danger to damage arduino? (I'm going to have 4 float switches with each one connected to 1 pin, so in total 4 pin an the all connected to only one ground). Should I place a resistor between each pin and the float resistor to restrict the voltage?

Greetings.
« Last Edit: March 05, 2012, 06:53:10 am by vrgomes » Logged

Canada
Offline Offline
God Member
*****
Karma: 7
Posts: 583
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are these just switches or are they some form of variable resistance level sensor?

If they are just switches, there is advantage to putting them on an analog input.

As far as shorting out the Arduino goes, as long as you've defined the pin mode to be an input (as CrossRoads shows) there will be no problem.
Logged

Facts just don't care if you ignore them.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I have tested and work very god xD
It is a simple switch...


What are the advantages of using an analog pin?
And If I use one analog pin can I use the same code that CrossRoads wrote and just change the Pin number?
I'am only asking this because in the code there is "digitalRead" and "digitalWrite" but I read that analog pins can behave just like digital pins.
Or I have to change it to "analogRead" and "analogWrite"?

I also want to detect when I have a power outage. So I have a UPS with 4 battery's that can run the arduino with no problem when eletricity is gone. So I thought in this:
- Conect a adapter to the eletricity out of the UPS system. It is a DC-3V - 500mA adapter.
- Conect the plus of the adapter to arduino and read the value and the ground to the ground of arduino.
- When there is power arduino will read some value, and when there is a power outage arduino will read zero.
Picture:


So by doing this I will damage arduino because I have heard of 40mA limitation (the adapter is only 3V but it is 500mA) on the pins but on the other side I read also that some people could detect when there is a power outage by this way.
I need some help here to know what should I do to detect a power outage with this adapter and without destroying Arduino.

People you have been great so far. Thank you so much  smiley-surprise
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 438
Posts: 23725
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Analog pins act like digital pins when you address them as digital pins, nothing else needed.

To monitor the 3v power supply, connect it to an analog pin just read it
powerSupply = analogRead (A0);
for instance.
The arduino will only draw minimal current, doesn't matter how much the device is capable of supplying.
40mA refers to how much an Output pin can sink (when low) or source (when high).
When its an input pin, the Input current is very, very small.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi CrossRoads thank you very much for the help  smiley-grin
So for what you said It can be like I imagined:


Is it right?
On the code:
Code:
int analogRead = 0;
void setup(){
// declare the pin as an input
pinMode (A0, INPUT);
}
void loop(){
analogRead = analogRead (A0);
}
Do I have to declare the pinMode? I'm asking this because if I want to pass A0 as a variable i don't see how I can do it. In arduino examples:
http://arduino.cc/en/Reference/analogRead

They don't declare the pinMode and call analog pins like digital ones:
Code:
int analogPin = 3;
analogRead(analogPin);

Greetings.
Logged

Canada
Offline Offline
God Member
*****
Karma: 7
Posts: 583
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

...
What are the advantages of using an analog pin?
...

Doh!... Sorry, I meant there would be no advantage.

Glad to see things are working out for you.  You seem to be getting the hang of it quickly.

Since there is no analog output on those particular pins you don't need to define a pin mode.  analogRead() is the only analog function that can be done on those pins and it assumes you are going to read data.  analogWrite() is a Pulse With Modulation (PWM) function that works on different pins and it too assumes a direction (out in this case).  pinMode() is for digital operations and changes the behavior of some of the digital functions.  For example, if you issue a pinMode(x, OUTPUT), then a digitalWrite(x, HIGH) will cause pin x to go to a high voltage level, however, if you issue pinMode(x, INPUT) then a digitalWrite(x, HIGH) will connect the internal pull-up resistor.  Check out the on-line Tutorial under Digital Pins.
« Last Edit: March 06, 2012, 03:40:16 pm by BillO » Logged

Facts just don't care if you ignore them.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, thanks for the explanation.
So my connection of the DC-3V adapter is well made? With the positive of the DC Adapter connected to a Arduino analog pin and the ground connected to Arduino ground?
And then on the code I just have to make analoRead() to the Analog Pin that I connect.

Logged

Canada
Offline Offline
God Member
*****
Karma: 7
Posts: 583
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your connection looks right.  Keep in mind, that that looks like an unregulated adapter.  Measure the voltage of it under the load ti will drive before making the attachment.  Be sure it is under 5v.  You could leave out the pinMode() statement in the setup section.
Logged

Facts just don't care if you ignore them.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 438
Posts: 23725
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, Gnd to Gnd and voltage to A0.

int analogValue;  // declare the size (16-bit int for the 10-bit ADC value)

analogValue = analogRead(A0);  // read the value, store it in 'analogValue'

then can do stuff with it:

if (analogValue >500){
// do something
}
else {
// do something else
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi thank you very much for reply. i didn't tested the DC-3V adaptor because I'am afraid that it has fluctuations in voltage and pumps more then 5v.
How can I protect arduino from energy spikes form de DC-3V adapater? A resistence in the midle or one opto-isolator?
The problem is that I don't know how to use those things. Anyone knows?

Greetings.
Logged

Canada
Offline Offline
God Member
*****
Karma: 7
Posts: 583
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Since you are just trying to detect a power failure and not determine brown-out or surge, I feel the opto-isolator option would be the best.  A reed relay might be another good option.  Both of these would easily provide a simple go/no-go indication of power and give you great transient protection.  You also would not need to employ an analog input either.

There are other methods too, but guaranteeing a spike won't get through would be more difficult.  However, if detecting brown-out and surge are also important, then you might want to investigate those other means.  I would suggest a voltage divider (a 10K pot would do fine) that brings the voltage of the adapter down to about 2.5V, then follow that with a 5.1V zener or transient suppression diode and maybe a couple of 0.1uF caps.  If you wanted a bit more safety, a ferrite bead between the zener (TSD) and C2 might also help.  See the attached drawing.



* Voltage Divider.jpg (8.87 KB, 446x96 - viewed 70 times.)
« Last Edit: March 09, 2012, 08:36:41 am by BillO » Logged

Facts just don't care if you ignore them.

Pages: [1]   Go Up
Jump to: