Jump to content


Photo

Ultrasonic sensor not working


  • Please log in to reply
8 replies to this topic

#1 grahambbird

grahambbird

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 30 March 2014 - 08:06 PM

As far as I can determine, the ultrasonic sensor on my robot kit is not working.

 

I've set it up from scratch again on a clean board and i've checked and double checked the connections and programming.

 

The distances coming in die after about 3-7 readings interspersed amongst the many 0cm readings.

 

Any ideas?

 

Cheers


  • 0

#2 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 30 March 2014 - 08:11 PM

Hi, I moved your post to a new topic so that I could better help you.

 

 

The distances coming in die after about 3-7 readings interspersed amongst the many 0cm readings.

 

Just to clarify, the first 3-7 readings are correct, but then it reads as 0cm after that?

 

Are you sure you have the sensor hooked up to 5V (coming from the Ardunio), not the power rail for the motors?

 

Could you post your code, as well as the output from the serial monitor?


  • 0

#3 grahambbird

grahambbird

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 02 April 2014 - 05:18 PM

Thanks!

 

 

Here is the code (the standard code form the project page):

 

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;
 
const int loopPeriod = 20;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;
 
// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int ultrasonic2EchoPin = 9;
 
int ultrasonic2Distance;
int ultrasonic2Duration;
 
 
void setup()
{
  Serial.begin(9600);
 
  // ultrasonic sensor pin configurations
  pinMode(ultrasonic2TrigPin, OUTPUT);
  pinMode(ultrasonic2EchoPin, INPUT);
}
 
 
void loop()
{
  debugOutput(); // prints debugging messages to the serial console
 
  if (millis() - timeLoopDelay >= loopPeriod)
  {
    readUltrasonicSensors(); // read and store the measured distances
 
    timeLoopDelay = millis();
  }
}
 
 
void readUltrasonicSensors()
{
  // ultrasonic 2
  digitalWrite(ultrasonic2TrigPin, HIGH);
  delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
  digitalWrite(ultrasonic2TrigPin, LOW);
 
  ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
  ultrasonic2Distance = (ultrasonic2Duration / 2) / 29;
}
 
 
void debugOutput()
{
  if ((millis() - timeSerialDelay) > serialPeriod)
  {
    Serial.print("ultrasonic2Distance: ");
    Serial.print(ultrasonic2Distance);
    Serial.print("cm: ");
    Serial.println();
 
    timeSerialDelay = millis();
  }
}

 

 

 

Here is the serial output:

 

ultrstance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 247cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 6cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 
ultrasonic2Distance: 0cm: 

  • 0

#4 grahambbird

grahambbird

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 02 April 2014 - 05:24 PM

The setup is on a clean board, I have a pic but I can't see  a way to post a jpg here.


  • 0

#5 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 07 April 2014 - 10:06 PM

Hi,

 

I'm so sorry for not getting back with you sooner. The website is supposed to notify me when there are new posts on the forum, but it didn't!

 

That output is pretty weird. I'm guessing it's a wiring issue, so a few pictures of your wiring would definitely help. To post a picture on here, click on the More Reply Options button and then you'll see a way to attach files to the post.

 

If you're sure you have the ultrasonic sensor hooked up to 5V, and that the trig & echo pins on the sensor are connected to the correct pins on the Arduino (as specified in the code), then my next guess is that there might be a grounding issue. Is there a wire going from the GND pin on the Arduino to the ultrasonic sensor (indirectly through the breadboard is fine)?


  • 0

#6 grahambbird

grahambbird

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 07 April 2014 - 10:17 PM

Here is the pic.

Attached Files


  • 0

#7 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 07 April 2014 - 10:38 PM

Thanks for the picture, the wiring looks correct to me.

 

Do you have a multimeter you could use to verify the sensor is getting power (measuring directly across the pins on the sensor)? If not, try connecting an LED in series with a resistor across the VCC and GND pins on the ultrasonic sensor (you can plug the long leg of the LED into the row on the breadboard that the VCC pin of the sensor is plugged into, the other leg into an empty row, and then the resistor from the row with the short leg of the LED to the row that the GND pin on the sensor is plugged into). This will rule out the possibility that it's not getting power for some reason, such as a bad jumper cable.

 

Also, if that doesn't work, one other thing I'd like to try is connecting the battery pack to the Arduino. It's possible (although unlikely) that the USB port on the computer can't source enough current for the Arduino to run properly.


  • 0

#8 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 24 April 2014 - 05:55 PM

You might want to check that the sensor's pings aren't hitting the wheels and returning early. I had an issue similar to this and I removed the wheels and tested and it gave me good readings. Try taking off the wheels and testing the code. If you get good readings, you know that the servo wheels are your issue!


  • 1

#9 grahambbird

grahambbird

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 30 April 2014 - 05:31 PM

You might want to check that the sensor's pings aren't hitting the wheels and returning early. I had an issue similar to this and I removed the wheels and tested and it gave me good readings. Try taking off the wheels and testing the code. If you get good readings, you know that the servo wheels are your issue!

Thanks, I'll give that a try!

 

Cheers


  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users