กลับหน้าหลัก
views
How to Read a Switch Input with digitalRead() on Arduino
Last updated on

How to Read a Switch Input with digitalRead() on Arduino


How to Read a Switch Input with digitalRead() on Arduino

This guide explains how to connect a switch to Arduino and write code to detect whether the switch is pressed or not. You’ll practice using digitalRead(), a fundamental command for almost every Arduino project that accepts user input.

How digitalRead() Works

Arduino digital pins can be configured as inputs or outputs. When set as input, use digitalRead(pin) to check if that pin has voltage present.

The command returns one of two values:

  • HIGH → Voltage present (around 5V)
  • LOW → No voltage (0V)

Basic Pull-down Circuit

A normally-open (NO) pushbutton doesn’t hold a stable state when not pressed. This leaves the input pin floating with no defined voltage, leading to erratic readings. The solution is to add a pull-down resistor as shown below:

Pull-down circuit diagram - pushbutton connected to pin D2 on one side and 5V on the other, with a 10kΩ resistor pulling D2 down to GND

Wiring steps:

  1. Connect one leg of the switch to 5V
  2. Connect the other leg to an Arduino digital pin (e.g., D2)
  3. Connect a 10kΩ resistor between that Arduino pin and GND (this is the pull-down)

Operation:

  • Switch open → current flows through resistor to GND → reading is LOW
  • Switch pressed → 5V flows into Arduino pin → reading is HIGH

Code: Read Switch Input to Control LED

// Pin definitions
const int buttonPin = 2;   // Switch connected to pin 2
const int ledPin = 9;      // LED connected to pin 9

// Variable to store switch state
int buttonState = 0;

void setup() {
  // Start Serial Monitor for debugging
  Serial.begin(9600);
  
  // Set LED pin as output
  pinMode(ledPin, OUTPUT);
  
  // Set switch pin as input
  pinMode(buttonPin, INPUT);
}

void loop() {
  // Read the state of the switch pin
  buttonState = digitalRead(buttonPin);

  // Check if switch is pressed (HIGH)
  if (buttonState == HIGH) {
    // Switch pressed → LED turns on
    Serial.println("LED ON");
    digitalWrite(ledPin, HIGH);
  } else {
    // Switch released → LED turns off
    Serial.println("LED OFF");
    digitalWrite(ledPin, LOW);
  }
}

What this code does:

  • Continuously reads pin D2 using digitalRead()
  • If it reads HIGH (switch pressed) → turns the LED on
  • If it reads LOW (switch released) → turns the LED off
  • Prints the state to Serial Monitor for verification

Customization Points

AdjustmentDefaultHow to change
Switch pin2Use any available digital pin
LED pin9Change to another pin or swap LED for Relay, Buzzer, etc.
Resistor configPull-downSwitch to Pull-up to invert logic

Test Using Serial Monitor

Serial Monitor output showing LED ON when switch is pressed and LED OFF when released

After uploading the code, open Serial Monitor (baud rate 115200). You should see the text change every time you press or release the switch. This confirms Arduino is reading the input correctly.

Key Takeaways

digitalRead() is a core command you need to master before building Arduino projects that accept input from switches, sensors, or other devices. Understanding pull-down and pull-up resistor logic lets you connect a wide variety of input devices reliably.

Reference Video

Video demonstrating Arduino switch input wiring and testing

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ประเมินราคาอัตโนมัติ + Reference Code

ขอให้ AI ประเมินราคาโปรเจคนี้

กรอกข้อมูลให้ครบ ระบบจะสร้างรหัสอ้างอิงและประเมินราคา/ระยะเวลาคร่าว ๆ จากรายละเอียดงาน แล้วให้กด Add LINE พร้อมพิมพ์รหัสนี้เพื่อคุยต่อ

คำถามให้ AI ประเมินแม่นขึ้น

หลังส่งฟอร์ม ระบบจะโชว์ Reference Code ให้ copy แล้วกด Add LINE เพื่อคุยต่อ ข้อมูลส่วนตัวจะไม่ถูกส่งเข้า GA4

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...