SRF02 ultrasonic range sensor

Driver for the SRF02 ultrasonic range sensor. More...

Detailed Description

Driver for the SRF02 ultrasonic range sensor.

Files

file  srf02.h
 Driver definitions for the SRF02 ultrasonic ranger.
 

Data Structures

struct  srf02_t
 Device descriptor for SRF02 sensors. More...
 

Macros

#define SRF02_DEFAULT_ADDR   (0xe0) /* 224 decimal */
 Default I2C address of SRF02 sensors.
 
#define SRF02_RANGE_DELAY   (70000U)
 The datasheet tells us, that ranging takes 70ms.
 

Enumerations

enum  srf02_mode_t {
  SRF02_MODE_REAL_INCH = 0x50, SRF02_MODE_REAL_CM = 0x51, SRF02_MODE_REAL_MS = 0x52, SRF02_MODE_FAKE_INCH = 0x56,
  SRF02_MODE_FAKE_CM = 0x57, SRF02_MODE_FAKE_MS = 0x58
}
 Possible measurement modes of the SRF02 sensor. More...
 

Functions

int srf02_init (srf02_t *dev, i2c_t i2c, uint8_t addr)
 Initialize the SRF02 ultrasonic sensor. More...
 
int srf02_trigger (const srf02_t *dev, srf02_mode_t mode)
 Trigger a new measurement. More...
 
uint16_t srf02_read (const srf02_t *dev)
 Read the results of the last ranging operation. More...
 
uint16_t srf02_get_distance (const srf02_t *dev, srf02_mode_t mode)
 Get the distance measured from the SRF02 ultrasonic sensor. More...
 
int srf02_set_addr (srf02_t *dev, uint8_t new_addr)
 Program the given device with a new bus address. More...
 

Enumeration Type Documentation

◆ srf02_mode_t

Possible measurement modes of the SRF02 sensor.

Enumerator
SRF02_MODE_REAL_INCH 

result in inches

SRF02_MODE_REAL_CM 

result in centimeters

SRF02_MODE_REAL_MS 

result in microseconds

SRF02_MODE_FAKE_INCH 

result in inches (no pulse send)

SRF02_MODE_FAKE_CM 

result in cm (no pulse send)

SRF02_MODE_FAKE_MS 

result in ms (no pulse send)

Definition at line 56 of file srf02.h.

Function Documentation

◆ srf02_get_distance()

uint16_t srf02_get_distance ( const srf02_t dev,
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
[in]devdevice descriptor of an SRF02 sensor
[in]modethere 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
Returns
the ranging result in inches, centimeters or microseconds

◆ srf02_init()

int srf02_init ( srf02_t dev,
i2c_t  i2c,
uint8_t  addr 
)

Initialize the SRF02 ultrasonic sensor.

Parameters
[in]devdevice descriptor of an SRF02 sensor
[in]i2cI2C device the sensor is connected to
[in]addrI2C address of the sensor
Returns
0 on successful initialization
-1 on error

◆ srf02_read()

uint16_t srf02_read ( const srf02_t dev)

Read the results of the last ranging operation.

Parameters
[in]devdevice to read from
Returns
result of the last ranging operation, meaning depends on the mode parameter given to the srf02_trigger function

◆ srf02_set_addr()

int srf02_set_addr ( srf02_t dev,
uint8_t  new_addr 
)

Program the given device with a new bus address.

Parameters
[in]devdevice to program
[in]new_addrnew address to program the given device to
Returns
0 On success, otherwise i2c_write error

◆ srf02_trigger()

int srf02_trigger ( const srf02_t dev,
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
[in]devdevice to trigger
[in]modethere 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
Returns
0 On success, otherwise i2c_write error