SRF02 Hangs when calling wire.available()

General & specific discussions on our Ultrasonic Rangers

Moderator: chris

SRF02 Hangs when calling wire.available()

Postby GeekMan » Mon Aug 23, 2010 4:41 am

I copied the example from this site for the SRF02 and Arduino. Only difference is I don't have an LCD so and using serial print statements and I am using 2k I2C restistors.

Anyway, when arduino boots, the following code executes until the while( Wire.available() < 2 ) and never returns.

Any idea why? BTW, when powering up the SRF02, it blinks one long blink cooresponding to default address. I have 2 units and get the same behavior when i swap them out.

/*
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>
#include <SoftwareSerial.h>

#define rxPin 2 // Software serial pin for rx
#define txPin 3 // Software serial pin for tx
#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

//SoftwareSerial lcd_03 = SoftwareSerial(rxPin, txPin); // defines a new software serial port for lcd_03

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

void setup(){

// lcd_03.begin(9600); // Begins serial port for LCD_03
Wire.begin();
Serial.begin(57600);
delay(100); // Waits to make sure everything is powered up before sending or receiving data

// pinMode(rxPin, INPUT);
// pinMode(txPin, OUTPUT);
// lcd_03.print(clrScrn, BYTE);
// lcd_03.print(hideCsr, BYTE);
//lcd_03.println("SRF02/08/10/235");

int softRev = getSoft(); // Calls function to get software revision


Serial.print("Software version: ");
Serial.print( softRev ) ;

}

void loop(){

int rangeData = getRange(); // Calls a function to get range
Serial.print("Range = ");
Serial.print(rangeData); // Print rangeData to LCD03
Serial.println();



delay(100); // Wait before looping
}

int getRange(){ // This function gets a ranging from the SRF08

int range = 0;
Serial.println("Before Trans:");
Wire.beginTransmission(srfAddress); // Start communticating with SRF08
Wire.send(cmdByte); // Send Command Byte
Wire.send(0x51); // Send 0x51 to start a ranging
Wire.endTransmission();
Serial.println("End Wire Trans");
delay(100); // Wait for ranging to be complete

Serial.println("Before SRF Mod");
Wire.beginTransmission(srfAddress); // start communicating with SRFmodule
Wire.send(rangeByte); // Call the register for start of ranging data
Wire.endTransmission();

Serial.println("Getting Range");
Wire.requestFrom(srfAddress, 2); // Request 2 bytes from SRF module
while(Wire.available() < 2) // Wait for data to arrive
{
// Never gets out of this loop. I GET STUCK RIGHT HERE**************************
}
highByte = Wire.receive(); // Get high byte
lowByte = Wire.receive(); // Get low byte
Serial.println("Done");
range = (highByte << 8) + lowByte; // Put them together

return(range); // Returns Range
}


int getSoft(){ // Function to get software revision

Wire.beginTransmission(srfAddress); // Begin communication with the SRF module
Wire.send(cmdByte); // Sends the command bit, when this bit is read it returns the software revision
Wire.endTransmission();

Wire.requestFrom(srfAddress, 1); // Request 1 byte
while(Wire.available() < 0); // While byte available
int software = Wire.receive(); // Get byte

return(software);

}
GeekMan
 
Posts: 8
Joined: Mon Aug 23, 2010 4:30 am

Re: SRF02 Hangs when calling wire.available()

Postby Gerry » Tue Aug 24, 2010 10:49 am

Your code runs fine, and doesn't hang up.

It looks as though the SRF02 is not responding. The one long blink of the LED at power-up indicates its running and at the default address of 0xE0 (0x70 on the Arduino). I'd guess there is a problem with the SCL/SDA connections. Check they are securely connected and going to the correct pins. Also check you have the pull-up resistors and they are wired correctly, it will not work without them. The Arduino does not have them and you must wire your own as shown on the our schematic.
http://www.robot-electronics.co.uk/htm/arduino_examples.htm#SRF02,%20SRF08,%20SRF10,%20SRF235
User avatar
Gerry
Site Admin
 
Posts: 249
Joined: Wed Nov 08, 2006 12:28 pm

Re: SRF02 Hangs when calling wire.available()

Postby GeekMan » Tue Aug 24, 2010 2:38 pm

What you say makes sense. I forgot to mention that the software address also comes back zero from GetSoft(), so I2C bus is not working.

I took it apart and rewired from scratch, but still no luck. I am using 2K for I2C pullups. Should i try another value?
I don't have 1.8 k. I have seen a wide array of values recommended.
GeekMan
 
Posts: 8
Joined: Mon Aug 23, 2010 4:30 am

Re: SRF02 Hangs when calling wire.available()

Postby GeekMan » Tue Aug 24, 2010 2:40 pm

BTW, I am running Duemillonove. I wired it exactly like your example in the link.
GeekMan
 
Posts: 8
Joined: Mon Aug 23, 2010 4:30 am

Re: SRF02 Hangs when calling wire.available()

Postby Gerry » Tue Aug 24, 2010 3:39 pm

2k pull-ups are fine. In fact anything from 1k8 to 10k will be fine.
I'm sure this is going to be a connection problem somewhere.
Are you powering the SRF02 from the Arduino? Is the 0v ground connected between them?
User avatar
Gerry
Site Admin
 
Posts: 249
Joined: Wed Nov 08, 2006 12:28 pm

Re: SRF02 Hangs when calling wire.available()

Postby GeekMan » Tue Aug 24, 2010 7:47 pm

Yes, using 5v pin to power from arduino and gnd from arduino is set to gnd from SRF02.
GeekMan
 
Posts: 8
Joined: Mon Aug 23, 2010 4:30 am

Re: SRF02 Hangs when calling wire.available()

Postby GeekMan » Tue Aug 24, 2010 8:50 pm

I will try this again when i get home. I suspect that the problem is:

Bad connection
or
screwed arduino.
or SRF02, but SRF02 lights up when booting, so it is an unlikely culprit.

Question: is the through hole in the SRF02 plated though? I stuck a header into it and soldered the pins on the short side of each header pin. Just wondering if solder has to touch both sides or one particular side of the board. ( Somebody posted somewhere to that effect )
GeekMan
 
Posts: 8
Joined: Mon Aug 23, 2010 4:30 am

Re: SRF02 Hangs when calling wire.available()

Postby Gerry » Wed Aug 25, 2010 8:17 am

Yes, all holes are plated through.
If its soldered one side it will be fine, just make sure the solder has flowed down into the hole so that you don't just have a "blob" sitting on the pin.
Make sure you have SCL going to SCL and SDA to SDA, and you don't have them crossed over.
User avatar
Gerry
Site Admin
 
Posts: 249
Joined: Wed Nov 08, 2006 12:28 pm

Re: SRF02 Hangs when calling wire.available()

Postby GeekMan » Thu Aug 26, 2010 1:21 am

Ok, i took the SRF02 out of circuit and set up a test of I2C between 2 arduinos set up as master and slave. They worked with no issue, so it is not the arduino.
Must be something with the sonar unit. I rewired everything just to make sure, then pasted the code back exactly as what i posted here, since you confirmed that it should work.

I notice that the software version from this function always returns zero, so wire functions are working, just unit gives back junk.


int getSoft(){ // Function to get software revision

Wire.beginTransmission(srfAddress); // Begin communication with the SRF module
Wire.send(cmdByte); // Sends the command bit, when this bit is read it returns the software revision
Wire.endTransmission();

Wire.requestFrom(srfAddress, 1); // Request 1 byte
while(Wire.available() < 0); // While byte available
int software = Wire.receive(); // Get byte

return(software);

}

output of program is:

Software version: 0Before Trans:
End Wire Trans
Before SRF Mod
Getting Range

Any other thoughts? I am running out of ideas.
GeekMan
 
Posts: 8
Joined: Mon Aug 23, 2010 4:30 am

Re: SRF02 Hangs when calling wire.available()

Postby GeekMan » Thu Aug 26, 2010 1:32 am

Never mind, I moved everything to a different bread board and it works.
Maybe loose connectors in the bread board. go figure.
Thanks for the help!
GeekMan
 
Posts: 8
Joined: Mon Aug 23, 2010 4:30 am


Return to Ultrasonic Sensors

Who is online

Users browsing this forum: No registered users and 0 guests

cron