検索コード

Lesson 71: Measure distance and display on LCD screen | Arduino Step By Step Course

Lesson 71: Measure distance and display on LCD screen | Arduino Step By Step Course

This project demonstrates how to combine an HC-SR04 ultrasonic sensor with an LCD1602 display to measure and display distances on the Arduino. This setup is useful for various applications requiring proximity detection and visual feedback.

Practical Applications:

  • Robotics: Measuring distances for obstacle avoidance or navigation.
  • Parking Assistance: Creating a simple parking distance sensor.
  • Home Automation: Building a proximity-based lighting system.
  • Industrial Automation: Monitoring distances in manufacturing processes.

Hardware/Components

To build this project, you will need the following components:

  • Arduino Uno (or compatible board)
  • HC-SR04 Ultrasonic Sensor
  • LCD 1602 Display (I2C version recommended)
  • Jumper Wires

Wiring Guide

The wiring is described in the video (in video at 00:31). The key connections are:

  • Ultrasonic Sensor: VCC to 5V, GND to GND, TRIG to pin 12, ECHO to pin 11.
  • LCD 1602: VCC to 5V, GND to GND, SDA to pin A4, SCL to pin A5.

%%WIRING%%

Code Explanation

The code utilizes the NewPing library for ultrasonic distance measurement and the LiquidCrystal_I2C library for LCD control (in video at 02:15). The key configurable parameters are:


#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
// Set the LCD address to 0x3F for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2; //extra VCC for LCD

The TRIGGER_PIN and ECHO_PIN define the Arduino pins connected to the ultrasonic sensor. MAX_DISTANCE sets the maximum measurable distance. The LCD address (0x3F) might need adjustment depending on your specific LCD module. The VCC2 pin provides an additional 5V supply for the LCD.

The sonar.ping_cm() and sonar.ping_in() functions are used to obtain distance measurements in centimeters and inches respectively (in video at 05:27 and 06:01).

Live Project/Demonstration

A demonstration of the project is shown in the video (in video at 06:42). The video shows the sensor measuring distances to various objects and displaying the results in centimeters and inches on the LCD screen. The serial monitor displays the same measurements, providing a means to verify the readings.

Chapters

  • [00:04] Introduction and Project Overview
  • [00:31] Wiring Diagram and Connections
  • [02:15] Code Explanation: Ultrasonic Sensor Setup
  • [03:19] Code Explanation: LCD1602 Setup
  • [04:21] Code Explanation: Arduino Setup
  • [04:46] Code Explanation: Main Loop and Display
  • [06:42] Live Demonstration
  • [07:26] Conclusion
788-Using LCD1602-I2C and HC-SR04 Ultrasonic Sensor with Arduino
言語: C++
/*
 * S08-03  
 * LCD1602-I2CとHC-SR04超音波センサーをArduinoで使用する  
 * Robojax(Robojax.com)のためにアフマド・シャムシリが執筆/更新  
 * 2019年1月16日15:51にカナダ・オンタリオ州エイジャックスで  
 * 
 * このコードは「現状のまま」で提供され、保証または責任はありません。このメモをそのまま保持する限り、自由に使用できます。*  
 * このコードはRobojax.comからダウンロードされました。  
 * このプログラムはフリーソフトウェアです:GNU一般公衆ライセンスの条件の下で再配布および/または修正できます。  
 * ライセンスのバージョン3、または(あなたの選択により)以降のバージョンのいずれかで公開されたFree Software Foundationによって。  
 * 
 * このプログラムは有用であることを願って配布されますが、いかなる保証もありません。特定の目的に対する商品性または適合性を含む暗黙の保証さえありません。詳細についてはGNU一般公衆ライセンスを参照してください。  
 * 
 * このプログラムと一緒にGNU一般公衆ライセンスのコピーを受け取っているはずです。受け取っていない場合は、<https://www.gnu.org/licenses/>を参照してください。
 */
#include <NewPing.h>

#define TRIGGER_PIN  12 // 超音波センサーのトリガーピンに接続されたArduinoピン。
#define ECHO_PIN     11 // 超音波センサーのエコーピンに接続されたArduinoピン。
#define MAX_DISTANCE 200 // 最大でペイングしたい距離(センチメートル単位)。最大センサー距離は400〜500cmに評価されています。

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPingのピンの設定と最大距離。

 // LCD1602-I2Cの設定開始
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
 // 16文字2行のディスプレイ用にLCDアドレスを0x3Fに設定します。
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2;
 // LCD1602-I2Cの設定の終了

void setup() {
  Serial.begin(9600); // 19600ボーでシリアルモニターを開いて、ピングの結果を確認してください。

  pinMode(VCC2, OUTPUT); // LCD用の追加VCC
  digitalWrite(VCC2, HIGH); // エクストラ VCC は現在 HIGH (5V) です。
 // LCDを初期化する、
  lcd.begin();
 // ブラックライトをオンにし、メッセージを印刷します。
  lcd.backlight();
  lcd.clear();
  lcd.setCursor (0,0);
  lcd.print("Robojax LCD1602");

}

void loop() {
  delay(50); // ピングの間に50ミリ秒待機します(約20ピング/秒)。29ミリ秒がピング間の最短遅延であるべきです。
   lcd.clear(); // 画面から以前の値をクリアする
  lcd.setCursor (0,0); // キャラクターゼロ、ライン1
  lcd.print("LCD1602 HC-SR04"); // テキストを印刷する

 // センチメートルで距離を表示する
  lcd.setCursor (0,1); // キャラクター0、行2
  lcd.print(sonar.ping_cm()); // センチメートルで距離を表示する
  lcd.setCursor (3,1); // キャラクター4、行2
  lcd.print("cm"); // ディスプレイに「cm」と表示する

 // インチで距離を表示する
  lcd.setCursor (7,1); // キャラクター8、行2
  lcd.print(sonar.ping_in()); // センチメートルで距離を表示する
  lcd.setCursor (9,1); // キャラクター4、行2
  lcd.print("inches"); // ディスプレイに「cm」と表示する

  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // pingを送信し、cm単位で距離を取得して結果を表示します(0 = 設定した距離範囲外)。
  Serial.print("cm");
  Serial.print(", ");
  Serial.print(sonar.ping_in());
  Serial.println("in");
  int distance = sonar.ping_cm();

}

必要かもしれないもの

リソースと参考文献

まだリソースはありません。

ファイル📁

ファイルは利用できません。