Problem with ultrasonic sensor

usually,the ultrasonic works normal,but sometimes,when i moved the ultrasonic sensor,it has no reaction,and the arduino borad looks like dead,it's not work too.
I dont know why..
some code behind,copy from others:
void loop()

{
digitalWrite(outputPin, LOW);
delayMicroseconds(2);
digitalWrite(outputPin, HIGH);
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
int distance = pulseIn(inputPin, HIGH);
distance= distance/58;
Serial.println(distance);
}

Make sure the connections are solid and don't break as you move it.

i'm sure the connect is fine,and i'm sure the problem is not aboat the connect

usually,the ultrasonic works normal,but sometimes,when i moved the ultrasonic sensor,it has no reaction,and the arduino borad looks like dead,it's not work too.

make sure as to what is wrong the board or the sensor upload the blinky to check if the board is in condition

i think the problem is the time sensor wait for,maby the sensor sometimes received a wrong data or even have not receive any data

I made a little function for this

double ping(int outPin, int inPin) //Get CM to obstacle in front of the sensor
{
  long duration;
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT);
  digitalWrite(outPin, LOW);
  delayMicroseconds(2);
  digitalWrite(outPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(outPin, LOW);
  delayMicroseconds(20);
  duration = pulseIn(inPin, HIGH, 2500);
  return duration / 29.0 / 2.0;
}

This returns the distance in CM to the thing it detected. At the same time I added a timeout on 2500 micro seconds, so if no ping back is received, it will return 0 and continue the loop, instead of hang.

bld:
I made a little function for this

double ping(int outPin, int inPin) //Get CM to obstacle in front of the sensor

{
  long duration;
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT);
  digitalWrite(outPin, LOW);
  delayMicroseconds(2);
  digitalWrite(outPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(outPin, LOW);
  delayMicroseconds(20);
  duration = pulseIn(inPin, HIGH, 2500);
  return duration / 29.0 / 2.0;
}



This returns the distance in CM to the thing it detected. At the same time I added a timeout on 2500 micro seconds, so if no ping back is received, it will return 0 and continue the loop, instead of hang.

I have changed the code like yours,but when i moved the thing detected(like paper),it returns nothing,and the arduino stop work.

out of curiosity what kind of sensors are you using? I've had good luck with SRF04 and MaxSonar EZ1 but i know someone who was using the sensors that come out of car bumpers and his board would freeze up randomly (have to turn off power and reset) , after a couple hours of tweaking we got it working pretty well but discovered that when the sensor reported a 0 that it would freeze up. My theory is that the pulse is coming back to quickly and screwing up the transistor circuit he was using to drive and read the sonar pin or its overflowing an internal timing register in the atmega. It's strange i've only ever seen this happen once and your post reminded me of it.

I'm working with a few SRF04's right now for a robotics competition and i have 6 wired up and running just fine on one board. I even put an obstacle right on the sensors with no problems. It may help to add a small delay at the end before the return. I started with delay(500) when i first wired everything up, that let me watch the values and put a hand in front of each and watch for a change. I dropped it down to 10 currently for some testing and i'm sure it can go even lower but all of the serial printing faster then that would crash the terminal. Once i'm ready for a real test i'll drop it to either 1ms or go down to the microseconds.

wkuace:
out of curiosity what kind of sensors are you using? I've had good luck with SRF04 and MaxSonar EZ1 but i know someone who was using the sensors that come out of car bumpers and his board would freeze up randomly (have to turn off power and reset) , after a couple hours of tweaking we got it working pretty well but discovered that when the sensor reported a 0 that it would freeze up. My theory is that the pulse is coming back to quickly and screwing up the transistor circuit he was using to drive and read the sonar pin or its overflowing an internal timing register in the atmega. It's strange i've only ever seen this happen once and your post reminded me of it.

I'm working with a few SRF04's right now for a robotics competition and i have 6 wired up and running just fine on one board. I even put an obstacle right on the sensors with no problems. It may help to add a small delay at the end before the return. I started with delay(500) when i first wired everything up, that let me watch the values and put a hand in front of each and watch for a change. I dropped it down to 10 currently for some testing and i'm sure it can go even lower but all of the serial printing faster then that would crash the terminal. Once i'm ready for a real test i'll drop it to either 1ms or go down to the microseconds.

The situation you said just right! my board was freeze up, and I must reset it.
So,the only way is change my ultrasonic sensor?

coos:
The situation you said just right! my board was freeze up, and I must reset it.
So,the only way is change my ultrasonic sensor?

Which sensor are you using?

The code I gave you is working fine, I am using it myself.

My sensor is DYP-ME007

I found a "datasheet" for your sensor its more of a guide then a datasheet, it even comes with some source code for the arduino. It looks like you have the right idea, hopefully your sensors are compatible. I took your code tweaked it and added a delay at the end. This will slow it down while your watching the serial terminal. It may also fix the freezing problem but no guarantee. It could be that you were polling it to fast, to many times and it overloaded the arduino or the sonar. I hope this helps

int outputPin = ?; // set you output pin
int inputPin = ?; // set your input pin
long distance;
long cm;
void setup(){
Serial.begin(); // set your baud rate
pinMode(outputPin, OUTPUT);
pinMode(inputPin,INPUT);
}
void loop()

{
  digitalWrite(outputPin, LOW);
  delayMicroseconds(2);
  digitalWrite(outputPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(outputPin, LOW);   
  distance = pulseIn(inputPin, HIGH); //it now initalizes before setup to speed up the process
  cm= distance/58;   //I don't like reusing the same variable name, it probably works fine though, 
                             //but i don't really trust it, besides you have plenty of memory for it.
  Serial.println(cm);   
  delay(500); // this will slow down your readings to 2 per second it should help when watching the Serial terminal
// if this helps then start to shrink it down to 100 then 10 then 1, maybe even go to microsecond delays or remove completely if it stops freezing
}

Even i had same problem. I realized the ultrasonic sensor is not getting enough current, so i connect it to external 5v supply and it works perfectly fine.
So i suggest use external 5v for sensor, "dont use arduino power supply"

guys have a problem with the sensor.......does not return proper values and gets stuck in the middle. pls reply...thanx