Reading a button press with Arduino is a fundamental skill in embedded programming. Here’s how to do it step by step: ![FHRC2ZEGSLEWL2O](https://hackmd.io/_uploads/SJTxg2jrxg.jpg) **What You Need:** * 1x [Arduino board](https://www.ampheo.com/c/development-board-arduino) (e.g., [Arduino Uno](https://www.ampheo.com/product/a000046-25542493)) * 1x [Push button](https://www.onzuu.com/category/pushbutton-switches) * 1x 10kΩ [resistor](https://www.onzuu.com/category/resistors) (optional if using internal pull-up) * [Breadboard](https://www.onzuu.com/category/solderless-breadboards) and [jumper wires](https://www.onzuu.com/category/jumper-wires-pre-crimped-leads) **Wiring the Button (with Pull-Down Resistor):** ![企业微信截图_20250709164507](https://hackmd.io/_uploads/HkrQ3sjSxx.png) Add a 10kΩ resistor between pin 2 and GND → When the button is pressed, pin 2 reads HIGH **Basic Code: Detect Button Press** ``` cpp const int buttonPin = 2; // Connected to the button const int ledPin = 13; // Built-in LED void setup() { pinMode(buttonPin, INPUT); // Set button pin as input pinMode(ledPin, OUTPUT); // Set LED pin as output } void loop() { int buttonState = digitalRead(buttonPin); // Read button if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); // Turn LED on if pressed } else { digitalWrite(ledPin, LOW); // Turn LED off if not } } ``` **Alternate Wiring: Internal Pull-Up (No Resistor Needed)** * Connect one side of the button to GND * Connect the other to pin 2 * Use internal pull-up: ``` cpp void setup() { pinMode(buttonPin, INPUT_PULLUP); // Activates internal pull-up resistor } ``` Note: Now LOW = pressed, HIGH = unpressed (inverted logic) **Summary:** * Use digitalRead() to check button state * Use pull-down (external) or pull-up (internal) to avoid floating inputs * Buttons can control LEDs, trigger events, or navigate menus