Account
Projects Forums Code & Files Design Sketches Photos Videos Chat Sites People Sounds  
 
  •  
 
 
Articles
07.02.2010

This code reads a Devantech SRF02 ultrasonic ranger (and probably an SRF08 and SRF10 as well) It uses the Wire library for Wiring and Arduino.

 

/*

  SRF02 sensor reader

 language: Wiring/Arduino Reads data from a Devantech SRF02 ultrasonic sensor.

 Should also work for the SRF08 and SRF10 sensors as well.

Sensor connections:

 SDA - Analog pin 4

 SCL - Analog pin 5

created 5 Mar. 2007

 by Tom Igoe

*/

// include Wire library to read and write I2C commands:

#include 

// the commands needed for the SRF sensors:

#define sensorAddress 0x70

#define readInches 0x50

// use these as alternatives if you want centimeters or microseconds:

#define readCentimeters 0x51

#define readMicroseconds 0x52

// this is the memory register in the sensor that contains the result:

#define resultRegister 0x02

void setup()

{

  // start the I2C bus

  Wire.begin();

  // open the serial port:

  Serial.begin(9600);

}

void loop()

{

  // send the command to read the result in inches:

  sendCommand(sensorAddress, readInches);

  // wait at least 70 milliseconds for a result:

  delay(70);

  // set the register that you want to reas the result from:

  setRegister(sensorAddress, resultRegister);

// read the result:

  int sensorReading = readData(sensorAddress, 2);

  // print it:

  Serial.print("distance: ");

  Serial.print(sensorReading);

  Serial.println(" inches");

  // wait before next reading:

  delay(70);

}

/*

  SendCommand() sends commands in the format that the SRF sensors expect

 */

void sendCommand (int address, int command) {

  // start I2C transmission:

  Wire.beginTransmission(address);

  // send command:

  Wire.send(0x00);

  Wire.send(command);

  // end I2C transmission:

  Wire.endTransmission();

}

/*

  setRegister() tells the SRF sensor to change the address pointer position

 */

void setRegister(int address, int thisRegister) {

  // start I2C transmission:

  Wire.beginTransmission(address);

  // send address to read from:

  Wire.send(thisRegister);

  // end I2C transmission:

  Wire.endTransmission();

}

/*

readData() returns a result from the SRF sensor

 */

int readData(int address, int numBytes) {

  int result = 0;        // the result is two bytes long

// send I2C request for data:

  Wire.requestFrom(address, numBytes);

  // wait for two bytes to return:

  while (Wire.available() < 2 )   {

    // wait for result

  }

  // read the two bytes, and combine them into one int:

  result = Wire.receive() * 256;

  result = result + Wire.receive();

  // return the result:

  return result;

}
 
Comments
Order by: 
Per page: 
 
  •  jackpercy wrote 105 Days Ago (neutral) 
     
    0
    October 13, 5:30 p.m. or so, in foshan, Canada Goose sale guangdong HuangQi traffic accident happened, a van to hit a 2 years after the girls, make a bit, but did not hesitate to get off, but to start saving once again from the girl who run over, and then have a car run again. In a few minutes after, 18 people who pass through the scene, but no one reaches or call the police, until 19 th also is a SheHuang aunt found only after the lift. According to the latest report, girls Canada goose parka sale has been brain death, life could happen at any time, even dangerous estimated future brain-damaged states can't recover. http://www.canadagoosejacket2011sale.com/
     
       
     
     
    -2 points
     
  • Comment by asicsning is hidden. Show.
    -4 points
     
  •  asicsning wrote 176 Days Ago (neutral) 
     
    0
    http://www.toryburchusashoes.com/
     
       
     
     
    -4 points
     
  •  asicsning wrote 189 Days Ago (neutral) 
     
    0
    http://www.ghdstraightenersuksale.co.uk/
     
       
     
     
    -3 points
     
  •  asicsning wrote 195 Days Ago (neutral) 
     
    0
    [url=http://www.mbttrainersuksale.co.uk/]MBT[/url] [url=http://www.mbtukshoessale.co.uk/]MBT Shoes[/url] [url=http://www.oakleyuksunglassessale.co.uk/]Oakley Sunglasses[/url] [url=http://www.cheapghdstraightenerssale.co.uk/]GHD Straighteners[/url] [url=http://www.cheapuggsaleuk.co.uk/]UGG Boots[/url]
     
       
     
     
    -3 points
     
Actions
Rating
0 votes
Copyright © 2012 Arduino User Community -
Not affiliated with or endorsed by Arduino (Arduino.cc)