Arduino DUE INPUT Defaults to HIGH

Hi everyone.
I'm using the Arduino Due, and have NOTHING plugged in, NO circuitry right now, as I am just trying to troubleshoot.

I wrote this sketch to troubleshoot, all the INPUT pins return HIGH / 1, when viewed on serial monitor, and all OUTOUT return LOW / 0, as they should.

I cannot figure out why INPUT pins are always returning HIGH even when set otherwise.
I have tried this on 2 brand new Arduino Due. Help???


const int buttonPin = 2; // UP
const int buttonPintwo = 3; // left
const int buttonPinthree = 4; // right
const int buttonPinfour = 5; // DOWN

const int buttonPinfive = 6; // ESC
const int buttonPinsix = 7; // SPACE
const int buttonPinseven = 8; // ENTER/RETURN (right side)

int val = 0;
int valtwo = 0;
int valthree = 0;
int valfour = 0;

int valfive = 0;
int valsix = 0;
int valseven = 0;

void setup() {
Serial.begin(9600);

digitalWrite(buttonPin, LOW);
digitalWrite(buttonPintwo, LOW);
digitalWrite(buttonPinthree, LOW);
digitalWrite(buttonPinfour, LOW);

digitalWrite(buttonPinfive, LOW);
digitalWrite(buttonPinsix, LOW);
digitalWrite(buttonPinseven, LOW);

// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
pinMode(buttonPintwo,INPUT);
pinMode(buttonPinthree,INPUT);
pinMode(buttonPinfour,INPUT);

pinMode(buttonPinfive, OUTPUT);
pinMode(buttonPinsix, OUTPUT);
pinMode(buttonPinseven, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
val = digitalRead(buttonPin);
Serial.println("one");
Serial.println(val);

valtwo = digitalRead(buttonPintwo);
Serial.println("two");
Serial.println(valtwo);

valthree = digitalRead(buttonPinthree);
Serial.println("three");
Serial.println(valthree);

valfour = digitalRead(buttonPinfour);
Serial.println("four");
Serial.println(valfour);

valfive = digitalRead(buttonPinfive);
Serial.println("five");
Serial.println(valfive);

valsix = digitalRead(buttonPinsix);
Serial.println("six");
Serial.println(valsix);

valseven = digitalRead(buttonPinseven);
Serial.println("seven");
Serial.println(valseven);
}

If there is nothing connected to the pin why would you expect it to read LOW? To read LOW you'd have to connect it to zero volts somehow. You have it connected to nothing, so the pin is floating. It could read anything based on whatever voltage it picks up from the environment.

interesting, something I didn't know due to me being a certified DumbDumb. I started down this path because i was getting a HIGH by default when attached to my circuit which, was a series of simple buttons, and I only wanted it to return HIGH when the button was pressed, not when it was dormant, waiting.

as you can see in the code Im trying to force the input pins LOW, but I guess if they arent grounded in a circuit the environment can still overpower so to speak and make it return HIGH because its floating?

Yep. And that's why we use pull up or down resistors. For buttons, pull ups are more common and are already present in a lot of micro controllers so you can just enable then via software. pinMode(pin, INPUT_PULLUP)

Cool, thanks for this.
I'll get my buttons reattached. I also haven't heard of "INPUT_PULLUP" - but just an FYI I guess, I just tired the same sketch but using "INPUT_PULLUP" in place of INPUT, and I still got HIGH on all my input pins. Here's hoping I can do it off the board I guess
ty

I just tired the same sketch but using "INPUT_PULLUP" in place of INPUT, and I still got HIGH on all my input pins.

That is what you would expect now that the input pins are connected internally (across a big resistor) to 5V.

One side of the switch should be connected to ground, and the other side to the input pin.

I only wanted it to return HIGH when the button was pressed

You will have to turn your logic around. HIGH when not pressed and LOW when pressed.

Is there some reason it has to be HIGH when pressed?

Not really, I had my logic reversed initially.
But I'm dealing with "Keyboard.press" and "Keyboard.release".
And sometimes I found that the button would stay "pressed", until tapped again.

I thought maybe because each button on HIGH has its initial state as RELEASE,
and then on LOW it was being PRESSED, it seemed to be "having a hard time" returning back to the release state.
I have ruled out that the actual button is sticking, etc.
I know thats VERY non-technical / logical, but i was just trying to "clean up" the code to troubleshoot this larger problem.

I'm using IF/ ELSE right now, but maybe I should be using double IFs to identify the state of each button

But I'm dealing with "Keyboard.press" and "Keyboard.release".

it seemed to be "having a hard time" returning back to the release state.
I have ruled out that the actual button is sticking, etc.

This is a different problem. I suggest that you write a simple piece of code which demonstrates the sticking key problem. Then start a new thread where you post this code, and a link to the keyboard, and any libraries you are using. Use a title for the thread which describes the problem.

ya I will
thanks again