Simple library for reading distance from SRF02 Ultrasonic Sensor in I2C mode.

Dependents:   Project_A el13jarDistanceSensorProject UltrasonicDistanceSensor-el13jb Distance_Sensor_SRF02 ... more

Embed: (wiki syntax)

« Back to documentation index

SRF02 Class Reference

SRF02 Class Reference

Library for interfacing with SRF02 Ultrasonic Sensor in I2C. More...

#include <SRF02.h>

Public Member Functions

 SRF02 (PinName sdaPin, PinName sclPin, char addr)
 Create a SRF02 object connected to the specified I2C pins.
int getDistanceCm ()
 Read distance in centimetres.
void changeAddress (char addr)
 Change I2C address of SRF02 sensor.

Detailed Description

Library for interfacing with SRF02 Ultrasonic Sensor in I2C.

See also:
http://www.robot-electronics.co.uk/htm/srf02tech.htm

Revision 1.1

Author:
Craig A. Evans
Date:
June 2016

Example:

 #include "mbed.h"
 #include "SRF02.h"
 
 SRF02 sensor(p28,p27,0xE0);  // SDA, SCL, address

 int main() {

    while(1) {

        // read sensor distance in cm and print over serial port
        int distance = sensor.getDistanceCm();
        serial.printf("Distance = %d cm\n",distance);
        // short delay before next measurement
        wait(0.5);

    }
}

Definition at line 55 of file SRF02.h.


Constructor & Destructor Documentation

SRF02 ( PinName  sdaPin,
PinName  sclPin,
char  addr 
)

Create a SRF02 object connected to the specified I2C pins.

Parameters:
sdaPin- mbed SDA pin
sclPin- mbed SCL pin
addr- write address of the SRF02 sensor

Definition at line 10 of file SRF02.cpp.


Member Function Documentation

void changeAddress ( char  addr )

Change I2C address of SRF02 sensor.

Parameters:
address-
See also:
https://www.robot-electronics.co.uk/htm/srf02techI2C.htm for valid addresses

Definition at line 50 of file SRF02.cpp.

int getDistanceCm (  )

Read distance in centimetres.

Returns:
distance in centimetres (int)

Definition at line 21 of file SRF02.cpp.