Arduino-ultrasonic-sensor

提供:Dev Guides
移動先:案内検索

Arduino-超音波センサー

HC-SR04超音波センサーは、SONARを使用して、コウモリのように物体の距離を決定します。 2 cm〜400 cmまたは1インチ〜13フィートの使いやすいパッケージで、高精度で安定した読み取り値を備えた優れた非接触範囲検出を提供します。

音響的には布のような柔らかい素材は検出が難しい場合がありますが、操作は日光や黒い素材の影響を受けません。 超音波送信機および受信機モジュールが付属しています。

超音波センサー

超音波センサー放射

技術仕様

  • 電源-+ 5V DC
  • 静止電流-<2mA
  • 動作電流-15mA
  • 有効角度-<15°
  • レンジング距離-2cm – 400 cm/1″ – 13ft
  • 解像度-0.3 cm
  • 測定角度-30度

必要なコンポーネント

次のコンポーネントが必要になります-

  • 1×ブレッドボード
  • 1×Arduino Uno R3
  • 1×超音波センサー(HC-SR04)

手順

回路図に従って、以下の画像に示すように接続します。

超音波回路接続

スケッチ

コンピューターでArduino IDEソフトウェアを開きます。 Arduino言語でコーディングすると、回路が制御されます。 [新規]をクリックして、新しいスケッチファイルを開きます。

スケッチ

Arduinoコード

const int pingPin = 7;//Trigger Pin of Ultrasonic Sensor
const int echoPin = 6;//Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600);//Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds/74/2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds/29/2;
}

注意すべきコード

超音波センサーには4つの端子があります-5V、トリガー、エコー、およびGNDは次のように接続されています-

  • &plus; 5VピンをArduinoボードの&plus; 5vに接続します。
  • Arduinoボードのデジタルピン7にトリガーを接続します。
  • EchoをArduinoボードのデジタルピン6に接続します。
  • GNDをArduinoのGNDに接続します。

このプログラムでは、センサーによって測定された距離をシリアルポート経由でインチとcmで表示しました。

結果

Arduinoシリアルモニターでは、センサーで測定された距離がインチとcmで表示されます。