Build a Proximity Alarm
Buzz when something gets too close.
Build a Proximity Alarm is a free Arduino & IoT Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. ✅
Frequently asked questions
Is the “Build a Proximity Alarm” lesson free?
Yes — the full text of “Build a Proximity Alarm” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.
What will I learn in “Build a Proximity Alarm”?
Buzz when something gets too close. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Arduino & IoT Academy?
No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Build a Proximity Alarm” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Arduino & IoT Academy lesson?
Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- How Ultrasonic Ranging Works
- Trigger & Echo Wiring
- pulseIn to Time the Echo
- Build a Proximity Alarm