Code: Select all
// setup pins and variables for DYP-ME007 sonar device
int echoPin = 2; // DYP-ME007 echo pin (digital 2)
int initPin = 3; // DYP-ME007 trigger pin (digital 3)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
void setup() {
pinMode(initPin, OUTPUT); // set init pin 3 as output
pinMode(echoPin, INPUT); // set echo pin 2 as input
// initialize the serial port, lets you view the distances being pinged if connected to computer
Serial.begin(115200);
}
void loop() {
digitalWrite(initPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(6); // wait 10 microseconds before turning off (delay 6us + 4us for DW)
digitalWrite(initPin, LOW); // stop sending the pulse 4us
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
if (distance < 500) {
Serial.print("Distance "); Serial.print(distance, DEC); Serial.println("cm");
delay(100); // wait 100 milli seconds before looping again if it was a good reading.
}
}
Conclusion: Works fine and I'm impressed by the accuracy, +/- 2mm at any range up to 2 meters (didn't have a longer ruler...) if the reflecting target is a solid material. If the material is irregular or soft (like my t-shirt) it has some difficulties to get a good reading. Drawback is that it needs 2 digital pins, 1 for trigger and 1 to read the output pulse.
/Bo