Ships worldwide · rates shown at checkout
Free help & advice · we reply within 24 hours
30-day returns · 12-month warranty on faults

05 - Button

Lonely Binary |

# Push Button Switch ![Push Button](/配件/LB-COMPONENT-SET/images/components/button.jpg) ## What is Push Button Push button is a mechanical switch that connects the circuit when pressed and disconnects when released. ### Push Button Features - **Mechanical Switch**: Controls circuit through physical contact - **Momentary Action**: Press to connect, release to disconnect - **4 Pins**: Divided into two groups, both groups connect simultaneously when pressed - **Debouncing Required**: Has mechanical bounce, software needs to handle ## Working Principle Push button has 4 pins, divided into two groups: - **When Pressed**: Both groups of pins connect respectively - **When Released**: Both groups of pins disconnect ### Pin Connection ![Push Button Pin Diagram](/配件/LB-COMPONENT-SET/images/circuits/button_pinout.png) Push button internal structure: ``` Pin1 ←→ Pin2 (one group) Pin3 ←→ Pin4 (another group) ``` When pressed, both groups of pins connect simultaneously. ## Usage Methods ### Push Button Lights Up LED (Pull-down Resistor Method, Recommended) #### Breadboard Connection Diagram ![Push Button LED Circuit Diagram](/配件/LB-COMPONENT-SET/images/circuits/button_led.png) #### Connection Instructions According to the diagram above, connection order is: ``` VCC (5V) → Push button one end Push button other end → LED anode → 220Ω resistor → GND LED anode → Pull-down resistor (10KΩ) → GND ``` **Working Principle**: - **When Pressed**: Current flows through push button and LED, LED lights up - **When Released**: Pull-down resistor connects LED anode to GND, LED turns off **Effect**: Press button LED lights up, release button LED turns off **Key Points**: - Push button one end connected to VCC, other end connected to LED anode - LED anode also connected to pull-down resistor (10KΩ), other end of pull-down resistor to GND - Pull-down resistor ensures LED anode is pulled to GND when button is released, LED turns off ## Arduino Example Code ### Basic Example: Push Button Controls LED ```cpp const int ledPin = 13; const int buttonPin = 2; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor } void loop() { if (digitalRead(buttonPin) == LOW) { // LOW when pressed digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } } ``` ### Connection Method (Using Internal Pull-up) - **Push button one end** → **Arduino Pin 2** - **Push button other end** → **GND** **Explanation**: Using `INPUT_PULLUP` mode, Arduino internally provides pull-up resistor automatically, no external resistor needed. ### Advanced Example: Button Debouncing ```cpp const int ledPin = 13; const int buttonPin = 2; int buttonState = HIGH; int lastButtonState = HIGH; unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; // Debounce delay 50ms void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); } void loop() { int reading = digitalRead(buttonPin); // If state changed, reset timer if (reading != lastButtonState) { lastDebounceTime = millis(); } // If delay time is sufficient, update state if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; // Toggle LED state when button is pressed if (buttonState == LOW) { digitalWrite(ledPin, !digitalRead(ledPin)); } } } lastButtonState = reading; } ``` **Explanation**: Debouncing can avoid false triggering caused by button bounce. ## Common Questions and Answers ### Q1: Push Button Pressed But No Response? - Check if push button is connected correctly - Check if pull-up or pull-down resistor is used (or use INPUT_PULLUP) - Check if level read in code is correct (pull-up: LOW when pressed) - Check if push button is damaged ### Q2: Push Button Response Not Sensitive? - Add software debouncing - Check if connection has good contact - Try using external pull-up/pull-down resistor (10KΩ) ### Q3: Why Pull-up or Pull-down Resistor is Needed? - Avoid floating pins, floating pins have uncertain level - Provide stable high or low level - Arduino can use internal pull-up (INPUT_PULLUP), no external resistor needed ## Safety Precautions ⚠️ **Important Notes**: - Must use pull-up or pull-down resistor to avoid floating pins - Push buttons have mechanical bounce, software needs debouncing - Do not exceed maximum current of push button (usually very small, a few milliamps) ## Practical Applications Push buttons are commonly used for user input, control switches, menu selection, etc. In Arduino projects, commonly used to control switches of LEDs, motors, relays, and other devices.