Connecting a switch to an analog pin

Hi,

I have an application in which I want to connect a switch to Arduino's input pin. Depending on whether a user has flipped the switch to "on" or "off" the value read at the pin should change.

I am wondering what is the correct way to connect the hardware. Do I need to add a resistance to limit the current? What are good resistance values? I am using a a simple flip switch for this application. A circuit diagram or some connection description would be extremely helpful.

I am using an Arduino BT and powering it with four 1.2V AA batteries. Also, although I an using the pin in a digital manner (either on or off) I prefer to use the analog inout pins for this purpose, as I have used up all the digital pins on my board.

regards,
A

Hi!

The resistance of an arduino input pin is above 100MOhm...

So u could use a 10MOhm resistor to save energy...
E. g.:
GND ---- switch/pin1
5V ---- 10MOhm resistor ---- some arduino input pin ---- switch/pin2
(the resistor will act as a pullup resistor)

if u dont like an own resistor, u can use the built-in pullup resistor...
BUT: it just has about 20kOhm, which causes an "on current" of 250uA (0.00025A), which is unnecessary high...
The softwar part would be this:
pinMode(XXX,INPUT);
digitalWrite(XXX,HIGH);
digitalRead(XXX); // HIGH if switch is off, LOW if connected...

Bye

Your title says analog pin but your discription is of an on/off application (a switch) and would best use a digital input pin, however analog pins can be programmed to operate as digital pins if you wish. A digital pin will read in software as a HIGH or LOW binary value.

So what is your question really? If it's to utilize a analog input pin as a digital pin, it's just a matter of the pin number you use. If you use pin #14 in your digital read/write/mode statements it will allow the analog pin #0 to operate just like any of the 0-13 digital pins. Pin #15 for A1, etc.

Lefty

Hi,

Thanks for the reply. I have a three pin switch, which can select from between two inputs (two pins to select a source and the third to continue the connection forward). Based on what you say, I think following might be a good arrangement for me:

GND----switch(pin 1)
5V ----- 10MOHM resistor --- switch(pin 2)
switch (pin 3) ---- arduino input pin

Does that work?

regards,
A

Hard to tell. If your switch is a SPDT (they have 3 pins) and the common switch terminal is the one you call pin3, then it can work. There would be no need for the resistor or a +5v connection at all as the Arduino pins have programmable pull-up resistor that will establish the HIGH value.

You really should draw out the switch contacts for us or post a link to it's data sheet, pins 1,2 and 3 mean nothing without further documentation or discription.

Again if you program an analog pin to operate as a digital pin you don't have to be thinking about voltage levels, just high and low. The low will be the ground connection through the switch in one position and the HIGH can be established by the internal pull-up resistor so the switch only has to provide an open connection when you want to read a HIGH in it's opposite postion.

Lefty

Hi Lefty,

Thanks for the reply. Sorry I dont have the diagram scanned, but I think you got it right. pin1 and pin2 are the input pins and pin3 is the common switch terminal. I have drawn below the schematic in text. I hope the text formatting works.

SP1, SP2, SP3 are pins 1, 2, 3 on the switch. A "" means a selector between SP1 and SP2.

GND ---------------------SP1
----SP3 ---------Arduino Pin
5V ------10MOHM-------SP2

regards,
A

5V ------10MOHM-------SP2

Per my prior post, you don't need the above part, just the GND connection (SP1) and the common (SP3) connection to the input pin. You turn on the internal pull-up resistor for the +5vdc value with a software command.

Lefty

BUT: it just has about 20kOhm, which causes an "on current" of 250uA (0.00025A), which is unnecessary high...

Just to clear things up there are two wrong things in this statement:-

  1. Internal pull ups are 50K (it says so in the data sheet)
  2. 250uA is not unnecessarily high for a pull up resistor. The higher the current the more resistant to noise pick up you have. Noise pickup will cause false readings from a digital pin. Typically I would use a 3K pull up and in noisy environments I would use 1K or below.

Talking about 10M resistors in this context is just plane silly.

the internal pull-up resistors r 20kOhm (min.) to 50kOhm (max.) according to the datasheet for the 168...
btw: the arduino reference says 20K...
i dont know the typical value... and the datasheet is blank at that place... ::slight_smile:

luckily that switch has 3 pins, so that we dont need any pull-up...
just during the transition there might be some noise, which might need software or hardware (a 100nF capacitor?) attention...

and we can use a digital pin, because there r just 2 conditions possible...
that is an advantage, because digital pins can be read faster...

so i would recommend the following "circuit":
GND ---- switch pin1
5V ---- switch pin2
arduino input pin ---- switch pin3 (---- tiny capacitor ---- GND)

BUT: dont connect GND directly to the input pin by accident, because that would cause a short circuit, if the switch is (not(?)) pressed...

so i would recommend the following "circuit":

Sorry but I wouldn't. This assumes that your switch is a "break before make", some are "make before break" and the input is effectively disconnected during part of the switch travel. Yes you have a capacitor to cope with this but why? A resistor is much cheaper and the current consumption compared to the rest of the processor is negligible.

If you are being green then it takes more energy to make a capacitor than make a resistor. But in any case your suggestion, of connecting directly to the +ve rail, is just bad electronic design.

Fortunately advice is free and people are able to choose for themselves. :slight_smile:

oh - there r "make before break" switches? fünny... ::slight_smile:

Yes there are. From Wikipedia:-

A switch with both types of contact is called a changeover switch. These may be "make-before-break" which momentarily connect both circuits, or may be "break-before-make" which interrupts one circuit before closing the other.

Link Switch - Wikipedia