https://fishpoint.tistory.com/9791
HC-SR04 초음파 거리 센서와 함께 ESP32 사용
이 글에서는 거리 센서 HC-SR04 센서를 ESP32에 연결하고 사용하는 방법에 대해 설명합니다. 왜 필요한가요? 가까이 다가가면 불이 들어오는 광고판이나 가까이 가면 뚜껑이 열리는 지능형 쓰레기
fishpoint.tistory.com
이 글에서는 거리 센서 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);
}
'경로 및 정보' 카테고리의 다른 글
| CP2102 USB to UART Bridge Controller Driver ( 드라이버 ) (0) | 2025.11.24 |
|---|---|
| AD(Active Directory) 공격 (0) | 2025.10.07 |
| Fileuplad via Stored XSS (0) | 2025.10.07 |
| Powershell 파워쉘 실행정책 - Execution Policy (0) | 2025.10.05 |
| CVE-2024-38819 (0) | 2025.09.08 |
