Pages: [1]   Go Down
Author Topic: Problem with ultrasonic sensor  (Read 3405 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);   
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 508
Posts: 31416
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Hyderabad , India
Offline Offline
God Member
*****
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I made a little function for this
Code:
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.
Logged

captain-slow.dk | non contagious!

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I made a little function for this
Code:
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.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

captain-slow.dk | non contagious!

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My sensor is DYP-ME007
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
}

Logged

Pages: [1]   Go Up
Jump to: