
前阵子你可能见过叫Tacit的Project,一个为盲人设计的声纳手套。原型用Arduino Mini,超声波传感器(Ultrasonic Sensor)和伺服电机搭建。头部的超声波传感器(Ultrasonic Sensor)不断地向两个方向发射和接受超声波,并将测得的距离值返回给Arduino处理,然后MCU根据处理得知的远近关系通过Servo力反馈(轻重)给手部。原理简单又有爱,该Project已经开源,有兴趣的同学可以自己动手做一下。

今天着重介绍的就是超声波传感器(Ultrasonic Sensor),它是测距常用模块。普通的超声波传感器测距范围大约是2cm到3-5m。传感器模块的工作原理大致是这样的:

- 模块收到10us以上的高电平脉冲触发信号;
- 模块自动发送8个40KHz的方波并检测回波;
- 若有回波信号返回,通过IO输出一高电平,高电平持续的时间就是超声波从发射到返回的时间.测试距离=(高电平时间*声速(340m/s))/2

常见的超声波传感器有3脚的,4脚和5脚的:
3脚的比如Parallax的 PING))),三脚分别是5v电源脚(5V),I/O信号端(SIG),地端(GND)。

4脚的比如HC-SR04,四个脚分别是5v电源脚(Vcc),触发控制端(Trig),接收端(Echo),地端(GND)。5脚的比4脚的多出一个OUT 脚,是防盗模块时的开关量输出脚,测距模块不用此脚。


下面我们就以HC-SR04为例来实验一下。如果你用的是3脚的可参考Arduino官网的教程。
实验目的
利用超声波传感器(Ultrasonic Sensor)测量距离
实验材料
Arduino 1块
超声波传感器(Ultrasonic Sensor) 1个
面包板 1块 (可选)
接线 若干
接线图

电路图

代码
/* HC-SR04 Ultrasonic Sensor
This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* Vcc connection of the HC-SR04 attached to +5V
* GND connection of the HC-SR04 attached to ground
* Echo connection of the HC-SR04 attached to digital pin 3
* Trig connection of the HC-SR04 attached to digital pin 2
*/
// this constant won't change. It's the pin number
// of the sensor's output:
const int TrigPin = 2;
const int EchoPin = 3;
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in centimeters:
long duration, cm;
// The HC-SR04 is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(TrigPin, OUTPUT);
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(5);
digitalWrite(TrigPin, LOW);
// The same pin is used to read the signal from the HC-SR04: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(EchoPin, INPUT);
duration = pulseIn(EchoPin, HIGH);
// convert the time into a distance
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;
}

打开Serial Monitor就可以看见一串距离值了。实例的视频就不做了,下面是Make Magzine针对PING)))的视频教程,供大家参考。
| 无觅猜您也喜欢: | |||
![]() 【Arduino学习笔记】4.通过按钮控制LED |
![]() Arduino UNO Rev. 3 |
![]() 【Arduino学习笔记】10. 2×16 LCD显示内容 |
![]() 【Arduino学习笔记】5.按钮去抖Debounce |
| 无觅 | |||




