0Pricing
Arduino & IoT Academy · บทเรียน

อ่านสถานะปุ่ม

ใช้ digitalRead เพื่อตรวจว่ากดปุ่มอยู่หรือไม่

อ่านสถานะปุ่ม เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Ask the Pin a Question

Now that a pin is listening, you can ask it what it sees. The function digitalRead checks a pin and tells you its current state.

It Returns HIGH or LOW

digitalRead gives back just two answers: HIGH or LOW. That is perfect for a button, which is only ever pressed or not.

int state = digitalRead(2);

Store the Reading

Save the result in a variable so you can use it. Here state holds whatever digitalRead found on pin 2 at that moment.

int buttonPin = 2;
int state = digitalRead(buttonPin);

Set the Mode First

Reading only works if you prepared the pin. Call pinMode with INPUT_PULLUP in setup() before you ever read it.

void setup() {
  pinMode(2, INPUT_PULLUP);
}

Read Inside loop()

Buttons change all the time, so you read inside loop(). Each pass grabs a fresh state, keeping your device up to date.

void loop() {
  int state = digitalRead(2);
}

Remember the Inversion

With INPUT_PULLUP the meaning flips: LOW means the button is pressed, and HIGH means it is released. Keep that in mind reading state.

See It on Serial

You can't see HIGH or LOW directly, so print it. Sending state to the Serial Monitor lets you watch the value as you press.

Serial.println(state);

Open the Channel

Before printing, open the USB channel once in setup with Serial.begin. A common speed is 9600 baud for simple projects.

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
}

Watch the Values Change

Upload, open the Serial Monitor, and press the button. You'll see the number jump from 1 to 0 the instant you push it. 🔘

HIGH and LOW Are Numbers

Under the hood HIGH is just 1 and LOW is 0. That is why your printed readings show up as plain 1s and 0s.

You Have Live Input

You now turn a physical press into a value your code can test. That single reading is the seed of every interactive device you'll build.

Quick Check

Recall what digitalRead hands back to your code.

Recap

You used digitalRead in loop() to capture HIGH or LOW, stored it, and printed it over Serial. Next you'll make an LED react to that state. ✅

คำถามที่พบบ่อย

บทเรียน “อ่านสถานะปุ่ม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “อ่านสถานะปุ่ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “อ่านสถานะปุ่ม”

ใช้ digitalRead เพื่อตรวจว่ากดปุ่มอยู่หรือไม่ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “อ่านสถานะปุ่ม” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม

ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. INPUT เทียบกับ INPUT_PULLUP
  2. อ่านสถานะปุ่ม
  3. if/else: LED ทำตามปุ่ม
  4. ต่อปุ่มบนเบรดบอร์ด
← กลับไปที่ Arduino & IoT Academy