Inputs are never 0?

Greetings,

I started playing around with a few sensors I've harvested from an old printer.
When I tested some switches, I've noticed, that the input values of the arduino pins are never 0.

Even when I have no electronics what so ever on the pins, the state of the pins seem to be random.
Digital as well as analog inputs.

Here is my example code:

void setup() {
Serial.begin(9600);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);

}

void loop() {

Serial.print("Pin 1: ");
Serial.println(digitalRead(1));
Serial.print("Pin 2: ");
Serial.println(digitalRead(2));
Serial.print("Pin 3: ");
Serial.println(digitalRead(3));
Serial.print("Pin 4: ");
Serial.println(digitalRead(4));
Serial.print("Pin 5: ");
Serial.println(digitalRead(5));

delay(100);

}

If I use a button and press it, I always get the state 1. But elsewise the state is either 1 or 0, switching randomly.

Am I doing something wrong? Is my Arduino Uno somewhat broken? (It's about 3 years old)

You're inputs are floating, so if there are no external pullup or pulldown resistors connected, it's normal for readings to fluctuate.

Have you tried using INPUT_PULLUP? With this mode, you won't need external resistors. Your switches and pushbuttons will need to be connected from GND to the input pin.

Ahh I was not aware, there is such an option.
Thanks, input_pullup did the trick! :slight_smile:

In my test setups I had a pulldown resistor. But perhaps I did something wrong. I'll fiddle a bit more around with it, maybe finding my error.

Lack of common grounds is a common mistake... That accounts for a pull-down resistor not pulling down.