digital read

hi, for my project i have some home made contact switches. These contacts work pretty well, and in some parts where electricity shouldn't be conducted, i use ductape to isolate. The problem is, sometimes even though the contact is on the ductape, i get a 1 instead of a 0 using digitalRead. I really need this to work. i tried with the contact being completely off, and anything i do, i always get a 1 instead of a 0 on many pins(some work at times). What can i do, so the whenever the contact conected to the pin isn't getting electricity, it goes 0?

I'm using an arduino Uno

Thank you

try using a several thousand ohm resistor from your digital input to ground. or if you want to do it without any extra parts, hook the switch to connect to ground and enable the internal pull up resistors. an open contact will read 1, and closed will read 0.

Sounds like you might be fighting 'floating input pin' condition. Search on pull-up and pull-down resistors and why they are needed for reading simple switch contacts.


Look at the hookup examples here and create one set or the other.

So, i tried using theinternal pull up resistors, and conecting it to ground. It worked for 2 switches, but when i added 2 more, it completely failed.

I will use 4 sets of 2 switches each( a total of 8 switches).

I tried exactly this same thing with the analog read, and it worked way better, i found a barrier betwen on and off, and used it as 0 1 also. The only problem with this, is that there are only 8 pins, and there are only 6 analog pins... I guess i could create a simple circuit or each set of 2 switches, and have the combination of 1s and 0s in one single analog pin, the problem is i don't have the slightest idea how to do this...

I would try the resistors idea, but i have no way of getting them at least until monday...

Any other ideas on how to solve this issue?

Thank you for the replys

There are 19 IO pins available, where did you lose 5 to? D0 thru D13 and D14-D19 (which are the analog pins, but can be used as digital pins also). I would keep D0, D1 free to allow debugging via comms over the serial port to the monitor.

Post your code, lets see what you have.
All the pins have internal pullups, to enable them:

pinMode(pinx, INPUT);
digitalWrite(pinx, HIGH);

now that pin will read as 1 unless pulled to ground.
So, you don't need any resistors, you just have to think of 0 as the pressed condition instead of 1.

The software doesn't care:

if (inputx == 0) {then do something}

works as well as

if (inputx == 1){then do something}

and you don't need external resistors.

Just change your logic approach a little.

#include <Servo.h>

Servo myservo[2]; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

int a;
int b;

byte Pin1[2]={1, 8};
byte Pin2[2]={4, 13};

void setup()

myservo[0].attach(10); // attaches the servo on pin 9 to the servo object

for (byte i=0; i < 2; i++){
pinMode(Pin1*, INPUT);*
_ pinMode(Pin2*, INPUT);_
_ digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);}
//End for*


void loop()
* int rot;*

* for (byte i=0; i < 2; i++){*
* rot=GetAngle(i);*

* Serial.print(rot);*
* Serial.print(" ");*
* }*
* Serial.println(" ");*

* delay(250); // waits 15ms for the servo to reach the position*
int GetAngle(byte IClaw) {
* // 00=1 01=2 11=3 10=4*
* a=digitalRead(Pin1[IClaw]);*
* b=digitalRead(Pin2[IClaw]);*

* if (a==0) return (b+1);*
* else return (4-b);*
* } //End GetAngle*
That's my code.
The circuit is simple: from arduino 5V to every contact positive pole, and from each negative pole of each switch to a different pin.

I don't understand how these arrays:

byte Pin1[2]={1, 8};
byte Pin2[2]={4, 13};

are getting set as inputs here. Maybe if you had Pin1 [ i ], Pin2 [ i ] ??

for (byte i=0; i < 2; i++){
    pinMode(Pin1, INPUT);
    pinMode(Pin2, INPUT);

    digitalWrite(Pin1, HIGH);
    digitalWrite(Pin2, HIGH);}

This is like Switch B in the example schematic I posted?

"The circuit is simple: from arduino 5V to every contact positive pole, and from each negative pole of each switch to a different pin."

If so, you need pull down resistors to make it read low when the switch is not pressed. If you wire like Switch A instead, that is the positive pole to an input pin and the negative to ground, the internal pullups will work. Just need to change your logic here to match that:

int GetAngle(byte IClaw) {
  // 00=1 01=2 11=3 10=4
   if (a==0) return (b+1);
   else return (4-b);
  } //End GetAngle

//End for

Thank you so much. Wiring it up like that worked perfect, the pullup resistors are making its job.

BTW before seeing your post i tried setting the pins to OUTPUT, and digitalWrite(low), and it kinda worked...