شِفر (کود) جستجو

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 با آردوینو
 * نوشته/به‌روزرسانی شده توسط احمد شمشیری برای Robojax (Robojax.com)
 * در 16 ژانویه 2019، ساعت 15:51 در آژاکس، انتاریو، کانادا
 * 
 * این کد "به همین شکل" و بدون ضمانت یا مسئولیت است. تا زمانی که این یادداشت را دست نخورده نگه دارید، استفاده از آن رایگان است.*
 * این کد از Robojax.com دانلود شده است.
 * این برنامه یک نرم‌افزار رایگان است: می‌توانید آن را تحت شرایط مجوز عمومی عمومی گنو که توسط بنیاد نرم‌افزار آزاد منتشر شده است، چه نسخه 3 مجوز، یا (به انتخاب شما) هر نسخه بعدی، مجدداً توزیع و/یا اصلاح کنید.
 * 
 * این برنامه به امید مفید بودن توزیع شده است، اما بدون هیچ ضمانتی؛ حتی بدون ضمانت ضمنی "قابلیت فروش یا مناسب بودن برای یک هدف خاص". برای جزئیات بیشتر به مجوز عمومی همگانی گنو مراجعه کنید.
 * 
 * شما باید یک نسخه از مجوز عمومی همگانی گنو را همراه با این برنامه دریافت کرده باشید. در غیر این صورت، به <https://www.gnu.org/licenses/> مراجعه کنید.
 */
#include <NewPing.h>

#define TRIGGER_PIN  12 // پین آردوینو به پین تریگر سنسور اولتراسونیک متصل شده است.
#define ECHO_PIN     11 // پین آردوینو به پین اکو روی سنسور اولتراسونیک متصل شده است.
#define MAX_DISTANCE 200 // حداکثر فاصله‌ای که می‌خواهیم پینگ کنیم (برحسب سانتی‌متر). حداکثر فاصله حسگر ۴۰۰ تا ۵۰۰ سانتی‌متر است.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // تنظیمات جدید پینگ پین‌ها و حداکثر فاصله.

 // شروع تنظیمات برای LCD1602-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
 // برای نمایش ۱۶ کاراکتری و ۲ خطی، آدرس LCD را روی ۰x3F تنظیم کنید.
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2;
 // پایان تنظیمات برای LCD1602-I2C

void setup() {
  Serial.begin(9600); // برای مشاهده نتایج پینگ، سریال مانیتور را با سرعت ۱۹۶۰۰ باود باز کنید.

  pinMode(VCC2, OUTPUT); // VCC اضافی برای LCD
  digitalWrite(VCC2, HIGH); // VCC اضافی اکنون بالا است (5 ولت)
 // مقداردهی اولیه LCD،
  lcd.begin();
 // چراغ سیاه را روشن کنید و یک پیام چاپ کنید.
  lcd.backlight();
  lcd.clear();
  lcd.setCursor (0,0);
  lcd.print("Robojax LCD1602");

}

void loop() {
  delay(50); // بین هر پینگ ۵۰ میلی‌ثانیه صبر کنید (حدود ۲۰ پینگ در ثانیه). کمترین زمان تأخیر بین هر پینگ باید ۲۹ میلی‌ثانیه باشد.
   lcd.clear(); // پاک کردن مقادیر قبلی از صفحه نمایش
  lcd.setCursor (0,0); // کاراکتر صفر، خط ۱
  lcd.print("LCD1602 HC-SR04"); // متن را چاپ کنید

 // فاصله چاپ (سانتی متر)
  lcd.setCursor (0,1); // کاراکتر ۰، خط ۲
  lcd.print(sonar.ping_cm()); // فاصله چاپ (سانتی متر)
  lcd.setCursor (3,1); // کاراکتر ۴، خط ۲
  lcd.print("cm"); // "سانتی متر" را روی صفحه نمایش چاپ کن

 // فاصله چاپ بر حسب اینچ
  lcd.setCursor (7,1); // کاراکتر ۸، خط ۲
  lcd.print(sonar.ping_in()); // فاصله چاپ (سانتی متر)
  lcd.setCursor (9,1); // کاراکتر ۴، خط ۲
  lcd.print("inches"); // "سانتی متر" را روی صفحه نمایش چاپ کن

  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // ارسال پینگ، دریافت فاصله بر حسب سانتی‌متر و چاپ نتیجه (0 = خارج از محدوده فاصله تنظیم شده)
  Serial.print("cm");
  Serial.print(", ");
  Serial.print(sonar.ping_in());
  Serial.println("in");
  int distance = sonar.ping_cm();

}

مواردی که ممکن است به آن‌ها نیاز داشته باشید

منابع و مراجع

هنوز هیچ منبعی موجود نیست.

فایل‌ها📁

هیچ فایلی موجود نیست.