Example Code for Arduino-I2C Mode
Last revision 2025/12/30
Test the SRF02 ultrasonic sensor using I2C mode with Arduino UNO. Users can learn how to communicate with the SRF02 via I2C and read distance measurements in centimeters.
Hardware Preparation
- SRF02 Ultrasonic Sensor, SKU:SEN0005, Quantity:1, Purchase Link
- Arduino UNO, Quantity:1
Software Preparation
- Development Tool: Arduino IDE (version 1.8.x or later). Download link
- Library: Wire library (included with Arduino IDE, no additional installation needed)
Wiring Diagram
| SRF02 | Arduino |
|---|---|
| 5v Vcc | 5V |
| SDA | A4 |
| SCL | A5 |
| Mode | no connection |
| 0v Ground | GND |
Other Preparation Work
Ensure the "Mode" pin of the SRF02 is left unconnected (for I2C mode). Connect the SRF02 to Arduino UNO as per the wiring diagram.
Sample Code
/*
Sample code for test the SRF02 with the I2C mode based on Arduino UNO!
Command for reference:http://robot-electronics.co.uk/htm/srf02techI2C.htm
Connection:
SRF02 Arduino
5v Vcc -> 5V
SDA -> A4
SCL -> A5
Mode -> no connection
0v Ground -> GND
*/
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial communication at 9600bps
}
int reading = 0;
void loop()
{
// step 1: instruct sensor to read echoes
Wire.beginTransmission(112); // 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.write(byte(0x00)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x51)); // command sensor to measure in "centimeters" (0x51)
// 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(112); // transmit to device #112
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(112, 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.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
Serial.print(reading); // print the reading
Serial.println("cm");
}
delay(250); // wait a bit since people have to read the output :)
}
Result
Open the Serial Monitor in Arduino IDE with baud rate 9600. You should see the distance measured by the SRF02 in centimeters printed continuously.
Additional Information
Command reference link: http://robot-electronics.co.uk/htm/srf02techI2C.htm
Was this article helpful?
