今天本来准备给之前的蓝牙遥控小车增加超声波壁障功能,可是手头有两款超声波模块,一个是大家最常见的的4pin HC-SR04,还有一款是3pin的SDM-IO。
在纠结使用哪款之前我决定先做个比较。
以下是本次对比用到的配件:
HC-SR04代码: - const int TrigPin = 2;
- const int EchoPin = 3;
- float cm;
- void setup()
- {
- Serial.begin(9600);
- pinMode(TrigPin, OUTPUT);
- pinMode(EchoPin, INPUT);
- }
- void loop()
- {
- digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
- delayMicroseconds(2);
- digitalWrite(TrigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(TrigPin, LOW);
- cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
- cm = (int(cm * 100.0)) / 100.0; //保留两位小数
- Serial.println("sc;");
- Serial.println("sd0,0;");
- Serial.println("ssHC-SR04:;");
- Serial.println("sd0,8;");
- Serial.print("ss");
- Serial.print(cm);
- Serial.println(";");
- Serial.println("ssCM;");
- delay(1000);
- }
复制代码SDM-IO代码: - int pin = 8; // connected to the Trig on the module, this serves as both Trigger and Echo
- unsigned long time;
- unsigned long sizeofpulse;
- float cm;
- void setup()
- {
- Serial.begin(9600);
- pinMode(pin, OUTPUT);
- digitalWrite(pin, HIGH); // Trig pin is normally HIGH
- }
- void loop()
- {
- pinMode(pin, OUTPUT); //return digital pin to OUTPUT mode after reading
- digitalWrite(pin, LOW);
- delayMicroseconds(25);
- digitalWrite(pin, HIGH); //Trig pin pulsed LOW for 25usec
- time = micros(); //record timer
- pinMode(pin, INPUT); //change pin to INPUT to read the echo pulse
- sizeofpulse = pulseIn(pin, LOW, 18000); //should be approx 150usec, timeout at 18msec
- time = micros() - time - sizeofpulse; // amount of time elapsed since we sent the trigger pulse and detect the echo pulse, then subtract the size of the echo pulse
- cm=(time*340.29/2/10000)-3; // convert to distance in centimeters
- Serial.println("sc;");
- Serial.println("sd0,0;");
- Serial.println("ssSDM-IO:;");
- // Serial.println("sd1,0;");
- Serial.print("ss");
- Serial.print(cm);
- Serial.println(";");
- Serial.println("ssCM;");
- delay(1000);
- }
复制代码硬件连接和运行效果如下:
接下来开始对比,先把两款超声波模块固定在同一水平面。如下图: 用杜邦线试了一下,HC-SR04几乎检测不到,偶尔会有变化。SDM-IO测试稳定,上下移动均能检测到杜邦线。
以下是测试视频,用了一根筷子,呵呵:
http://player.youku.com/player.php/sid/XNzcwMzE0NzMy/v.swf
以上对比测试只是简单测试,可以看出,SDM-IO更灵敏,无盲区。当然这两款的价格还是有差距的。根据个人需要选择了。相关产品可以查阅以下资料。
HC-SR04:http://pan.baidu.com/s/1sjv0tK5 SDM-IO:http://pan.baidu.com/s/1ixG1S 串口1602LCD:http://pan.baidu.com/s/1pJ8vAxt
|