หน้าแรกHome › Arduino › บทที่ 05Lesson 05
เซนเซอร์วัดระยะ (Ultrasonic)Distance Sensor (Ultrasonic)
วัดระยะด้วย HC-SR04Measure distance with the HC-SR04
บทนี้เราจะได้...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
อุปกรณ์ที่ใช้What you need
เสียงเดินทางในอากาศราว 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.
โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ดLab bench: wire it, then run the code
ลากจากขาหรือช่องหนึ่งไปอีกช่องเพื่อต่อสาย · ชี้ที่ช่องเพื่อดูว่าช่องไหนเชื่อมกัน · แตะที่สายเพื่อลบ · แตะที่ 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
ภารกิจMissions
อ่านโค้ดทีละบรรทัด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 hearpulseIn(echoPin, HIGH)จับเวลา (ไมโครวินาที) ตั้งแต่ส่งเสียงจนเสียงสะท้อนกลับมาTimes (in microseconds) how long until the echo comes backduration / 58แปลงเวลาเป็นระยะทางหน่วยเซนติเมตรTurns the time into centimetresSerial.println(...)ส่งข้อความไปแสดงที่ Serial MonitorPrints a message to the Serial Monitorลองแก้เองดูสิ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.
ควิซท้ายบท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?
ใบงาน: Worksheet: เซนเซอร์วัดระยะ (Ultrasonic)Distance Sensor (Ultrasonic)
วาดภาพเซนเซอร์ส่งเสียงไปชนวัตถุแล้วสะท้อนกลับDraw the sensor sending sound to an object and the echo coming back
ทำครบทุกภารกิจแล้ว?All missions done?
กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.