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

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

05

เซนเซอร์วัดระยะ (Ultrasonic)Distance Sensor (Ultrasonic)

วัดระยะด้วย HC-SR04Measure distance with the HC-SR04

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

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

  • รู้ว่าเซนเซอร์ส่งเสียงออกไปแล้วฟังเสียงสะท้อน เหมือนค้างคาวLearn that the sensor sends a sound and listens for the echo, like a bat
  • ใช้ pulseIn() จับเวลา แล้วคำนวณเป็นเซนติเมตรUse pulseIn() to time it and work out centimetres
  • แสดงผลบน Serial MonitorShow the result in the Serial Monitor
2

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

Arduino UnoArduino Uno× 1
เซนเซอร์ HC-SR04HC-SR04 sensor× 1
สายจัมเปอร์Jumper wires× 4
มัธยม · ทำไมหารด้วย 58Secondary · Why divide by 58?

เสียงเดินทางในอากาศราว 343 เมตร/วินาที = 0.0343 cm ต่อไมโครวินาที เสียงต้องเดินไปและกลับ ระยะจึง = เวลา × 0.0343 ÷ 2 ≈ เวลา ÷ 58Sound travels about 343 m/s = 0.0343 cm per microsecond. It goes there and back, so distance = time × 0.0343 ÷ 2 ≈ time ÷ 58.

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

    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:

    digitalWrite(trigPin, HIGH)สั่งให้เซนเซอร์ส่งเสียงความถี่สูงที่หูคนไม่ได้ยินออกไปTells the sensor to send a high-pitched sound people can't hear
    pulseIn(echoPin, HIGH)จับเวลา (ไมโครวินาที) ตั้งแต่ส่งเสียงจนเสียงสะท้อนกลับมาTimes (in microseconds) how long until the echo comes back
    duration / 58แปลงเวลาเป็นระยะทางหน่วยเซนติเมตรTurns the time into centimetres
    Serial.println(...)ส่งข้อความไปแสดงที่ Serial MonitorPrints a message to the Serial Monitor
    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".

    • เพิ่มโค้ดให้ไฟ L ติดเมื่อวัตถุใกล้กว่า 10 cm (ใช้ if)Add code so the L light turns on when something is closer than 10 cm (use if)
    • เปลี่ยนให้วัดถี่ขึ้น ทุก 0.1 วินาทีMeasure more often, every 0.1 seconds
    • ท้าทาย: แปลงเป็นนิ้วด้วย (1 นิ้ว = 2.54 cm) และแสดงทศนิยมโดยใช้ตัวแปร floatChallenge: also show inches (1 inch = 2.54 cm) with decimals using a float

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

    6

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

    เซนเซอร์ HC-SR04 วัดระยะด้วยอะไร?What does the HC-SR04 use to measure distance?

    ขาไหนของเซนเซอร์ฟังเสียงสะท้อน?Which sensor pin hears the echo?

    ถ้าวัตถุอยู่ไกลขึ้น ค่าที่ pulseIn ได้จะ...If the object is further away, pulseIn gives...

    ผลลัพธ์จาก Serial.println() ไปแสดงที่ไหน?Where does Serial.println() show its output?

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

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

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