検索コード

Sharp赤外線距離モジュール向けのArduinoコードとビデオ

Sharp赤外線距離モジュール向けのArduinoコードとビデオ

このチュートリアルでは、Sharp製の赤外線距離モジュールを使って距離を測定し、検出した距離に応じて動作をトリガーする方法を解説します。本プロジェクトでは、ハードウェアのセットアップ方法と、距離測定を読み取り、物体が一定の範囲内に検出されたときにブザーを鳴らすArduinoコードの記述方法を示します。部品の配線方法を学び、コード内の主要な識別子(変数名やピン定義など)を理解できるようになります。

Sharpt IR distance sensor

このプロジェクトでは、赤外線を発し、反射した光に基づいて物体までの距離を測定するSharpのIRセンサーを使用します。物体が検出範囲内に入るとブザーが作動し、Sharp IRモジュールの機能を簡単かつ効果的に示します。セットアップとコードの視覚的なガイドは動画(00:00)を必ず確認してください。

ハードウェアの解説

このプロジェクトの主な構成要素は、シャープ製の赤外線距離センサー、Arduinoボード、そしてブザーです。シャープの赤外線センサーは送信機と受信機で構成されており、これらが連携して距離を測定します。送信機からの赤外線が物体に当たって反射し受信機に戻ると、その光が戻るまでにかかった時間に基づいて距離を算出できます。

Arduinoはコントローラとして動作し、IRセンサーから距離測定値を読み取り、その測定値に基づいてブザーを制御します。指定した範囲内で物体が検出されるとブザーが鳴り、この構成はロボットの障害物検知などの用途に有用です。

データシートの詳細

製造元鋭い
部品番号GP2Y0A21YK0F
ロジック/IO電圧5 V
電源電圧4.5~5.5 V
出力電圧0.4〜2.5V
測定範囲10~80 cm
応答時間50ミリ秒
出力電流20 mA
パッケージモジュール
備考 / バリエーション複数の距離範囲で利用可能

  • 損傷を避けるため、適切な電源(4.5 - 5.5 V)を確保してください。
  • 必要に応じて抵抗器を使用して電流を制限してください。
  • 正確な読み取りのためにセンサーを清潔に保ってください。
  • 周囲光による干渉に注意してください。
  • 配線は断続的な接続が生じないよう、確実に固定してください。

配線手順

Arduino wiring for Sharf IR distance sensor
Arduino wiring for Sharf IR distance sensor

Sharp製IR距離モジュールを配線するには、モジュールのGNDピンをArduinoのGNDピンに接続します。次に、モジュールのVCCピンをArduinoの5Vピンに接続します。Sharp IRモジュールの信号出力ピンはArduinoのピン2に接続し、距離データを読み取ります。

ブザーでは、長いピン(プラス)をArduinoの8番ピンに、短いピン(マイナス)をGNDに接続してください。すべての接続が確実で、部品に正しく電源が供給されていることを確認して、正しく動作するようにしてください。

コード例とウォークスルー

以下は、ピンを定義しセンサーをセットアップするArduinoコードの簡単な抜粋です。識別子IRセンサー信号に使用されるアナログピンとして定義されます。

#define IR A0 // define signal pin
SharpIR SharpIR(IR, model); // Initialize SharpIR with the defined pin

このコードは指定されたアナログピンでSharp製IRセンサーを初期化します。識別子model使用されているセンサーの種類を指定しており、この場合はGP2Y0A21YK0Fモデルに対して1080に設定されています。

setup 関数内で、距離の測定値を監視するためにシリアル通信を開始します:

void setup() {
    Serial.begin(9600); // Initialize serial communication
    Serial.println("Robojax Sharp IR  "); // Print message to serial monitor
}

setup関数はシリアル通信を9600ボーで初期化し、Arduinoが距離データをシリアルモニタに送信して観察できるようにします。

loop() 関数内で、センサーから距離を読み取り、それを表示します:

void loop() {
    int dis = SharpIR.distance(); // Read the distance
    Serial.print("Mean distance: "); // Print distance to serial monitor
    Serial.println(dis); // Output the distance measurement
}

この抜粋は、距離を連続して読み取り、シリアルモニタに出力する方法を示しています。変数disSharp製IRセンサーで測定された距離の値を保持します。

デモンストレーション/当日の流れ

セットアップが完了すると、Sharp製IRセンサーの指定範囲内に物体が検出されるたびにブザーが鳴るはずです。シリアルモニタには「Mean distance」と、測定された距離(センチメートル)が表示されます。範囲内に物体がない場合も、モニタにその旨が表示されます(ビデオでは02:30)。

よくある落とし穴としては、センサーが正しく位置合わせされていることや、赤外線信号を遮る障害物がないことを確認する点が挙げられます。また、読み取りが不安定にならないよう電源が安定しているかを確認してください。

動画のタイムスタンプ

  • 00:00- プロジェクトの紹介
  • 02:30- センサー機能の実演
  • 03:45- 配線の説明
  • 午前05:00- コードのウォークスルー

画像

Sharpt IR distance sensor GP2Y0A21YK0F
Sharpt IR distance sensor GP2Y0A21YK0F
Arduino wiring for Sharf IR distance sensor
Arduino wiring for Sharf IR distance sensor
Sharpt IR distance sensor
Sharpt IR distance sensor GP2Y0A21YK0F
69-This is the Arduino code and video for a Sharp Infrared Sensor Module
言語: C++
/*
 * Sharp IR (infrared) distance measurement module for Arduino
 * Measures the distance in cm.

 * Original library: https://github.com/guillaume-rico/SharpIR

 * Watch video instruction for this code: https://youtu.be/GL8dkw1NbMc
 *
 * Full explanation of this code and wiring diagram is available at
 * my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62

 * Written by Ahmad Shamshiri on Feb 03, 2018 at 07:34
 * in Ajax, Ontario, Canada. www.robojax.com
 *

 * Get this code and other Arduino codes from Robojax.com
 Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
 all in one place. 

 If you found this tutorial helpful, please support me so I can continue creating
 content like this. 

 or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/




 */
  // Sharp IR code for Robojax.com
#include <SharpIR.h>

#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            430 for GP2Y0A41SK
/*
2 to 15 cm GP2Y0A51SK0F	use 1080
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series	use 430
10 to 80 cm GP2Y0A21YK0F	use 1080
10 to 150 cm GP2Y0A60SZLF	use 10150
20 to 150 cm GP2Y0A02YK0F	use 20150
100 to 550 cm GP2Y0A710K0F	use 100550

 */

SharpIR SharpIR(IR, model);
void setup() {
    // Sharp IR code for Robojax.com
 Serial.begin(9600);
 Serial.println("Robojax Sharp IR  ");
}

void loop() {
    // Sharp IR code for Robojax.com
    delay(500);

  unsigned long startTime=millis();  // takes the time before the loop on the library begins

  int dis=SharpIR.distance();  // this returns the distance to the object you're measuring

  // Sharp IR code for Robojax.com

  Serial.print("Mean distance: ");  // returns it to the serial monitor
  Serial.println(dis);
  //Serial.println(analogRead(A0));
  unsigned long endTime=millis()-startTime;  // the following gives you the time taken to get the measurement
 Serial.print("Time taken (ms): ");
 Serial.println(endTime);
     // Sharp IR code for Robojax.com

}

必要かもしれないもの

リソースと参考文献

ファイル📁

Arduinoライブラリ(zip)

フリッツィングファイル