近接アラームを作る
何かが近づきすぎたらブザーを鳴らします。
「近接アラームを作る」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。
「近接アラームを作る」で何を学びますか?
何かが近づきすぎたらブザーを鳴らします。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Arduino & IoT Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「近接アラームを作る」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このArduino & IoT Academyレッスンでコードを書いて実行できますか?
はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。