레슨 71: 거리 측정 및 LCD 화면에 표시하기 | 아두이노 단계별 강좌
이 프로젝트는 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 라이브러리를, LCD 제어를 위해 LiquidCrystal_I2C 라이브러리를 사용합니다(02:15 시점). 주요 구성 가능한 매개변수는 다음과 같습니다:
#define TRIGGER_PIN 12 // 초음파 센서의 트리거 핀에 연결된 Arduino 핀.
#define ECHO_PIN 11 // 초음파 센서의 에코 핀에 연결된 Arduino 핀.
#define MAX_DISTANCE 200 // 측정하려는 최대 거리(센티미터 단위).
// 16자 2줄 디스플레이의 LCD 주소를 0x3F로 설정
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2; // LCD용 추가 VCC
TRIGGER_PIN 및 ECHO_PIN은 초음파 센서에 연결된 Arduino 핀을 정의합니다. MAX_DISTANCE는 측정 가능한 최대 거리를 설정합니다. LCD 주소(0x3F)는 특정 LCD 모듈에 따라 조정이 필요할 수 있습니다. VCC2 핀은 LCD에 추가 5V 전원을 제공합니다.
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
자원 및 참고자료
아직 자원이 없습니다.
파일📁
파일이 없습니다.