# 명칭
Ultrasonic Sensor, 초음파 센서
# 기능
초음파를 발생시켜 반사되어 돌아오는 음파를 측정하여 거리를 계산하는 센서
음속은 대략 340m/s
# 사용 예
초음파 센서로 거리 측정하기
## 회로도
## 코드
int trigPin = 3;
int echoPin = 2;
void setup() {
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
long duration, cm;
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
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;
}
# 코드 (codeiPlus)
# 참고자료
* Ping Ultrasonic Range Finder : https://www.arduino.cc/en/Tutorial/Ping
* 초음파 센서로 거리재기 예제 : http://www.hardcopyworld.com/ngine/aduino/index.php/archives/104
# 3핀 초음파센서
http://codesforprogram.blogspot.kr/2013/08/3-pin-ultrasonic-sensor-and-arduino.html
PING))) Ultrasonic Distance Sensor : https://www.parallax.com/product/28015
## 예제
서보모터에 연결된 초음파 센서를 좌우로 움직이기