Test: Ultrasonic Sensor Module (DYP-ME007)

Post Reply
Point65
Posts: 9
Joined: Fri Jan 28, 2011 9:37 pm

Test: Ultrasonic Sensor Module (DYP-ME007)

Post by Point65 »

Kinderkram sent me one for test (I assume) so I signed up for the mission. Here is the test code I used slightly modified from http://www.emartee.com. I had to reduce the delay to 6 as it stopped after 20-30 readings, the delayWrite to low needs 4us to complete. Now I've had it running for +15minutes without stopping.

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

bradleyk
Posts: 48
Joined: Wed Jan 19, 2011 10:58 pm

Re: Test: Ultrasonic Sensor Module (DYP-ME007)

Post by bradleyk »

you could do it with one plus 2 diodes, but the code will need to be changed to work with the multiwii

bradleyk
Posts: 48
Joined: Wed Jan 19, 2011 10:58 pm

Re: Test: Ultrasonic Sensor Module (DYP-ME007)

Post by bradleyk »

hi,
my version using interrupts

Code: Select all


// set up pins and variables for DYP-ME007 sonar device
int echoPin = 2; // DYP-ME007 echo pin (digital 2)  MUST be PIN 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);
attachInterrupt(0, sonor,FALLING ); // Look for a return pulse, it should be high as the pulse goes low-high-low


}

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 = micros();
   delay(100);
}

void sonor()
{
distance =  (micros()-pulseTime)/58 ; // Distance = pulse time / 58 to convert to cm.
if (distance < 500 && distance >0) {
  Serial.print("Distance "); Serial.print(distance, DEC); Serial.println("cm");
   }
}

CarlP
Posts: 3
Joined: Wed Feb 15, 2012 7:32 pm

Re: Test: Ultrasonic Sensor Module (DYP-ME007)

Post by CarlP »

You can use one pin, just change it from an output pin to an input pin every cycle. Saw someone do on the net and tried it with my UNO and it seems to work fine.

Is there a plan to integrate the code for the sensor?

Post Reply