ห้องทดลองเทคโนโลยีTech Lab ครูใบเฟิร์นKru Baifern

หน้าแรกHome › Arduino › บทที่ 03Lesson 03

03

ปุ่มกด (Push Button)Push Button

รับข้อมูลจากปุ่มกดRead a button press

ระดับเนื้อหาLevel
1

บทนี้เราจะได้...In this lesson you will...

  • ต่อปุ่มกดให้บอร์ดอ่านค่าได้ (ขาที่รับข้อมูล ไม่ใช่ส่ง)Wire a button so the board can read it (an input pin, not an output)
  • ใช้ digitalRead() กับ if / else ตัดสินใจUse digitalRead() with if / else to decide
  • รู้จัก INPUT_PULLUP ว่าทำไมกดแล้วได้ LOWUnderstand INPUT_PULLUP and why pressing gives LOW
2

อุปกรณ์ที่ใช้What you need

Arduino UnoArduino Uno× 1
ปุ่มกด (Push Button)Push button× 1
สายจัมเปอร์Jumper wires× 2
มัธยม · ข้างในปุ่มมีอะไรSecondary · Inside the button

ปุ่ม 4 ขามีขาที่เชื่อมกันอยู่แล้วเป็นคู่ ๆ (ขาซ้าย–ขวาในแถวเดียวกัน) ตอนกด แผ่นโลหะข้างในจะเชื่อมแถวบนกับแถวล่างเข้าหากัน จึงต้องต่อสายคนละแถวA 4-leg button has its legs joined in pairs (left and right in the same row). Pressing it joins the top row to the bottom row, so the wires must go on different rows.

3

โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ดLab bench: wire it, then run the code

สีสายWire

ลากจากขาหรือช่องหนึ่งไปอีกช่องเพื่อต่อสาย · ชี้ที่ช่องเพื่อดูว่าช่องไหนเชื่อมกัน · แตะที่สายเพื่อลบ · แตะที่ LED เพื่อกลับขาDrag from one pin or hole to another to add a wire · point at a hole to see what's connected · tap a wire to remove it · tap an LED to flip it

โค้ด (แก้ได้เลย!)Code (edit it!)

ภารกิจMissions

    มัธยม · INPUT_PULLUP ทำงานยังไงSecondary · How INPUT_PULLUP works

    INPUT_PULLUP เปิดตัวต้านทานราว 20–50kΩ ในชิป ต่อขา 2 ขึ้นไปที่ 5V ตอนไม่กดปุ่ม ขา 2 จึงอ่านได้ HIGH พอกดปุ่ม ขา 2 ต่อตรงลง GND จึงอ่านได้ LOW ถ้าไม่มีตัวต้านทานนี้ ตอนไม่กด ขาจะ "ลอย" และอ่านค่าแบบสุ่มINPUT_PULLUP turns on a 20–50kΩ resistor inside the chip from pin 2 up to 5V. Unpressed, pin 2 reads HIGH. Pressed, pin 2 is tied to GND and reads LOW. Without it, an unpressed pin "floats" and reads random values.

    4

    อ่านโค้ดทีละบรรทัดReading the code, line by line

    ช่องโค้ดเริ่มจากโครงเปล่า ปิ๊งจะบอกทีละบรรทัดว่าต้องพิมพ์อะไร ตอนกดรัน บรรทัดที่บอร์ดกำลังทำจะเป็นสีเหลือง คำสั่งสำคัญของบทนี้มีดังนี้The code box starts as an empty sketch, and Ping tells you what to type line by line. While it runs, the line the board is doing turns yellow. The key commands in this lesson:

    pinMode(2, INPUT_PULLUP)ตั้งขา 2 ให้รับค่า และเปิดตัวต้านทานดึงขึ้นในบอร์ด ไม่กด = HIGHMake pin 2 an input with the built-in pull-up. Not pressed = HIGH
    int state = digitalRead(2)อ่านค่าขา 2 แล้วเก็บไว้ในตัวแปรชื่อ stateRead pin 2 and keep it in a variable called state
    if (state == LOW) { ... }ถ้ากดปุ่มอยู่ (LOW) ให้ทำในวงเล็บแรกIf the button is pressed (LOW), do the first block
    else { ... }ไม่อย่างนั้น (ไม่ได้กด) ให้ทำในวงเล็บนี้แทนOtherwise (not pressed), do this block instead
    5

    ลองแก้เองดูสิTry changing it

    แก้โค้ดในช่องโค้ดของโต๊ะทดลองด้านบนได้เลย แล้วกด ▶ รันโค้ด ดูผล อยากเริ่มใหม่ทั้งหมด กด "ล้างโค้ด"Edit the code in the lab bench's code box above, then press ▶ Run code. Want to start over? Press "Clear code".

    • แก้โค้ดให้กลับกัน: กดแล้วไฟดับ ปล่อยแล้วไฟติดMake it the other way round: press = off, release = on
    • ย้ายสายปุ่มไปขา 7 แล้วแก้โค้ดให้ใช้ได้Move the button to pin 7 and fix the code
    • ท้าทาย: กดหนึ่งครั้งไฟติดค้าง กดอีกครั้งไฟดับ (สวิตช์แบบกดติด-กดดับ) ใช้ตัวแปรจำสถานะChallenge: press once = stays on, press again = off (a toggle). Use a variable to remember.

    มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.

    6

    ควิซท้ายบทQuick quiz

    ใช้ INPUT_PULLUP แล้วกดปุ่ม digitalRead จะได้ค่าอะไร?With INPUT_PULLUP, what does digitalRead give when pressed?

    คำสั่งไหนใช้อ่านค่าจากปุ่ม?Which command reads the button?

    ทำไมต้องต่อสายปุ่มคนละแถว (บน/ล่าง)?Why do the button wires go on different rows?

    else ทำงานตอนไหน?When does else run?

    ทำครบทุกภารกิจแล้ว?All missions done?

    กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.

    กลับแผนที่Back to map
    ← บทที่ 02Lesson 02 บทที่ 04Lesson 04 →