Button test input pin Arduino Uno

Hi everyone,

Im new here, and also to Arduino, and I have been learning quite a lot from here! I do have a question, and I hope this is the right thread!

So, I have an Arduino Uno, in that Arduino I connect pin 2 to a button and then the other side of the button goes to the ground!

Then the code goes like this:

int sigPin = 2;

void setup(){
pinMode(sigPin, INPUT);
Serial.begin(9600);
}

void loop(){

Serial.println(digitalRead(sigPin));

}

The issue is, that I would expect the serial to spit ou 1's until I press the button and while the button is pressed I would expect 0's, but, I get a random value showing without pressing the button, 0's and 1', just randomly switching, and, if I press the button I get the 0's I expected! what might this be?

Greetings,
Guilherme Menescal Dantas

   pinMode(sigPin, INPUT_PULLUP);

Yess, thanks Paul_B, first time I read about INPUT_PULLUP, will read all about.

It is now working perfectly by the way.

Hello Gui, and welcome!

Do you know C or any other programming language?
With only C you can be effective with Arduino.

Do you know where to find the Arduino Reference Page or other resources?
There are links in the colored bar at the top of the page.

If you bookmark references then when you use the IDE, also open those in a web browser to have your "book" instantly at hand. I zoom the pages, I can't do that with books!

Arduino Foundations will give you a good knowledge base to start with.