Read ultrasonic distance sensor

<br />


Basic Example how to read an ultrasonic distance sensor.


/* read ultrasonic distance sensor

 *
 * read an ultrasonic distance sensor using I2C bus
 *
 * uses SRF02 library from grapelabs, http://www.grapelabs.de
 * uses metro library, http://www.arduino.cc/playground/Code/Metro
 *
 * remove red jumper near built-in poti to use I2C bus
 * connect plug with three wires on A5 and plug with one wire on A4
 *

 * SensorAktor Workshop
 * Lab3 2008
 * Kunsthochschule fuer Medien Koeln
 * Academy of Media Arts Cologne
 * http://interface.khm.de

 * pin mapping SensorAktor Shield
 * import SensorAktor.h to get already defined mapping
 *
 * analog inputs: analog0 = pin0, analog1 = pin1, analog2 = pin2,
 * analog3 = pin3, analog4 = pin4, analog5 = pin5
 * built-in poti: poti = pin5
 * amplified input: mic = pin0
 * switches: switch1 = pin1, switch2 = pin2, switch3 = pin3
 * leds: led1 = pin3, led2 = pin5, led3 = pin6
 * power outs: out1 = pin3, out2 = pin5, out3 = pin6
 * motor outs: motor1_1 = pin8, motor1_2 = pin13, motor2_1 = pin11, motor2_2 = pin12
 * motor pwms: pwmMotor1 = pin10, pwmMotor2 = pin9
 * servo outs: servo1 = pin2, servo2 = pin4, servo3 = pin7

 */

// import sensor aktor library for pin mapping
#include <SensorAktor.h>

// import wire library to use I2C bus
#include "Wire.h"

// import SRF02 library
#include "SRF02.h"
// create instance for ultrasonic distance sensor
SRF02 distanceSensor(0x70, SRF02_CENTIMETERS);

// import metro library (for serial output)
#include
// create instace for metro
Metro serialMetro = Metro(10);

void setup()
{

  // start serial communication (for printing to serial port)
  Serial.begin(9600);

  // start I2C communication
  Wire.begin();

  // set interval for ultrasonic distance sensor
  SRF02::setInterval(100);

}

void loop()
{

  // update sensor data
  SRF02::update();

  // check serial metro
  if(serialMetro.check() == 1){

    // print sensor value to serial port
    Serial.println(distanceSensor.read());

  }

}


To use the SFR02 library include it in the header of your code and define a variable for the distance sensor. You also have to include the Wire library, because the ultrasonic distance sensor is connected to the I2C bus. You can find more informations about the I2C bus at http://arduino.cc/en/Reference/Wire.


// import SRF02 library
#include "SRF02.h"

// create instance for ultrasonic distance sensor
SRF02 distanceSensor(0x70, SRF02_CENTIMETERS);

// import metro library (for serial output)
#include


Start the I2C communication and set the interval for measuring the ultrasonic distance sensor.


void setup()
{

  // start serial communication (for printing to serial port)
  Serial.begin(9600);

  // start I2C communication
  Wire.begin();

  // set interval for ultrasonic distance sensor
  SRF02::setInterval(100);

}


In the loop() function you have to update the measuring. To read the sensor use the read() function.


void loop()
{

  // update sensor data
  SRF02::update();

  // print sensor value to serial port
  Serial.println(distanceSensor.read());

}


Files needed

Since Arduino 0015 there are some problems with the old “SensorAktor.h” file.

Use this one instead.


SensorAktor.zip

readultrasonicdistancesensor.zip