SRF02 Ultrasonic Distance Sensor


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 and Installation

Download the ZIP archive 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.


The SRF02 module should be connected to the I2C bus of the Arduino, i.e. the analog port pins 4 (SDA) and 5 (SCL). No pull-up resistors are needed, since the Atmel MCU already contains them internally.


SRF02(uint8_t deviceId, uint8_t mode)

Create a new instance of a SRF02 sensor with the specified (7-bit) device id and measurement mode (SRF02_INCHES, SRF02_CENTIMETERS, SRF02_MICROSECONDS).

unsigned int read()

Read the current distance value.


Call this method from within the loop() function. Most of the time update() simply returns and does nothing. When the measurement interval elapsed, the last measurement value is fetched and a new measurement is triggered.

setInterval(unsigned int interval)

Set the measurement intervall in milliseconds. The default is 70 ms.

configureDeviceId(uint8_t currentDeviceId, uint8_t newDeviceId) 

New devices are always configured to use the device id 0x70. Call this method to change the device id. Only connect this single SRF02 sensor to the I2C bus before calling this method!


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.

#include "Wire.h"
#include "SRF02.h"

SRF02 srf02[3] = {

unsigned long nextPrint = 0;

void setup() {

void loop() {
  while (true) {
    if (millis() > nextPrint) {
      nextPrint = millis () + 1000;

Changing the I2C id of several SRF02s

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:

#include "Wire.h"
#include "SRF02.h"

void setup() {
  SRF02::configureDeviceId(0x70, 0x71);

void loop() {


The SRF02 Arduino Library is licensed under the GNU Lesser General Public License Version 3.0 .

This page was last updated on 07. February 2016