INPUT_PULLUP uses a built-in resistor, so a button only needs to connect the pin to ground. The pin reads LOW when pressed. Serial.print sends text to the Serial Monitor for debugging.
const int buttonPin = 2; void setup() { pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); } void loop() { if (digitalRead(buttonPin) == LOW) { Serial.println("Pressed!"); delay(200); // simple debounce } }