srf range finders with arduino

The tricky part to using the srf series range finder with the arduino is the I2c bus interface it is a fairly straightforward protocol (a good reference is at http://www.robot-electronics.co.uk/htm/using_the_i2c_bus.htm) but would take a lot of typing if you did it from scratch. It is basically a handshake method where the arduino slaves the srf range finder to its clock pulses and tells it when and from where the rangefinder will send information. It sends out info on the leading edge of the clock pulse and receives it on the falling edge, but the signals state (bit passing) never changes while the clock state is high, except to signal a start or stop to a communication. So the easiest way to communicate with your range finder is to use the code found on the Wiring web site (it is the sibling of arduino but even more user friendly it seems) and use the “wire” library (here’s the link http://wiring.org.co/reference/libraries/Wire/index.html). I took that code and modified it to include changing the address, the sensitivity and the range settings for the srf range finder (it should be talked about although maybe a bit muddled in the pamphlet that comes with the range finder). Also be sure to use the arduino 06 platform when implementing the
Ps it took me a really long time to do the water colors so i hope you enjoy them.

#include Wire.h
////////////////////use below pins for input and out put/////////////////////////
/////////*************On the Arduino, SDA is on analog input pin 4, and SCL on analog input pin 5.****************///////////////////////
// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
// by Nicholas Zambetti
// and James Tichenor
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600);
// start serial communication at 9600bps
}
int reading = 0;
void loop()
////////////////sensitivity adjust/////////////////////////////////////////////
{ Wire.beginTransmission(0x70); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.send(0x01); // sets register pointer to the command register (0x01) reg 1
Wire.send(0x21); // command sensor to analog gain (sensitivity of sensor)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting
//////////////////////range select////////////////////////////
Wire.beginTransmission(0x70); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.send(0x20); // sets register pointer to the command register (0x10) reg 2
Wire.send(0x00); // command sensor to analog range (distance)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting
//////////////////////////// step 1: instruct sensor to read echoes ///////////////////////////////////////
Wire.beginTransmission(0x70); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.send(0x00); // sets register pointer to the command register (0x00) write
Wire.send(0x50); // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting
//////////////// step 2: wait for readings to happen ///////////////////////////////////////////////
delay(70); // datasheet suggests at least 65 milliseconds
////////////// step 3: instruct sensor to return a particular echo reading ////////////////////////////
Wire.beginTransmission(0x70); // transmit to device #112
Wire.send(0x02); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
//////////////// step 4: request reading from sensor /////////////////////////
Wire.requestFrom(0x70, 2); // request 2 bytes from slave device #112
/////////////// step 5: receive reading from sensor ////////////////////////////
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.receive(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.receive(); // receive low byte as lower 8 bits
Serial.println(reading); // print the reading
}
delay(250); // wait a bit since people have to read the output
}
/*
// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
// usage: changeAddress(0x70, 0xE6);
void changeAddress(byte oldAddress, byte newAddress)
{
Wire.beginTransmission(oldAddress);
Wire.send(0x00);
Wire.send(0xA0);
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.send(0x00);
Wire.send(0xAA);
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.send(0x00);
Wire.send(0xA5);
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.send(0x00);
Wire.send(newAddress);
Wire.endTransmission();
}
*/