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
/*
* S08-03
* استخدام مستشعر الموجات فوق الصوتية LCD1602-I2C وHC-SR04 مع أردوينو
* كتبه/حدّثه أحمد شمشيري لروبوجاكس (Robojax.com)
* في ١٦ يناير ٢٠١٩، الساعة ٣:٥١ مساءً، في أجاكس، أونتاريو، كندا
*
* هذا الكود متوفر "كما هو" دون أي ضمان أو مسؤولية. الاستخدام مجاني طالما حافظت على هذه الملاحظة سليمة.*
* تم تنزيل هذا الكود من Robojax.com
*
* هذا البرنامج مجاني: يمكنك إعادة توزيعه و/أو تعديله بموجب شروط رخصة جنو العمومية كما نشرتها مؤسسة البرمجيات الحرة، إما الإصدار ٣ من الرخصة، أو (حسب رغبتك) أي إصدار لاحق.
*
* يُوزع هذا البرنامج على أمل أن يكون مفيدًا، ولكن دون أي ضمان؛ دون أي ضمان ضمني لقابلية التسويق أو الملاءمة لغرض معين. راجع رخصة جنو العمومية لمزيد من التفاصيل.
*
* من المفترض أن تكون قد استلمت نسخة من رخصة جنو العمومية مع هذا البرنامج. إذا لم تكن قد استلمتها، فراجع <https://www.gnu.org/licenses/>.
*/
#include <NewPing.h>
#define TRIGGER_PIN 12 // دبوس Arduino مرتبط بدبوس الزناد على مستشعر الموجات فوق الصوتية.
#define ECHO_PIN 11 // دبوس Arduino مرتبط بدبوس الصدى على مستشعر الموجات فوق الصوتية.
#define MAX_DISTANCE 200 // أقصى مسافة نرغب في إرسال إشارة لها (بالسنتيمترات). أقصى مسافة للمستشعر هي ٤٠٠-٥٠٠ سم.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // إعداد Ping الجديد للدبابيس والمسافة القصوى.
// بدء الإعدادات لـ LCD1602-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// اضبط عنوان LCD على 0x3F لعرض مكون من 16 حرفًا وسطرين
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2;
// نهاية الإعدادات لـ LCD1602-I2C
void setup() {
Serial.begin(9600); // افتح شاشة التسلسل بسرعة 19600 بود لرؤية نتائج ping.
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); // انتظر لمدة 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"); // اطبع "سم" على الشاشة
// مسافة الطباعة بالبوصة
lcd.setCursor (7,1); // الحرف 8، السطر 2
lcd.print(sonar.ping_in()); // مسافة الطباعة بالسنتيمتر
lcd.setCursor (9,1); // الحرف 4، السطر 2
lcd.print("inches"); // اطبع "سم" على الشاشة
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // إرسال إشارة ping، والحصول على المسافة بالسنتيمتر وطباعة النتيجة (0 = خارج نطاق المسافة المحددة)
Serial.print("cm");
Serial.print(", ");
Serial.print(sonar.ping_in());
Serial.println("in");
int distance = sonar.ping_cm();
}
الأشياء التي قد تحتاجها
-
إي باي
-
إي بايشراء LCD1602-I2C من eBayebay.us
-
علي إكسبريساشترِ LCD1602-I2C من علي إكسبريسs.click.aliexpress.com
-
بانجوداشترِ مستشعر الموجات فوق الصوتية HC-SR04 من بانجودbanggood.com
الموارد والمراجع
لا توجد موارد حتى الآن.
ملفات📁
لا توجد ملفات متاحة.