SRF02 ultrasonic range sensor

Driver for the SRF02 ultrasonic range sensor.

enum srf02_mode_t
SRF02_MODE_REAL_INCH = 0x50
result in inches
SRF02_MODE_REAL_CM = 0x51
result in centimeters
SRF02_MODE_REAL_MS = 0x52
result in microseconds
SRF02_MODE_FAKE_INCH = 0x56
result in inches (no pulse send)
SRF02_MODE_FAKE_CM = 0x57
result in cm (no pulse send)
SRF02_MODE_FAKE_MS = 0x58
result in ms (no pulse send)
int srf02_init(srf02_t * dev, i2c.h::i2c_t i2c, uint8_t addr)

Initialize the SRF02 ultrasonic sensor.

Parameters

dev:device descriptor of an SRF02 sensor
i2c:I2C device the sensor is connected to
addr:I2C address of the sensor

Return values

  • 0 on successful initialization
  • -1 on error
int srf02_trigger(const srf02_t * dev, srf02.h::srf02_mode_t mode)

Trigger a new measurement.

This function triggers a new ranging operation. After triggering this operation, you have to wait at least 70ms for the result to be ready.

The result of the ranging operation is returned in inches, centimeters or microseconds - depending on the given mode parameter.

Parameters

dev:device to trigger
mode:there are three real ranging modes, which return the result in inches, centimeters or microseconds. Another set of three fake ranging modes do the same but without transmitting the burst

Return values

  • 0 On success, otherwise i2c_write error
uint16_t srf02_read(const srf02_t * dev)

Read the results of the last ranging operation.

Parameters

dev:device to read from

Return values

  • result of the last ranging operation, meaning depends on the mode parameter given to the srf02_trigger function
uint16_t srf02_get_distance(const srf02_t * dev, srf02.h::srf02_mode_t mode)

Get the distance measured from the SRF02 ultrasonic sensor.

This function combines the srf02_trigger and the srf02_read functions for simplified usage in simple (single sensor) setups.

Parameters

dev:device descriptor of an SRF02 sensor
mode:there are three real ranging modes, which return the result in inches, centimeters or microseconds. Another set of three fake ranging modes do the same but without transmitting the burst

Return values

  • the ranging result in inches, centimeters or microseconds
int srf02_set_addr(srf02_t * dev, uint8_t new_addr)

Program the given device with a new bus address.

Parameters

dev:device to program
new_addr:new address to program the given device to

Return values

  • 0 On success, otherwise i2c_write error
SRF02_DEFAULT_ADDR

Default I2C address of SRF02 sensors.

1
(0xe0)      /* 224 decimal */
SRF02_RANGE_DELAY

The datasheet tells us, that ranging takes 70ms.

1
(70000U)
struct srf02_t

Device descriptor for SRF02 sensors.

i2c.h::i2c_t i2c

I2C device the sensor is connected to.

uint8_t addr

I2C bus address of the sensor.