SRF08 Ultra Sonic Range Finder

SRF08 Sensor layout

Circuit

Below is the diagram for setting up the SRF08 sensor for a typical Arduino. The only additional circuitry necessary are two 1.8kOhm pullup resistors as seen below.

Circuit diagram

BreadBoard

Code

  1. /*
  2. * SRF08.ino - example interfacing with sonar sensor SRF08
  3. *       library uses Wire.h
  4. *               SDA - Analog Pin 4
  5. *               SCL - Analog Pin 5
  6. *
  7. * Authors:
  8. *       - Zach Foresta - Foureza87@yahoo.com (April 2009)
  9. *       - Leo Colomb - @LeoColomb (May 2012)
  10. *
  11. * For component setup - http://playground.arduino.cc/Main/SonarSrf08
  12. */
  13.  
  14. #include <Wire.h>
  15. #include <SonarSRF08.h>
  16.  
  17. SonarSRF08 MySonar;
  18.  
  19. #define CommandRegister 0x00
  20. int New_Address = 248; // 0xF8
  21. #define ResultRegister  0x02
  22. #define GainRegister 0x00 // Setup Analogue Gain -- http://www.robot-electronics.co.uk/htm/srf08tech.html section "Analogue Gain"
  23. #define RangeLocation 0xFF // Setup Range Location -- http://www.robot-electronics.co.uk/htm/srf08tech.html section "Changing the Range"
  24.  
  25. int DEBUG = 1;
  26. char unit = 'c'; // 'i' for inches, 'c' for centimeters, 'm' for micro-seconds
  27. float sensorReading = 0;
  28. int time = 70;
  29.  
  30.  
  31. void setup()
  32. {
  33.   MySonar.connect();  
  34.   MySonar.changeAddress(CommandRegister, New_Address, GainRegister, RangeLocation);
  35.   if (DEBUG){
  36.     Serial.begin(9600);
  37.   }
  38.   New_Address += 4;
  39.   // offset address not sure why this is but it works for this address
  40. }
  41.  
  42. void loop()
  43. {
  44.   // set units, gain, and range location for reading out distance
  45.   MySonar.setUnit(CommandRegister, New_Address, unit, GainRegister, RangeLocation);
  46.   // pause
  47.   delay(time);
  48.   // set register for reading
  49.   MySonar.setRegister(New_Address, ResultRegister);
  50.   // read data from result register
  51.   sensorReading = MySonar.readData(New_Address, 2);
  52.   // print out distance
  53.   Serial.print("Distance: ");
  54.   Serial.print(sensorReading);
  55.   Serial.print(" units");
  56.   Serial.println();
  57.   // pause
  58.   delay(time);
  59. }

Useful Links

Share