検索コード

LCD1602ディスプレイ付きSharp赤外線距離モジュールのArduinoコードとビデオ

LCD1602ディスプレイ付きSharp赤外線距離モジュールのArduinoコードとビデオ

このチュートリアルでは、Sharpの赤外線距離モジュールをLCD1602ディスプレイと組み合わせて、距離を正確に測定する方法を学びます。Sharpの赤外線センサーは測定した距離を出力し、その値はLCDに表示されるため、リアルタイムでフィードバックを得ることができます。プロジェクトの最後には、4〜30センチメートルの距離を測定できる動作するセットアップが完成します(動画では00:30)。

シャープ アナログ出力モジュール:
2〜15cm GP2Y0A51SK0F
4〜30 cm GP2Y0A41SK0F / GP2Y0AF30 シリーズ
10〜80cm GP2Y0A21YK0F
10〜150cm GP2Y0A60SZLF
20〜150 cm GP2Y0A02YK0F
100~550cm GP2Y0A710K0F

Sharpt IR distance sensor

シャープのIR距離センサーは赤外線を発光し、光が戻ってくるまでの時間を測定することで動作します。これにより、受信した光の量に基づいて距離を算出できます。LCD1602ディスプレイは、この情報を読みやすい形式で表示するために使用されます。構成はコンパクトで、ロボット工学や距離測定システムを含むさまざまな用途に適しています。

ハードウェア解説

このプロジェクトではSharp製の赤外線距離センサーとLCD1602ディスプレイを使用します。SharpのIRセンサーには3本の配線があり、赤い線は5V電源に、黒い線はグラウンド(GND)に、黄色い線は出力信号でArduinoのアナログピンに接続します。LCD1602ディスプレイは通信のために複数のピンが必要で、これらもArduinoに接続されます。

LCD1602ディスプレイは、送信されるデータを管理するためにLiquidCrystalライブラリを使用しており、表示出力を簡単に制御できます。このライブラリはArduinoとLCD間の通信を処理し、テキストや数値を簡単に表示できるようにします。

データシートの詳細

製造者鋭い
部品番号GP2Y0A41SK0F
ロジック/IO電圧5ボルト
電源電圧4.5~5.5V
動作距離範囲4〜30 cm
出力電圧(30cmで)0.4 V
出力電圧(4 cmで)3 V
応答時間30ミリ秒
パッケージコンパクトモジュール
備考 / バリエーション異なる範囲向けに複数のモデルを用意しています

  • センサーに5 Vで正しく電源が供給されていることを確認してください。
  • すべてのコンポーネントに共通のグラウンドを使用してください。
  • 出力電圧は距離によって変化するため、注意してください。
  • 出力信号の読み取りを安定化させるためにフィルタの使用を検討してください。
  • 正確な測定のため、センサーを清潔に保ち、障害物がないようにしてください。

配線手順

Arduino Wiring Diagram of Sharp distance sensor with LCD1602-2
Sharp距離センサーとLCD1602-2のArduino配線図 — Arduino Wiring Diagram of Sharp distance sensor with LCD1602-2

Sharpの赤外線距離モジュールを配線するには、赤い線をArduinoの5Vピンに接続します。次に、黒い線をArduinoのグラウンド(GND)ピンのいずれかに接続します。距離を出力する黄色い線はアナログピンに接続してください。A0Arduino上で、LCD1602ディスプレイのピンを次のように接続してください:rsピン12へ、enピン11へ、d4ピン5に、d5ピン4へ、d6ピン3に、そしてd7ピン2に接続してください。すべての接続が確実に固定され、配線の緩みが発生しないようにしてください(動画の02:15参照)。

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

このプロジェクトのArduinoコードはLCDとSharp製の赤外線センサーを初期化します。主要な識別子には以下が含まれます:IRSharpの赤外線出力に使用されるアナログピンとして定義されており、およびmodelこれは使用されているSharpセンサーの種類を指定します。setup関数内でLCDがコンテンツを表示するように初期化されます。

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#define IR A0 // define Sharp IR signal pin
#define model 430 // the model of the IR module

この抜粋は、LiquidCrystalライブラリがどのように初期化され、IRセンサーのピンとモデルがどのように定義されているかを示しています。モデルは、使用している特定の種類のセンサーに対してライブラリが正しく動作することを保証するために重要です。

loop関数では、距離が継続的に測定され、LCDに表示されます。この関数SharpIR.distance()距離をセンチメートル単位で取得し、それを文字列に整形してLCDに表示します。

int dis = SharpIR.distance(); // gets the distance in cm
String distance = String(dis);
distance = "Distance: " + distance + "cm";
lcd.print(distance);

このコードスニペットはセンサーから距離の読み取り値を取得し、表示用に整形します。ディスプレイを継続的に更新することで、Sharpの赤外線センサーが測定した距離に関するリアルタイムのフィードバックを提供します。

デモンストレーション/何を期待できるか

配線とコードのアップロードが完了すると、LCDにSharpのIRセンサーが測定した距離が表示されるはずです。障害物をさまざまな距離に置いてセットアップをテストできます。測定値は4〜30センチメートルの指定範囲内で正確であるはずです。ただし、この範囲の極端な値では測定が信頼できなくなることがあります(ビデオでは04:50)。

動画のタイムスタンプ

  • 00:00はじめに
  • 午前02:15配線手順
  • 午前04:50セットアップの実演

画像

Sharp距離センサーとLCD1602-2のArduino配線図
Sharp距離センサーとLCD1602-2のArduino配線図
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
Sharpt IR distance sensor
Sharpt IR distance sensor GP2Y0A21YK0F
71-This is the Arduino code and video for a Sharp Infrared Sensor Module with LCD1602.
言語: C++
/*
 * Sharp IR (infrared) distance measurement module for Arduino
 * Measures the distance in cm and displays it on LCD1602 (without the I2C module)

 * Original library: https://github.com/guillaume-rico/SharpIR
 
 * Watch video instructions for this code: https://youtu.be/NjUOEAoKY7A
 * 
 * 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/>.

*/



// original source https://www.arduino.cc/en/Tutorial/HelloWorld
// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

///////////////////***** start of Sharp IR
//Sharp IR library source: https://github.com/guillaume-rico/SharpIR
#include <SharpIR.h>

#define IR A0 // define Sharp IR signal pin
#define model 430 // the model of the IR module
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:
/*
 * GP2Y0A02YK0F --> "20150"
 GP2Y0A21YK --> "1080"
 GP2Y0A710K0F --> "100500"
  GP2YA41SK0F --> "430"
 */
 
 

SharpIR SharpIR(IR, model);
/////////////////////**** end of Sharp IR

void setup() {
  // Robojax LCD1602 Test 
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  // Robojax Sharp IR with LCD1602 Test 
}

void loop() {
   lcd.clear();
  // Robojax.com Sharp IR Test 
    lcd.setCursor(0, 0);
    lcd.print("Robojax IR Test");
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
 int dis=SharpIR.distance();// gets the distance in cm
 String distance = String(dis);
 distance ="Distance: "+distance+"cm";
 lcd.print(distance); 



  delay(500);
  // Robojax.com Sharp IR Test  
}

リソースと参考文献

ファイル📁

Arduinoライブラリ(zip)

フリッツィングファイル