Arduino Micro Pulldown resistors for pin 2 and 3

Hi,
I'm trying to use two buttons with my Arduino Micro, I've seen the tutorial about the pulldown resistor and understood that if there is no pulldown or pullup resistor a pin will remain undefined switching between ON and OFF continuously. Now i have wired my arduino like the schematic I draw and even though PIN 3 isn't connected to ground it remains OFF until I press it.
I tried:

  • connecting just PIN 3 to ground and not PIN 2 but PIN 2 becomes undefined

  • connecting PIN 2 to ground and PIN 4 (instead of PIN 3) and PIN 4 becomes undefined

Is this behaviour expected? If yes why?
I'm new to arduino and electronics in general so it might be something really obvious that I don't know
This is the code:

#include <Keyboard.h> // The main library for sending keystrokes.

int FirstPin = 2;
int SecondPin = 3;


void setup() {

  //Keyboard.begin();  // Initialise the library.

  Serial.begin(9600);
  //configure pin 2 and 3 as an input and enable the internal pull-up resistor
  pinMode(FirstPin, INPUT);
  pinMode(SecondPin, INPUT);
}

//------------------------------------------------------

void loop() {
  //read the pushbutton value into a variable
  int sensorValFirst = digitalRead(FirstPin);
  int sensorValSecond = digitalRead(SecondPin);

  if (sensorValFirst == HIGH)  {

    Serial.println("TriggerFirst");
    triggerFirst();

  }

  if (sensorValSecond == HIGH)  {

    Serial.println("TriggerSecond");
    triggerSecond();
  }
}

//------------------------------------------------------

void triggerFirst() {

  /*Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_TAB);
    Keyboard.releaseAll();*/

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

}
//------------------------------------------------------

void triggerSecond() {

  /*Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press('r');
    delay(100);//??
    Keyboard.releaseAll();*/

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

}

if there is no pulldown or pullup resistor a pin will remain undefined switching between ON and OFF continuously.

An unconnected pin will not change state continuously but its state will be unpredictable which makes it useless as an indication of a button press

Do yourself a favour and use INPUT_PULLUP in pinMode() to turn on the built in pullup resistors. Wire the buttons to take the inputs to GND when pressed and test for a LOW state on the pins to detect a button press

thanks I will use INPUT_PULLUP but could you please explain me anyways why the PIN 3 behaves as if it has a pulldown resistor even if it doesn't? I am curious
thank you again for the quick answer

could you please explain me anyways why the PIN 3 behaves as if it has a pulldown resistor

If the pin is not explicitly held in a fixed state, either by the program or by an external connection, then it could be in any state, including of course, LOW, but you cannot rely on
it

ok thanks a lot, I used INPUT_PULLUP as you suggested and everything works

lodde:
thanks I will use INPUT_PULLUP but could you please explain me anyways why the PIN 3 behaves as if it has a pulldown resistor even if it doesn’t? I am curious
thank you again for the quick answer

An unconnected input pin will have an unpredictable value that will depend on the conditions around it. Maybe it will hold HIGH. Maybe it will hold LOW. Maybe it will erratically switch between the two. Maybe it will slowly drift between them over the course of several minutes. The high impedance means very little energy is required to change the voltage at the input (Power = Volts2 / Resistance), so it is sensitive to external forces that you can’t control.

“Undefined” means you don’t know what it’s doing. It could do anything. In this circumstance, with this specific circuit and your specific electrical environment, you are seeing it stay LOW. In another situation you might see it stay HIGH. Take it next to a high power mains wire and you might get enough mains hum to see it switch at 50 or 60 Hz. It might even work differently with different chips, since their output drivers will probably have different leakage characteristics when set to input mode. The point is you can’t predict it.

Using a pulldown resistor is effectively adding its resistance in parallel with the internal impedance of the input. This lowers the effective resistance of the input, meaning it requires more energy to change the voltage enough to change its state, making it much more resistant to external noise.