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超声波传感器与Arduino
* 由Ahmad Shamshiri为Robojax(Robojax.com)撰写/更新
* 日期:2019年1月16日,15:51,地点:加拿大安大略省阿贾克斯
*
* 此代码“按现状”提供,不附带任何担保或责任。只要您保留此说明,便可自由使用。*
* 此代码已从Robojax.com下载
* 此程序是自由软件:您可以根据自由软件基金会发布的GNU通用公共许可证的条款重新分发和/或修改该程序,许可证版本为3,或(根据您的选择)任何较新版本。
*
* 此程序的发布寄希望于它将会有用,
* 但不附带任何担保;甚至没有关于适销性或适合特定目的的隐含担保。有关更多详细信息,请参见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-500厘米。
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing引脚设置和最大距离。
// LCD1602-I2C设置开始
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// 将LCD地址设置为0x3F,用于16字符和2行显示器
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 现在是高电平 (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); // 字符零,第一行
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()); // 发送信号,获取距离(单位:厘米)并打印结果(0 = 超出设定距离范围)
Serial.print("cm");
Serial.print(", ");
Serial.print(sonar.ping_in());
Serial.println("in");
int distance = sonar.ping_cm();
}
|||您可能需要的东西
-
易趣从eBay购买HC-SR04超声波传感器ebay.us
-
易趣在eBay上购买LCD1602-I2Cebay.us
-
全球速卖通在AliExpress购买LCD1602-I2Cs.click.aliexpress.com
-
Banggood从Banggood购买HC-SR04超声波传感器banggood.com
资源与参考
尚无可用资源。
文件📁
没有可用的文件。