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