บทเรียนที่ 71: วัดระยะทางและแสดงผลบนหน้าจอ LCD | คอร์สเรียน Arduino ทีละขั้นตอน
โปรเจกต์นี้สาธิตวิธีการรวมเซนเซอร์อัลตราโซนิก HC-SR04 เข้ากับจอแสดงผล LCD1602 เพื่อวัดและแสดงระยะทางบน Arduino การตั้งค่านี้มีประโยชน์สำหรับการใช้งานต่างๆ ที่ต้องการการตรวจจับระยะใกล้และการแสดงผลด้วยภาพ
การประยุกต์ใช้งานจริง:
- หุ่นยนต์: การวัดระยะทางสำหรับการหลบหลีกสิ่งกีดขวางหรือการนำทาง
- ระบบช่วยจอดรถ: การสร้างเซนเซอร์วัดระยะจอดรถอย่างง่าย
- ระบบอัตโนมัติภายในบ้าน: การสร้างระบบไฟที่ทำงานตามระยะใกล้
- ระบบอัตโนมัติในอุตสาหกรรม: การตรวจสอบระยะทางในกระบวนการผลิต
ฮาร์ดแวร์/อุปกรณ์
ในการสร้างโปรเจกต์นี้ คุณจะต้องมีอุปกรณ์ดังต่อไปนี้:
- Arduino Uno (หรือบอร์ดที่เข้ากันได้)
- เซนเซอร์อัลตราโซนิก HC-SR04
- จอแสดงผล LCD 1602 (แนะนำรุ่น I2C)
- สายจัมเปอร์
คู่มือการเดินสายไฟ
การเดินสายไฟได้อธิบายไว้ในวิดีโอ (ในวิดีโอที่ 00:31) การเชื่อมต่อหลักคือ:
- เซนเซอร์อัลตราโซนิก: VCC ไปยัง 5V, GND ไปยัง GND, TRIG ไปยังพิน 12, ECHO ไปยังพิน 11
- LCD 1602: VCC ไปยัง 5V, GND ไปยัง GND, SDA ไปยังพิน A4, SCL ไปยังพิน A5
%%WIRING%%
คำอธิบายโค้ด
โค้ดใช้ไลบรารี NewPing สำหรับการวัดระยะทางด้วยอัลตราโซนิก และไลบรารี LiquidCrystal_I2C สำหรับควบคุม LCD (ในวิดีโอที่ 02:15) พารามิเตอร์หลักที่สามารถกำหนดค่าได้คือ:
#define TRIGGER_PIN 12 // พิน Arduino ที่เชื่อมต่อกับพิน trigger บนเซนเซอร์อัลตราโซนิก
#define ECHO_PIN 11 // พิน Arduino ที่เชื่อมต่อกับพิน echo บนเซนเซอร์อัลตราโซนิก
#define MAX_DISTANCE 200 // ระยะทางสูงสุดที่ต้องการวัด (เป็นเซนติเมตร)
// กำหนดที่อยู่ LCD เป็น 0x3F สำหรับจอแสดงผล 16 ตัวอักษรและ 2 บรรทัด
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2; // VCC เพิ่มเติมสำหรับ LCD
TRIGGER_PIN และ ECHO_PIN กำหนดพิน Arduino ที่เชื่อมต่อกับเซนเซอร์อัลตราโซนิก MAX_DISTANCE กำหนดระยะทางสูงสุดที่วัดได้ ที่อยู่ LCD (0x3F) อาจต้องปรับเปลี่ยนขึ้นอยู่กับโมดูล LCD เฉพาะของคุณ พิน VCC2 ให้แหล่งจ่ายไฟ 5V เพิ่มเติมสำหรับ LCD
ฟังก์ชัน sonar.ping_cm() และ sonar.ping_in() ใช้เพื่อรับค่าการวัดระยะทางเป็นเซนติเมตรและนิ้วตามลำดับ (ในวิดีโอที่ 05:27 และ 06:01)
โปรเจกต์สด/การสาธิต
การสาธิตโปรเจกต์แสดงในวิดีโอ (ในวิดีโอที่ 06:42) วิดีโอแสดงเซนเซอร์วัดระยะทางไปยังวัตถุต่างๆ และแสดงผลลัพธ์เป็นเซนติเมตรและนิ้วบนหน้าจอ LCD จอภาพอนุกรมแสดงการวัดเดียวกัน เพื่อให้สามารถตรวจสอบค่าที่อ่านได้
บทต่างๆ
- [00:04] บทนำและภาพรวมโปรเจกต์
- [00:31] แผนผังการเดินสายไฟและการเชื่อมต่อ
- [02:15] คำอธิบายโค้ด: การตั้งค่าเซนเซอร์อัลตราโซนิก
- [03:19] คำอธิบายโค้ด: การตั้งค่า LCD1602
- [04:21] คำอธิบายโค้ด: การตั้งค่า Arduino
- [04:46] คำอธิบายโค้ด: ลูปหลักและการแสดงผล
- [06:42] การสาธิตสด
- [07:26] บทสรุป
/*
* S08-03
* Using LCD1602-I2C and HC-SR04 Ultrasonic Sensor with Arduino
* Written/updated by Ahmad Shamshiri for Robojax (Robojax.com)
* on January 16, 2019 at 15:51 in Ajax, Ontario, Canada
* 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 download 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/>.
*/
#include <NewPing.h>
#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). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// start of settings for LCD1602-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x3F for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2;
// end of settings for LCD1602-I2C
void setup() {
Serial.begin(9600); // Open serial monitor at 19600 baud to see ping results.
pinMode(VCC2, OUTPUT);// extra VCC for LCD
digitalWrite(VCC2, HIGH);//extra VCC is now HIGH (5V)
// initialize the LCD,
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0); //
lcd.print("Robojax LCD1602");
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
lcd.clear();// clearn previous values from screen
lcd.setCursor (0,0); //character zero, line 1
lcd.print("LCD1602 HC-SR04"); // print text
// print distance in cm
lcd.setCursor (0,1); //character 0, line 2
lcd.print(sonar.ping_cm());// print distance in cm
lcd.setCursor (3,1); //character 4, line 2
lcd.print("cm");// print "cm" on the display
// print distance in inch
lcd.setCursor (7,1); //character 8, line 2
lcd.print(sonar.ping_in());// print distance in cm
lcd.setCursor (9,1); //character 4, line 2
lcd.print("inches");// print "cm" on the display
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.print("cm");
Serial.print(", ");
Serial.print(sonar.ping_in());
Serial.println("in");
int distance = sonar.ping_cm();
}
สิ่งที่คุณอาจต้องการ
-
อีเบย์
-
อีเบย์Purchase LCD1602-I2C from eBayebay.us
-
อาลีเอ็กซ์เพรสPurchase LCD1602-I2C from AliExpresss.click.aliexpress.com
-
แบงกูดPurchase HC-SR04 Ultrasonic Sensor from Banggoodbanggood.com
แหล่งข้อมูลและเอกสารอ้างอิง
ยังไม่มีทรัพยากรเลย
ไฟล์📁
ไม่มีไฟล์ที่พร้อมใช้งาน