สร้างสัญญาณเตือนระยะใกล้
ส่งเสียงเตือนเมื่อมีสิ่งใดเข้าใกล้เกินไป
สร้างสัญญาณเตือนระยะใกล้ เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Turn Range Into Action
You can read distance, so now react to it. A proximity alarm buzzes the moment something comes too close to the sensor. 🚨
Add a Buzzer
Wire a small buzzer to a digital pin and to ground. Driving that pin HIGH makes a sound, and LOW makes it stop.
const int buzzerPin = 8;Set the Buzzer Pin
In setup, mark the buzzer pin as OUTPUT so the Arduino is allowed to drive it on and off whenever you decide.
pinMode(buzzerPin, OUTPUT);Pick a Threshold
Choose a trigger distance, for example 20 cm. Anything closer than this counts as too near and should set off the alarm.
const int alarmDistance = 20;Read the Distance
Each loop, take a fresh measurement using the trigger and pulseIn steps you learned, then store the result in a distance variable.
Compare It
Use an if statement to check whether the reading is below your threshold. That comparison is the brain of the whole alarm.
if (cm < alarmDistance) {
// too close!
}Sound the Alarm
When something is too close, drive the buzzer pin HIGH. Otherwise drive it LOW so it stays silent until needed.
if (cm < alarmDistance) digitalWrite(buzzerPin, HIGH);
else digitalWrite(buzzerPin, LOW);Ignore Bad Reads
Remember pulseIn can return zero on a timeout. Skip those values so a missed echo never falsely triggers your buzzer.
Add a Warning LED
Pair the buzzer with an LED on another pin. A visible blink plus the sound makes the alert clear even in a noisy room.
Smarter Alerts
You can make the buzzer beep faster as objects get closer by mapping distance to the delay between beeps, just like a parking sensor.
Make It Yours
This pattern, sense, decide, react, is the heart of every smart device. Swap the buzzer for a relay or a message to extend it.
Quick Check
Think about what decides when the buzzer sounds.
You Built a Device
You combined a sensor, logic, and an output into a working alarm. That sense-decide-react loop is what real IoT projects are made of. ✅
คำถามที่พบบ่อย
บทเรียน “สร้างสัญญาณเตือนระยะใกล้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สร้างสัญญาณเตือนระยะใกล้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สร้างสัญญาณเตือนระยะใกล้”
ส่งเสียงเตือนเมื่อมีสิ่งใดเข้าใกล้เกินไป คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “สร้างสัญญาณเตือนระยะใกล้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การวัดระยะด้วยคลื่นอัลตราโซนิก
- การต่อสาย Trigger และ Echo
- ใช้ pulseIn จับเวลาเสียงสะท้อน
- สร้างสัญญาณเตือนระยะใกล้