반응형
이 글에서는 거리 센서 HC-SR04 센서를 ESP32에 연결하고 사용하는 방법에 대해 설명합니다. 왜 필요한가요? 가까이 다가가면 불이 들어오는 광고판이나 가까이 가면 뚜껑이 열리는 지능형 쓰레기통을 본 적이 있으신가요?또는 쇼핑몰에서 충돌을 지능적으로 피하는 로봇도 거의 모두 초음파 센서를 사용합니다.

연결 회로

실습 코드
#define echoPin 18 // attach pin ESP32 GPIO18 to pin Echo of HC-SR04
#define trigPin 5 // attach pin ESP32 GPIO5 to pin Trig of HC-SR04
long duration; // Variable to store time taken to the pulse
// to reach the receiver
int distance; // Variable to store distance calculated using
// formula
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
// Serial Communication is starting with 9600 of
// baudrate speed
Serial.begin(9600);
// The text to be printed in the serial monitor
Serial.println("Distance measurement using ESP32");
delay(500);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // wait for 2 ms to avoid
// collision in the serial monitor
digitalWrite(trigPin, HIGH); // turn on the Trigger to generate pulse
delayMicroseconds(10); // keep the trigger "ON" for 10 ms to generate
// pulse for 10 ms.
digitalWrite(trigPin, LOW); // Turn off the pulse trigger to stop
// pulse generation
// If pulse reached the receiver echoPin
// become high Then pulseIn() returns the
// time taken by the pulse to reach the
// receiver
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0344 / 2; // Expression to calculate
// distance using time
Serial.print("Distance: ");
Serial.print(distance); // Print the output in serial monitor
Serial.println(" cm");
delay(100);
}
간략하게 설명했습니다. 기사 원문을 참고하려면 이 링크를 따라가세요. 배움을 멈추지 마세요.
반응형
'ESP32' 카테고리의 다른 글
| ESP32 아두이노 IDE 에서 사용하기 (3) | 2024.10.05 |
|---|---|
| ESP32-C3FH4 BLE5.0/WiFi 보드 (3) | 2024.09.30 |
| ESP32로 MP3 파일을 재생하는 방법 (2) | 2024.09.25 |
| ESP32 및 DS18B20 디지털 1-wire 온도계 인터페이스하기 (2) | 2024.09.25 |
| Arduino IDE로 ESP32를 프로그래밍하는 방법 (5) | 2024.09.23 |
| ESP32에 I2C LCD를 연결하는 방법 (1) | 2024.09.20 |
| ESP32 블루투스를 스마트폰과 연결하는 방법 SerialToSerialBT (10) | 2024.09.19 |
| ESP32 Bluetooth Low Energy(BLE) 시작하기 (14) | 2024.09.18 |
취업, 창업의 막막함, 외주 관리, 제품 부재!
당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약,
아이디어는 있지만 구현할 기술이 없는 막막함.
우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.
이제 고민을 멈추고, 캐어랩을 만나세요!
코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.
제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!
귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.
캐어랩