ใช้ pulseIn จับเวลาเสียงสะท้อน
วัดพัลส์เสียงสะท้อนและแปลงเป็นเซนติเมตร
ใช้ pulseIn จับเวลาเสียงสะท้อน เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Fire the Pulse
To take a reading you first send a clean trigger pulse. The HC-SR04 expects a short HIGH on Trig to start a new measurement.
Start From Clean
Set Trig LOW for a couple of microseconds first. This clears any leftover state so your trigger pulse is crisp and reliable.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);A Ten-Microsecond Pulse
Now drive Trig HIGH for 10 microseconds, then back LOW. That exact width is what the sensor needs to launch its ultrasonic burst.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);Meet pulseIn
The function pulseIn waits for a pin to go HIGH, then times how long it stays HIGH, returning that duration in microseconds.
Time the Echo
Call pulseIn on the Echo pin watching for HIGH. The number it returns is the full round-trip time of your sound pulse.
long duration = pulseIn(echoPin, HIGH);It Waits for You
pulseIn is blocking: it pauses your sketch until the echo arrives or times out. For simple distance projects that pause is perfectly fine.
Turn Time Into cm
Multiply the duration by the speed of sound and divide by two. The result is a clean distance in centimeters.
float cm = duration * 0.0343 / 2;Where That Number Comes From
Sound travels about 0.0343 cm per microsecond. That constant is just the speed of sound rewritten in units that match pulseIn.
Print It Out
Send the value to the Serial Monitor so you can watch the distance change as you move your hand toward the sensor.
Serial.print(cm);
Serial.println(" cm");Give It a Breather
Add a short delay between readings, around 60 milliseconds. This stops one echo from leaking into the next measurement.
delay(60);Handle a Timeout
If nothing reflects, pulseIn returns zero. Checking for that lets you skip junk readings instead of printing a false distance.
Quick Check
Think about what pulseIn measures.
From Pulse to Distance
Trigger, time with pulseIn, then convert and print. You can now read range in code. Next, make the sensor do something useful. ✅
คำถามที่พบบ่อย
บทเรียน “ใช้ pulseIn จับเวลาเสียงสะท้อน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ใช้ pulseIn จับเวลาเสียงสะท้อน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ใช้ pulseIn จับเวลาเสียงสะท้อน”
วัดพัลส์เสียงสะท้อนและแปลงเป็นเซนติเมตร คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ใช้ pulseIn จับเวลาเสียงสะท้อน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การวัดระยะด้วยคลื่นอัลตราโซนิก
- การต่อสาย Trigger และ Echo
- ใช้ pulseIn จับเวลาเสียงสะท้อน
- สร้างสัญญาณเตือนระยะใกล้