กลับไปหน้ารวมไฟล์
using-buttons-with-arduino-54a6e8.md

Main push button article: Using push buttons with Arduino

Main switch button article: Using switch buttons with Arduino

Let's speak about push buttons, the wiring and how to implement the code for this circuit elements in Arduino. Push buttons connect two points in a circuit when you press them. That means that logic state of the circuit change when you press and keep pressed the button. Switch buttons instead maintain the state without the need to keep the button pressed. That means that logic state of the circuit change every time you press the button.

Wiring schema

This example demonstrates the use of a button with Arduino Nano using internal pull-up resistor. That means that we will have a default HIGH value and LOW value when the button is turned ON.

Arduino Nano wiring with a button

Note: the button pin can be connected to Arduino Nano D4 or any other digital input pin.

Arduino code for push button

We've defined a struct (called button) to represent the state of the button. The serial monitor will output that state in real time.

#define BUTTON_PIN 4

struct button {
byte pressed = 0;
};

button button;

void setup()
{
pinMode(BUTTON_PIN, INPUT_PULLUP);

Serial.begin(115200);
}

void loop()
{
button.pressed = isButtonPressed(BUTTON_PIN);

if (button.pressed) {
Serial.println("Button pressed");
} else {
Serial.println("Button not pressed");
}
}

bool isButtonPressed(int pin)
{
return digitalRead(pin) == 0;
}

Arduino code for switch button

We've defined a struct (called toggle) to represent the state of the switch. The serial monitor will output that state in real time.

#define TOGGLE_PIN 4

struct toggle {
byte on = 0;
};

toggle toggle;

void setup()
{
pinMode(TOGGLE_PIN, INPUT_PULLUP);

Serial.begin(115200);
}

void loop()
{
toggle.on = isToggleOn(TOGGLE_PIN);

if (toggle.on) {
Serial.println("Toggle ON");
} else {
Serial.println("Toggle OFF");
}
}

bool isToggleOn(int pin)
{
return digitalRead(pin) == 0;
}

Note: we use toggle to define the button because switch is reserved word and may cause conflicts.


🛠️ เจาะลึกเบื้องหลังการทำงาน (Deep Dive / Technical Analysis)

Pushing a button seems incredibly simple until you connect one to an Arduino and realize it behaves like an insane random number generator. The Using Buttons with Arduino project is the essential foundational lesson in electrical "Floating States," teaching programmers why raw C++ logic fails without basic physical electronics knowledge.

The Floating Pin Crisis

If you connect a button directly between 5V and Pin 2, the Arduino will read HIGH when you press it.

  1. The Phantom Signal: But what happens when you let go? The wire is connected to nothing. It acts as a tiny radio antenna.
  2. The Arduino's processor is so sensitive it will pick up the static electricity from your skin or the 60Hz hum from a nearby wall outlet. Pin 2 will violently fluctuate between HIGH and LOW thousands of times a second, crashing your entire software!

The Pull-Down Resistor (Forcing Logic)

You must mechanically force the Pin to choose a state.

  • You wire a 10k Ohm Resistor from Pin 2 straight down to GND.
  • When the button is NOT pressed, any stray static electricity is harmlessly sucked down through the resistor into the ground. Pin 2 definitively reads LOW.
  • When you press the button, 5V power rushes in. Electricity always takes the path of least resistance. It chooses Pin 2 instead of fighting the massive 10K resistor! Pin 2 definitively reads HIGH.

The Software Shortcut (INPUT_PULLUP)

Breadboarding physical resistors is tedious. Modern ATmega chips have microscopic resistors built into the silicon itself!

  • Instead of using pinMode(btnPin, INPUT);, you write: pinMode(btnPin, INPUT_PULLUP);
  • This activates an invisible 20K Ohm internal resistor connecting Pin 2 to 5V.
  • The Flipped Logic: Now, the button must be wired to GND.
  • Unpressed = HIGH. Pressed = LOW! This completely changes how you write your code, but saves infinite space on your circuit board!

Essential Input Hardware

  • Arduino Uno/Nano.
  • Tactile Push Buttons.
  • 10k Ohm Resistors (Color bands: Brown, Black, Orange).
  • LEDs to visually confirm the digitalRead() boolean state.

ข้อมูล Frontmatter ดั้งเดิม

title: "Using buttons with Arduino"
description: "How to use push and switch buttons with Arduino"
author: "hibit"
category: ""
tags:
  - "button"
  - "switch"
  - "push"
views: 2686
likes: 0
price: 299
difficulty: "Easy"
components:
  - "1x Mini Breadboard"
  - "1x Switch buttons"
  - "1x Push buttons"
  - "1x Arduino Nano"
tools: []
apps:
  - "1x Arduino IDE"
downloadableFiles:
  - "https://github.com/hibit-dev/buttons"
documentationLinks: []
passwordHash: "0c55c7c45bb34f811cb7e9ae98569af0ce6943959b1e51bf336603ac2f4011f1"
encryptedPayload: "U2FsdGVkX19TVHNQxzh7N5b+q5ycnhrU5M6Kpj7fpsSq3txBaAPN/dLkWB5CV6jRq3wh2O3cFco/GPhLyw9+7IGOESrMBpiak47mpFBvW/A="
seoDescription: "Learn how to connect and code push and switch buttons with Arduino for basic electronics projects."
videoLinks: []
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/using-buttons-with-arduino-54a6e8_cover.jpg"
lang: "en"