1. 초음파 센서?

음파를 쏘아서 반향되어 수집되는 음파까지의 시간차로 거리를 계산해 내는데 사용되는 센서입니다. 음속이 340m/s 정도 되니까 센서를 통해 응답이 오는 시간만 알면 초음파 센서 앞에 있는 사물까지의 거리를 잴 수 있습니다.

측정거리 : 2cm ~ 5m, 측정각도 : 15′, 측정 해상도 : 3mm

30b8a9072b356d8d

2. 연결방법

초음파 센서를 보시면 4개의 핀이 있습니다. VCC, Trig, Echo, GND 입니다. 아두이노에 다음과 같이 연결합니다.

초음파 센서 아두이노
VCC 5V
Trig D2
Echo D3
GND GND

uwave_bb

3. 동작방법

아두이노에서 Trig(트리거) 핀으로 HIGH를 입력하면 초음파 모듈에서 40KHz 음파를 발사합니다.(10us 이상 HIGH 유지를 권장) 이때부터 Echo 핀은 High 상태가 되고, 음파가 되돌아와 수신되면 echo 핀이 다시 Low 상태가 됩니다. 이 간격에서 거리를 구하고 다시 2로 나누면 됩니다. (왕복이므로)

음파속도가 340m/s 이고 1cm 가는데 29us 가 걸립니다. 거리를 구하는 공식은

Distance = time / 29 / 2;

4. 코드 (스케치)

void setup()
{
Serial.begin(9600);
pinMode(2,OUTPUT); // 센서 Trig 핀
pinMode(3,INPUT); // 센서 Echo 핀
}

void loop()
{
long duration, cm;

  digitalWrite(2,HIGH); // 센서에 Trig 신호 입력
delayMicroseconds(10); // 10us 정도 유지
digitalWrite(2,LOW); // Trig 신호 off

  duration = pulseIn(3,HIGH); // Echo pin: HIGH->Low 간격을 측정
cm = microsecondsToCentimeters(duration); // 거리(cm)로 변환

  Serial.print(cm);
Serial.print(“cm”);
Serial.println();

  delay(300); // 0.3초 대기 후 다시 측정
}

long microsecondsToInches(long microseconds)
{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second).  This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

예제에 사용된 스케치는 아래에서 받으세요.메뉴 > 도구 > 시리얼 모니터 : 실행하셔서 측정된 거리 확인해보세요.

[wpdm_file id=7]