Jump to content


Photo

Ultrasonic Sensor Not Working


  • Please log in to reply
7 replies to this topic

#1 cellicott

cellicott

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 07 August 2014 - 02:54 AM

I'm using the newest robotics kit. Everything works when I plug in the robot, except for the ultrasonic sensor. I've tinkered with the code for a long time now, with no luck. Eventually i just copied and pasted the final code from the guide. No luck. I've seen several people have problems with their sensors, recording objects at 0cm or up to 1000, however mine starts at 0cm, jumps immediately down to about negative 470cm, occasionally reaching and exceeding 500cm. Waving my hand in front of the sensor has no effect on the readings.

 

 

I've uploaded pictures of my electrical setup as well as a basic diagram.

 

Thanks in advance.

Attached File  IMG_0842 (1).JPG   150.54KB   0 downloads

Attached File  IMG_0843 (1).JPG   147.07KB   0 downloads

Attached File  IMG_0844 (1).JPG   121.45KB   0 downloads

Attached File  Screen Shot 2014-08-06 at 10.38.29 PM.png   98.11KB   0 downloads


  • 0

#2 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 07 August 2014 - 03:22 PM

Hey, sorry for the trouble you're having!

 

Your wiring looks good to me, thanks for the pictures and schematic.

 

I've found that about 1 in 200 ultrasonics are defective, so it's possible you got a bad one. But let's try a few debugging steps first.

 

1. to start, try uploading just the ultrasonic code to the robot to make debugging easier:

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

2. run the above program and check the output. If it still doesn't work, continue..

 

3. do you have a multimeter? If so, check the voltage across the VCC & GND pins on the ultrasonic to verify it's getting 5V.

 

4. it's hard to verify the trig & echo wires are correct from the picture, so just for kicks try swapping them (after testing, put them back to how you think they should be)

 

5. sometimes the jumper wires break, so try replacing all of the wires to the ultrasonic (including power, IF you weren't able to check with a multimeter)

 

6. go back to step 4! (but afterwords jump to step 7, else you'll get stuck in a loop :P)

 

7. this probably should have been like step 1, but try using new batteries..

 

8. we'll cross this bridge if/when we get here  :)

 


  • 0

#3 cellicott

cellicott

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 07 August 2014 - 06:08 PM

Tried the code, same results. Multimeter was dead so I tried replacing the Vcc and Ground wires, pulled up the serial monitor and there definitely was something wrong with the wires. All results read 0cm, and didn't respond to anything waving in front of them. Not sure if this is a good thing or a bad thing, but it seems like progress. I messed around with the trig and echo wires, nothing happened.

 

Batteries are brand new anyway, but I replaced them anyway. Shouldn't this not make a difference when using the computer as a power source?

 

Looks like we may need to move on to step 8, unless there's something I missed.


  • 0

#4 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 07 August 2014 - 07:22 PM

Step 8 was me sending you a new sensor, but let's try a little more debugging first..

 

So before replacing the power wires, you were getting negative values in the serial terminal, and after replacing them you're seeing "0cm" being printed out? If the output in the terminal changed when you used different wires, then some of the wires are definitely bad (either the original ones, or the new ones -- or both).

 

Did you replace the trig & echo wires as well?

 

One thing to try is to tilt the sensor backwards a little (so that it's facing slightly upward), lifting the front of the robot up a little, and then with your other hand putting it right in front of the sensor (touching it) and then slowly moving it back. Try this, and see if "0cm" continues to print out.

 

From your pictures it looks like you used some hookup wire. Do you have enough on hand to replace the jumper wires going to the sensor (power, gnd, trig & echo) with hookup wire? That would eliminate any doubt about a break in one of the wires.

 

I've had a lot of trouble with jumper wires in the past. Sometimes the pins on the end become loose and you can pull them out with the slightest amount of force. Other times, the pins are still attached but it seems like there's a break somewhere. I just switched suppliers for jumper wires, so hopefully the new ones will be better..


  • 0

#5 cellicott

cellicott

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 07 August 2014 - 08:53 PM

I've narrowed it down to one wire; the red jumper wire. Very odd. When this wire is in play, nothing works. When it's connecting Vcc to the power rail, the serial monitor records distances at around -470, which we established. However that's not the odd part. When plugged into the ground side, the serial monitor records distances at around positive 470. In addition to that, the recordings are marked at a much shorter delay than the code states. It seems as though the wire has a life of its own. 

 

All wires going into the sensor are hookup wires now, and it starts up recording 0cm. At this point I replaced all of the hookup up wires with their original one by one, assuming their was another faulty wire. Every time I did this it recorded it's -400 business. Then I thought maybe it was a combination of wires, so I replaced the only jumper wire left with another hookup wire, clicked serial monitor, and instead of seeing 0s as previously, it was the -470 thing again.

 

This seems to be something more than just the sensor acting up...

 

Tried hooking the sensor on to another Arduino, using this code:


#define trigPin 8
#define echoPin 9

void setup()
{
  Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) /29;
Serial.print(distance);
Serial.print(" cm");
delay(500);
} 

 and this electrical setup:

 

1PYPEj4.jpg

 

Despite what the image looks like, the yellow wire on the right is plugged into power.

 

Got the same -400 results. 


  • 0

#6 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 07 August 2014 - 09:35 PM

It seems as though the wire has a life of its own.

 

It's aliiiiiive! Ha ha :) 

 

Well the behavior you're getting doesn't make any sense to me, so I think it's probably either a power issue or a faulty sensor. I'll go ahead and just send you a new ultrasonic sensor, testing it beforehand to make sure it works, and hopefully that fixes the issue (if not, you'll probably need to get a multimeter to help debug whatever the problem is). 

 

Should I send it to the same address your order went to (Broad St)?


  • 1

#7 cellicott

cellicott

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 07 August 2014 - 09:48 PM

Yes, that would be fine. Thanks, Nate. Hopefully things will work out this time.


  • 0

#8 cellicott

cellicott

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 11 August 2014 - 06:35 PM

Got the new sensor today. Everything works perfectly!

Thanks again.


  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users