注釈
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
特別割引:最新製品の独占割引をお楽しみください。
祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[ ここ]をクリックして今すぐ参加しましょう!
超音波
概要
車をバックする際、衝突を避けるために車と周囲の障害物との距離を確認します。その距離を検出する装置が超音波センサーです。この実験では、超音波がどのように距離を検出するかを学びます。
必要なコンポーネント
このプロジェクトでは、以下のコンポーネントが必要です。
全てのキットを一式購入するのが便利です。こちらがリンクです:
名称 |
このキットのアイテム数 |
リンク |
|---|---|---|
Elite Explorer Kit |
300+ |
下記のリンクから個別に購入することもできます。
コンポーネント紹介 |
購入リンク |
|---|---|
- |
|
配線図
回路図
コード
注釈
elite-explorer-kit-main\basic_project\06-ultrasonicのパスの下にある06-ultrasonic.inoファイルを直接開く。または、このコードをArduino IDEにコピーします。
コード解析
1. 超音波センサーとLCD1602の初期化
#include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // initialize the Liquid Crystal Display object with the I2C address 0x27, 16 columns and 2 rows // Define the pin numbers for the ultrasonic sensor const int echoPin = 3; const int trigPin = 4; void setup() { pinMode(echoPin, INPUT); // Set echo pin as input pinMode(trigPin, OUTPUT); // Set trig pin as output lcd.init(); // initialize the LCD lcd.clear(); // clear the LCD display lcd.backlight(); // Make sure backlight is on }
2. LCD1602で距離を表示
void loop() { float distance = readDistance(); // Call the function to read the sensor data and get the distance lcd.setCursor(0, 0); //Place the cursor at Line 1, Column 1. From here the characters are to be displayed lcd.print("Distance:"); ////Print Distance: on the LCD lcd.setCursor(0, 1); //Set the cursor at Line 1, Column 0 lcd.print(" "); //Here is to leave some spaces after the characters so as to clear the previous characters that may still remain. lcd.setCursor(7, 1); //Set the cursor at Line 1, Column 7. lcd.print(distance); // print on the LCD the value of the distance converted from the time between ping sending and receiving. lcd.setCursor(14, 1); //Set the cursor at Line 1, Column 14. lcd.print("cm"); //print the unit "cm" delay(800); // Delay for 800 milliseconds before repeating the loop }
3. 時間を距離に変換
float readDistance(){// ...}ここでの「PING」は、超音波センサーが超音波パルス(または「PING」)を送出し、そのエコーを待つプロセスを指します。
PINGは、2マイクロ秒以上のHIGHパルスによってトリガーされます。(クリーンなHIGHパルスを保証するために、事前に短いLOWパルスを与えます。)
digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);エコーピンはPINGからの信号を読み取るために使用され、その持続時間はピンの送信からオブジェクトのエコーの受信までの時間(マイクロ秒)です。以下の関数を使用して持続時間を取得します。
pulseIn(echoPin, HIGH);音速は340m/sまたは29マイクロ秒/センチメートルです。
これにより、ピンによる往復の移動距離が得られるため、障害物までの距離を取得するために2で割ります。
float distance = pulseIn(echoPin, HIGH) / 29.00 / 2; // Formula: (340m/s * 1us) / 2