بناء إنذار اقتراب
أطلق صوتًا عندما يقترب شيء أكثر من اللازم.
بناء إنذار اقتراب درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. ✅
الأسئلة الشائعة
هل درس «بناء إنذار اقتراب» مجاني؟
نعم — نص درس «بناء إنذار اقتراب» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.
ماذا ستتعلم في «بناء إنذار اقتراب»؟
أطلق صوتًا عندما يقترب شيء أكثر من اللازم. تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟
لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «بناء إنذار اقتراب»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟
نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- كيف يعمل قياس المسافة بالموجات فوق الصوتية
- توصيل طرفَي Trigger وEcho
- استخدام pulseIn لقياس زمن الصدى
- بناء إنذار اقتراب