I have a pretty basic question here, but I'd like to verify this just in case (don't feel like frying my Arduino).
I'm using one of the Arduino Uno's analog pins to read out a simple push button, the source is an AC to DC converter which converts 220V AC net current (EU standard) to 5V DC. Power then flows through a 50 ohm resistor and passes through the button, to end up at the Arduino analog pin for a HIGH or LOW readout. (There are a lot of other components to the circuit and it was beneficial to have the button integrated in the soldering board due to space constraints, which is why it's not powered by the Arduino itself).
My question here is whether the current going into the analog pin is too high? (According to Ohm's Law it'd be 0.1A?) Should I ditch the 50 ohm resistor and just hook up the source through the button directly to the analog pin?
An input pin is a load.... a very small one. It will take only what it needs from a "source". Providing more current is not an issue to worry your self over.
You only need to read 5V or GND? Use a digital pin. If you don't need the values in between... no reason to go for the full range of analog values when 1 or 0 (High or Low) is enough.
What you need to worry about is if the voltage EVER goes above 5.5V. If that could happen, you need to take precautions to protect the pin.
Power then flows through a 50 ohm resistor and passes through the button, to end up at the Arduino analog pin for a HIGH or LOW readout...
...My question here is whether the current going into the analog pin is too high? (According to Ohm's Law it'd be 0.1A?)
That would be true if one side of the resistor is connected to 5V and the other side of the resistor was connected to ground.
But if I understand what you are saying, the resistor is in series with the Arduino input. The resistance of an Arduino input pin is somewhere around 100 Megohms, and in series resistances sum.
For practical purposes, you can usually approximate the resistance as infinite and you can assume zero current flows into the Arduino.*
Should I ditch the 50 ohm resistor and just hook up the source through the button directly to the analog pin?
Yes. 50 Ohms added to 100 million Ohms isn't doing anything. But, I'd actually like to see a schematic just in case I'm not completely understanding your circuit...
...and passes through the button, to end up at the Arduino analog pin
You've got another issue here... When the button is off/open there is no connection to the input. The input is "floating", and with that high impedance (resistance) it can float up or down and give you "false" readings. You can fix that by adding a pull-down resistor between the Arduino input and ground to insure that the input is low when the connection to 5V is broken by the button. A resistor somewhere in the ballpark of 10K Ohms is good for that.
There's nothing wrong with using a pull-down resistor, but it's "more traditional" to use a pull-up resistor and wire the switch so that it pulls the input down when the switch is turned-on (closed). That would require reversing the logic in your sketch (and there may be other reasons this won't work in your application). There are actually pull-up resistors built-into the ATmega chip that you can [u]enable[/u] so that you don't have to add any external resistors.
The built-in internal pull-ups might not work on analog inputs... I'm not sure.
That's only true if you stay within the specs. If you go over 5V, or if you go negative, lots of current can flow and you can fry your Arduino.
The resistance of an Arduino input pin is somewhere around 100 Megohms, and in series resistances sum.
1uA Max input current, so more like 1 Megohm.
Internal pullups work on analog pins too.
Measuring 0 or 5V, any digital pin can do that too. Can give yourself some protection and have the 5V drive the base of a simple NPN, emitter to Gnd, when turned on the collector will bring an internally pulled up pin low. No button needed then, no worrying about the 5V being too high, just look for the pin being low.
With UNO board by default (at Reset) ALL inputs are Digital, absolutely all.
As the number of pin is limited (it is not possible to have 200 pins by IC package) all pins have a main function (digital) and one or many "alternatesfunction".
The alternates functions can be activated by programing.
Main "alternate functions":
UART
SPI
I2C
Analog input
PWM / CTC mode
So you can use A0 to A5 either in digital or in analog mode.
The switch between digital and analog mode is done automaticaly inside the analogRead() function.
68tjs:
So you can use A0 to A5 either in digital or in analog mode.
The switch between digital and analog mode is done automaticaly inside the analogRead() function.
This isn't actually true, there is no "mode" involved, the pins can be used as digital pins and
can have their analog voltages measured at one and the same time.
This sketch:
void setup()
{
Serial.begin (115200) ;
for (int i = 0 ; i < 2 ; i++)
{
pinMode (A0, INPUT) ;
Serial.print (analogRead (A0) * 5.0 / 1024) ; Serial.println ("V when an input") ;
pinMode (A0, INPUT_PULLUP) ;
delay (300) ;
Serial.print (analogRead (A0) * 5.0 / 1024) ; Serial.println ("V when an pulled up input") ;
pinMode (A0, OUTPUT) ;
delay (300) ;
Serial.print (analogRead (A0) * 5.0 / 1024) ; Serial.println ("V when an output") ;
Serial.println ("add external pulldown >") ; // prompt to continue
while (Serial.available () == 0) {}
}
}
void loop () {}
Generates this sort of output:
3.13V when an input
4.98V when an pulled up input
5.00V when an output
add external pulldown > <<<<< here I plugged an external 390 ohm pull-down resistor in.
0.00V when an input
0.12V when an pulled up input
4.73V when an output
add external pulldown >
Note that the pin voltage is 5.0 when an output driving nothing, and 4.73V when
driving the 390 ohm resistor - you can monitor the output load this way on an
ostensibly digital output if you desire.
[ note this is on ATmega processors, not the Due which is entirely different ]
Thank you all for the quick replies! I've taken your advice into account (especially DVDdoug's), and rewired the circuit as in attachment. There's a lot more to this circuit than this button, of course, but I decided to leave that out as all other components are isolated from this button, to preserve simplicity.
To reiterate then; I hook up the 5V source -> 50ohm resistor (already soldered, so I can leave it?) -> digital pin 13. Then enable the pull-up resistor in pin 13 and invert the logic in my code?
An Arduino Uno has an LED connected to digital pin 13. Use another pin.
As you are using an internal pull-up resistance, your push button needs to go between input pin and ground. When the push button is open-circuit, the pull-up resistance will make the input go to about +5 volts so the digital reading would be high. When the push button is pressed, it will short the input to ground so the digital reading would be low.
Archibald:
An Arduino Uno has an LED connected to digital pin 13. Use another pin.
Have a look on the schematic : LED is now isolated by an operational amplifier, so you can use the pin 13 as you like.
Unless you want to use the led or SPI --> "alternate function" of this I / O (Atmel PB5 or arduino 13 ) is SCK.
CrossRoads:
1uA Max input current, so more like 1 Megohm.
Internal pullups work on analog pins too.
Measuring 0 or 5V, any digital pin can do that too. Can give yourself some protection and have the 5V drive the base of a simple NPN, emitter to Gnd, when turned on the collector will bring an internally pulled up pin low. No button needed then, no worrying about the 5V being too high, just look for the pin being low.
BTW I measured the RC decay period for an unconnected input pin and its about 1 to 2 seconds
on the Arduino Mega. Assuming about 20pF of input and PCB capacitance that implies the
input resistance of a pin (at room temp at least) is about 7 x 10^10 ohms, which is what you
expect for CMOS circuitry. The 1uA figure in the datasheet is not a measurement I suspect,
just a figure plucked from the air.