Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: Jak24 on June 21, 2011, 12:51:26 PM

Title: Srf08 Ultrasonic sensor giving false readings
Post by: Jak24 on June 21, 2011, 12:51:26 PM
Hi!

So for my current project I want to use 4 SRF08 sensors:
http://www.jk-sensor.com/UserFiles/File/ultrasonic/srf08.pdf (http://www.jk-sensor.com/UserFiles/File/ultrasonic/srf08.pdf)
So i connected it up with my arduino mega 2560, and i used the example code from here:
http://www.robot-electronics.co.uk/files/srf08.pde (http://www.robot-electronics.co.uk/files/srf08.pde)
with a few modifications:

Code: [Select]
/*
Generic example for the SRF modules 02, 08, 10 and 235.
Only the SRF08 uses the light saensor so when any other
range finder is used with this code the light reading will
be a constant value.
*/

#include <Wire.h>

#define srfAddress 0x70                           // Address of the SRF08
#define cmdByte 0x00                              // Command byte
#define lightByte 0x01                            // Byte to read light sensor
#define rangeByte 0x02                            // Byte for start of ranging data
#define clrScrn 0x0C                              // Byte to clear LCD03 screen
#define mvCsr 0x02                                // Byte to tell LCD03 we wish to move cursor
#define hideCsr 0x04                              // Byte to hide the cursor

byte highByte = 0x00;                             // Stores high byte from ranging
byte lowByte = 0x00;                              // Stored low byte from ranging

void setup(){
 
 
  Wire.begin();                                                                  // Waits to make sure everything is powered up before sending or receiving data
  Serial.begin(9600);

}

void loop(){
 
  int rangeData = getRange();                     // Calls a function to get range
  Serial.print(rangeData);
  Serial.println();
  delay(1);                                      // Wait before looping
}

int getRange(){                                   // This function gets a ranging from the SRF08
 
  int range = 0;
 
  Wire.beginTransmission(srfAddress);             // Start communticating with SRF08
  Wire.send(cmdByte);                             // Send Command Byte
  Wire.send(0x51);                                // Send 0x51 to start a ranging
  Wire.endTransmission();
 
  delay(100);                                     // Wait for ranging to be complete
 
  Wire.beginTransmission(srfAddress);             // start communicating with SRFmodule
  Wire.send(rangeByte);                           // Call the register for start of ranging data
  Wire.endTransmission();
 
  Wire.requestFrom(srfAddress, 2);                // Request 2 bytes from SRF module
  while(Wire.available() < 2);                    // Wait for data to arrive
  highByte = Wire.receive();                      // Get high byte
  lowByte = Wire.receive();                       // Get low byte

  range = (highByte << 8) + lowByte;              // Put them together
 
  return(range);                                  // Returns Range
}



essentially i took out the LCD part and just used the serial print function to display the data on my pc.
I also took out the light sensor and software revision part, as I don't really need that info. :P
When I start looking at readings, it's fine, for measuring things within 50-60cm and is quite accurate.
But If I measure things beyond that value, the readings don't really go beyond 70 cm ,and are quite inaccurate.
What could that problem be, has anyone had this problem before?

Regards

Jak24
Title: Re: Srf08 Ultrasonic sensor giving false readings
Post by: waltr on June 21, 2011, 02:06:34 PM
Is the 5V to the US sensor good? Using a separate 5V reg fo r the US sensor is a good idea.

What is the test target? Is it large enough for the US to reflect a strong signal from?

Have you searched these forums for more info on the SRF08 and other US sensors?
Title: Re: Srf08 Ultrasonic sensor giving false readings
Post by: Webbot on June 21, 2011, 07:17:35 PM
If you are repeatedly getting the sonar to ping then it could also be that you are suffering from echoes from earlier pings. You need to wait till all sound has died away before taking your next reading.
Easy to test - just add a significant delay (say 1 second) at the start of the loop.