The SRF02 is an ultrasonic distance sensor with an I2C library. This library simplifies accessing SRF02 modules from within Arduino code. One special thing about the SRF02 is, that you first need to trigger a new measurement and after approx. 65 ms you can read the result. The SRF02 library lets you declare as many sensor instances you have connected and with a single call to SRF02::update() all instances are triggered to perform a new measurement. You don't need to care about timing issues. Just call SRF02::update() regularly from within the loop() function. If new measurement results are available they will be fetched from the I2C bus and stored into instance variables. The current distance value is accessible through the read() method.
Download the ZIP archive SRF02-1_4.zip and unzip into your <Path to Arduino>/libraries directory.
Start Arduino and select File->Sketchbook->Examples->Library SRF02->SRF02 from the Menu to load the example sketch.
unsigned int read()
setInterval(unsigned int interval)
configureDeviceId(uint8_t currentDeviceId, uint8_t newDeviceId)
Assuming that you have connected three SRF02 with device ids 0x70, 0x71 and 0x72 connected to the I2C bus. The following code regularly reads the sensors and writes the distance measurements every one second onto the serial interface.
Since all SRF02 are shipped with the same id (0x70), changing their ids is a bit tedious, because you can change only one device at the same time. First you need a program that does nothing else than initializing the I2C interface (Wire.begin()) and calling the SRF02::configureDeviceId(0x70, newId) method (replace newId by the id you want the SRF02 to listen to). Transfer the program, connect exactly one SRF02 and start the Arduino. The SRF02 is now reporgrammed. It's best to stick a label onto the sensor, that tells you the new id, since this SRF02 will never listen anymore to id 0x70 (except you change it back to the default id). Switch off, change the code for the next sensor (replace newId with another not yet assigned id), transfer the program, connect the next sensor and turn on the Arduino. Repeat until you've programmed all your SRF02s.
The following sketch puts a new sensor with device id 0x70 on the new id 0x71:
The SRF02 Arduino Library is licensed under the GNU Lesser General Public License Version 3.0.