관리 메뉴

Kim's Programming

아두이노 초음파 센서의 이용 본문

Arduino/부품 이야기

아두이노 초음파 센서의 이용

Programmer. 2015. 7. 1. 14:20

 

이번 포스팅에서는 초음파 센서에 대해서 알아 보겠습니다.

 

초음파센서의 기본 작동은

 

초음파를 쏴주고 되돌아오는 반사파를 읽어내는 것입니다.

 

초음파 센서는 올바르게 연결 후 소스코드를 활용하여 거리를 쟤게 되는데요

 

일단 소스코드 보고 가겠습니다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#define Echo    6
#define Trigger    7
 
 
 
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;
}
 
 
 
void setup()
 
{
 
Serial.begin(9600);
 
}
 
 
 
void loop()
 
{
 
int inches,cm,value;
 digitalWrite(Trigger, HIGH); //초음파를 발생.
 delayMicroseconds(10);
 digitalWrite(Trigger, LOW); //초음파의 발생 정지.
 value=pulseIn(Echo, HIGH);
 inches=microsecondsToInches(value);//인치 환산
 
 cm=microsecondsToCentimeters(value);//센치미터 환산
 
 Serial.println("==================\n");
 Serial.print("Distance(inches) = ");
 Serial.println(inches);
 
Serial.print("\n\n");
 
Serial.print("Distance(cm) = ");
 Serial.println(cm);
 Serial.print("\n\n");
 
 Serial.println("==================\n");
 delay(1000);
 
}
 
 
cs

 

그대로 이용하시면 거리를 측정 하실 수 있습니다.

 

원리는 다음과 같습니다.

 

소리의 속도는 340m/s입니다 이를 환산하여 걸리는 시간을 이용 거리를 쟤는 원리입니다.

 

직접 실험해 보았습니다.

 

간편하게 초음파 센서를 연결하고 실험해보았습니다.

 

 

회로는 다음과 같이 연결해 주시면 됩니다. 위의 Trig 와 Echo는 그림상에서는 2,3번에 연결했지만 소스코드를 바꾸시거나

 

또는 위에 소스코드에 맞게 연결해주시면 됩니다. 전 위의 소스코드에 따라 6번과 7번에 연결하였습니다.

 

 

직접 연결한 모습입니다.

 

serial로 출력하게 만들어 거리가 직접 재어 집니다.

 

하지만 주의할점이 있습니다. 초음파는 직선으로 가서 직선으로 오는것이 아닙니다.

 

초음파가 발사될때 부채꼴로 나아간다음 반사되는 음파가 들어오는것이므로

 

정확도가 매우 좋은 편은 아닙니다.

 

위의 결과 사진을 보시면 중간에 뜬금없는 147inch에 375cm값이 나온게 있습니다.

 

이는 소위 말하는 값이 튄 경우이며 정면이 아닌 더 먼곳에서 반사되어 거리가 측정되에 값이 나온 것입니다.

 

 

'Arduino > 부품 이야기' 카테고리의 다른 글

아두이노 Servo 모터 무한회전 개조  (0) 2015.07.18
아두이노 부트로더 단독 보드 복구  (18) 2015.07.06
아두이노 servo 모터  (1) 2015.07.01
LB1630 드라이버 IC와 작동  (0) 2015.06.30
적외선 센서와 ADC  (0) 2015.06.30