以上时序图表明你只需要提供一个 10uS 以上脉冲触发信号,该模块内部将发出 8 个 40kHz 周期电平并检测回波。一旦检测到有回波信号则输出回响信号。回响信号的脉冲宽度与所测的距离成正比。由此通过发射信号到收到的回响信号时间间隔可以计算得到距离。公式:uS/58=厘米或者 uS/148=英寸;或是:距离=高电平时间*声速(340M/S)/2;建议测量周期为 60ms 以上,以防止发射信号对回响信号的影响。
参考别人库进行修改与调整
SR04.CPP
- #include "SR04.h"
- /* Ping))) Sensor
- This sketch reads a PING))) 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:
- * +V connection of the PING))) attached to +5V
- * GND connection of the PING))) attached to ground
- * SIG connection of the PING))) attached to digital pin 7
- http://www.arduino.cc/en/Tutorial/Ping
- created 3 Nov 2008
- by David A. Mellis
- modified 30 Aug 2011
- by Tom Igoe
- This example code is in the public domain.
- */
- SR04::SR04(int echoPin, int triggerPin) {
- _echoPin = echoPin;
- _triggerPin = triggerPin;
- pinMode(_echoPin, INPUT);
- pinMode(_triggerPin, OUTPUT);
- _autoMode = false;
- _distance = 999;
- }
- long SR04::Distance() {
- long d = 0;
- _duration = 0;
- // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
- // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
- digitalWrite(_triggerPin, LOW);
- delayMicroseconds(2);
- digitalWrite(_triggerPin, HIGH);
- delayMicroseconds(DEFAULT_PINGS); //@1696933323
- digitalWrite(_triggerPin, LOW);
- delayMicroseconds(2);//@1696933323
-
- // The same pin is used to read the signal from the PING))): 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.
- _duration = pulseIn(_echoPin, HIGH, PULSE_TIMEOUT);
- //_duration = pulseIn(_echoPin, HIGH);
- d = MicrosecondsToCentimeter(_duration);
- //delay(25);
- return d;
- }
- long SR04::DistanceAvg(int wait, int count) {
- long min, max, avg, d;
- min = 999;
- max = 0;
- avg = d = 0;
- if (wait < 25) {
- wait = 25;
- }
- if (count < 1) {
- count = 1;
- }
- for (int x = 0; x < count + 2; x++) {
- d = Distance();
- delayMicroseconds(DEFAULT_DELAY);
- if (d < min) {
- min = d;
- }
- if (d > max) {
- max = d;
- }
- avg += d;
- }
- // substract highest and lowest value
- avg -= (max + min);
- // calculate average
- avg /= count;
- return avg;
- }
- void SR04::Ping() {
- _distance = Distance();
- }
- long SR04::getDistance() {
- return _distance;
- }
- long SR04::MicrosecondsToCentimeter(long duration) {
- // 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 duration / 29 / 2; duration /(29*2)
-
- /**
- * Do the measurement calculation and return result in centimeter
- * SR04 measure echo time to obstacle and return way.
- *
- * Sound travels with 340m/sec
- *
- * Example: Obstace 100cm away from SR04. Measure time is 100cm to
- * obstacle and 100cm return = 200cm
- *
- * 1sec = 1000ms = 1.000.000uS
- * 1.000.000 / 340 = Distance in microseconds for 100cm
- * 2941uS fuer 100cm = 5882 uS fuer 200cm
- *
- * duration / 5882 * 100 = distance in cm
- */
-
- //long d = (duration * 100) / 5882;
- long d = (duration * 50) / 2941;
- d = (duration == 0)?999:d;
- return d;
- }
- long SR04::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;
- }
复制代码
SR04.h
- #ifndef SR04_H
- #define SR04_H
- #if defined(ARDUINO) && ARDUINO >= 100
- #include "Arduino.h"
- #else
- #include "WProgram.h"
- #endif
- //#include "pins_arduino.h"
- #include
- #define PULSE_TIMEOUT 50000L // 5m -> 340m/sec 29411uS
- #define DEFAULT_DELAY 30
- #define DEFAULT_PINGS 10
- class SR04 {
- public:
-
- /**
- * Constructor
- * Ultrasonic sensor SR04, four connections pins
- * VCC, ECHO, TRIGGER, GND
- *
- * param echoPin digital INPUT-Pin for measuring distance
- * param triggerPin if 10us high a trigger signal is generated from
- * SR04
- *
- * return void
- */
- SR04(int echoPin, int triggerPin);
- /**
- * Do a measurment for this sensor. Return distance as long
- * in centimenter
- * return long distance in centimeter
- */
- long Distance();
-
- /**
- * Do count measurents and calculate the average.
- * To avoid defilement from ow/high peaks, min/max values
- * are substracted from the average
- *
- * param wait delay between measurements, default = DEFAULT_DELAY/ms
- * param count number of measurements, default DEFAULT_PINGS
- * return long distance in centimeter
- **/
- long DistanceAvg(int wait=DEFAULT_DELAY, int count=DEFAULT_PINGS);
-
- /**
- * Do only a ping. Get result with methode getDistance()
- *
- * param keine
- */
- void Ping() ;
-
- /**
- * return latest distance. Methode Ping() should be called before
- * param keine
- * return Distanz in Zentimeter
- */
- long getDistance();
-
- private:
- /**
- * Do the measurement calculation and return result in centimeter
- * SR04 measure echo time to obstacle and return way.
- *
- * Sound travels with 340m/sec
- *
- * Example: Obstace 100cm away from SR04. Measure time is 100cm to
- * obstacle and 100cm return = 200cm
- *
- * 1sec = 1000ms = 1.000.000uS
- * 1.000.000 / 340 = Distance in microseconds for 100cm
- * 2941uS fuer 100cm = 5882 uS fuer 200cm
- *
- * duration / 5882 * 100 = distance in cm
- */
- long MicrosecondsToCentimeter(long duration);
- long MicrosecondsToInches(long microseconds);
-
- long _currentDistance;
- int _echoPin, _triggerPin;
- long _duration, _distance;
- bool _autoMode;
- };
- #endif
复制代码
|