how to integrate a simple switch to arduino ?

Hello Friends,

I simply want to integrate a switch to arduino ANALOG INPUT PINS .

What i did was, i connected one pin of the switch ( i used a toggle switch with three pinouts / switch have two positions ) to 5V . And other two pins to ANALOG PIN A0 and A1 of the arduino . I didnt use any resistors in between .

So what I now want is to read the analog input . Since the switch has two positions, when I toggle the switch into one position, the analog read of the respective pin must show 1023 ( since that pin gets 5V via the switch ), and other pin must be 0 ( since it doesn't get any voltage via the switch at that moment ) . And when I toggle the switch to the other position, this must work other-way .

Lets say like this,

Pin1 / Switch Position 1 -----> connected to pin A0
Pin2 / Switch Position 2 -----> connected to pin A1
Other pin of the switch ----> directly connected to 5V

But the problem is, the output i get into the serial monitor is not smooth . When I first turn onto position 1, it gives a result on serial print like below . The problem is Switch 2 value increases with time . And then comes to 1023 max, and then decreases . It's like a sine wave .

Please help me to figure out this.

Serial Printer Output

switch 1 : 1023
switch 2 : 300
switch 1 : 1023
switch 2 : 410
switch 1 : 1023
switch 2 : 544
switch 1 : 1023
switch 2 : 700
switch 1 : 1023
switch 2 : 897
switch 1 : 1023
switch 2 : 1000
switch 1 : 1023
switch 2 : 1022
switch 1 : 1023
switch 2 : 1023
switch 1 : 1023
switch 2 : 1021
switch 1 : 1023
switch 2 : 1000
switch 1 : 1023
switch 2 : 870
switch 1 : 1023
switch 2 : 600
switch 1 : 1023
switch 2 : 520
switch 1 : 1023
switch 2 : 114
......

Hi friends,

I found the solution.

I simply grounded one pin when the other pin is at high .

Thanks.
Dileesha.

The more proper solution is to wire pull-down resistors (say 10K ohms) from the two input pins to ground. The problem is having a 'floating' input pin condition on the pin when the switch is positioned to the other pin.

Lefty

Yes lefty.

Thanks.