본문 바로가기

ESP32

HC-SR04 초음파 거리 센서와 함께 ESP32 사용

반응형

 

 

이 글에서는 거리 센서 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);
}

 

 

간략하게 설명했습니다. 기사 원문을 참고하려면 이 링크를 따라가세요. 배움을 멈추지 마세요.

 

 

반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

카카오 채널 추가하기

카톡 채팅방에서 무엇이든 물어보세요

당신의 성공을 위해 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩