SRF01 Ultrasonic Range Finder Working Code

I searched the internet and the forums trying to find code that worked for this range finder. I only found a couple of examples and neither worked. So a friend and myself set down and figured it out. We used the old code as our template and modified it.

All you have to do to make this work with your device is hook to 5v, ground, and digital pin 10.

Enjoy.

// Code modified and developed by Will Myers and Justin Miller

#include <SoftwareSerial.h>

#define txrxPin 10                                           // Defines Pin 10 to be used as both rx and tx for the SRF01
#define srfAddress 0x01                                      // Address of the SFR01
#define getSoft 0x5D                                         // Byte to tell SRF01 we wish to read software version
#define getRange 0x53                                        // Byte used to get range from SRF01 in inches
#define getStatus 0x5F                                       // Byte used to get the status of the transducer

SoftwareSerial UltrasonicBus(txrxPin, txrxPin);              // Sets up the SoftwareSerial with Digital Pin 10 and sets the name to "UltrasonicBus"

void setup(){
  Serial.begin(19200);
  
  UltrasonicBus.begin(9600);                                    
  delay(200);                                                // Waits some time to make sure that SRF01 is powered up
  Serial.println("SRF01 Test");                           
  SRF01_Cmd(srfAddress, getSoft);                            // Calls a function to get the SRF01 software version
  while (UltrasonicBus.available() < 1);                     // Waits to get good data
  int softVer = UltrasonicBus.read();                        // Read software version from SRF01
  Serial.print("V:");                                       
  Serial.println(softVer);                                   // Prints the software version to LCD03
  delay(200); 
  
  Serial.println("Initialization Complete");                 // After inititalization is complete you should see the correct version of your device printed to the serial monitor
}


void loop(){
 
  int max = 1;                                               // Setup so that you can averaqe readings
  int sum = 0;                                               // Currently I am not taking an average.. Max = 1
  for (int count = 0; count < max; count++) {
    sum = sum + doRange();                                   // Calls a function to get range from SRF01 and sums the ranges up so that you can take an avg
  }
  
  int range = sum / max;
 
  Serial.print("Range avg = ");                                
  Serial.print(range);                                       // Print range result to the screen
  Serial.println("  ");                                      // Print some spaces to the screen to make sure space direcly after the result is clear
  checkLock();                                               // Calls a function to check if the transducer is locked or unlocked
}


void SRF01_Cmd(byte Address, byte cmd){                      // Function to send commands to the SRF01
  pinMode(txrxPin, OUTPUT);                                  // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
  digitalWrite(txrxPin, LOW);                              
  delay(2);                                               
  digitalWrite(txrxPin, HIGH);                            
  delay(1);                                                
  UltrasonicBus.write(Address);                              // Send the address of the SRF01
  UltrasonicBus.write(cmd);                                  // Send commnd byte to SRF01
  pinMode(txrxPin, INPUT);                                   // Make input ready for Rx
  int availableJunk = UltrasonicBus.available();             // Filter out the junk data
  for(int x = 0; x < availableJunk; x++){
    byte junk = UltrasonicBus.read();
  }
}



void checkLock() {
  SRF01_Cmd(srfAddress, getStatus);                          // Call to a function that checks if the trancducer is locked or unlocked
  byte statusByte = UltrasonicBus.read();                    // Reads the SRF01 status, The least significant bit tells us if it is locked or unlocked
  int status = statusByte & 0x01;                            // Get status of lease significan bit
  if(status == 0)
  {                                      
    Serial.println("Unlocked");                              // Prints the word unlocked followd by a couple of spaces to make sure space after has nothing in
  }
   else 
  {                                      
    Serial.println("Locked   ");                             // Prints the word locked followd by a couple of spaces to make sure that the space after has nothing in
  }
}

int doRange() {
  SRF01_Cmd(srfAddress, getRange);                           // Calls a function to get range from SRF01
  while (UltrasonicBus.available() < 2);                     // Waits to get good data
  byte highByte = UltrasonicBus.read();                      // Get high byte
  byte lowByte = UltrasonicBus.read();                       // Get low byte
  int dist = ((highByte<<8)+lowByte);                        // Put them together
  return dist;
}

Thanks,

could be the beginning of a library ...

Hello,

We use 5 SRF01 on an arduino 2560.
We had made a code like yours for to have the range of one sensor and it works well.

But we try since 3 weeks to have the range of the others.
Did you try to use more than one SRF01 on your arduino board?With single pin or each pin for each sensors?

Thank you for your help and sorry for our bad english, we are two swiss student.

I was incredibly happy to find this, as I could not get my sensor working. Whenever I try to upload this code though, It says "error compiling". What might a solution be?

I get this error code:

c:/users/henry/desktop/stuff/arduino-1.0.1/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr6/crtm2560.o: In function __vector_default': (.vectors+0xcc): relocation truncated to fit: R_AVR_13_PCREL against symbol __vector_51' defined in .text.__vector_51 section in core.a(HardwareSerial.cpp.o)

I am using Ping ultrasonic sensors. I would like know how it is possible to get the analog values in arduino. Am a newbie to arduino. Using analogRead command i get only values 0. Can someone help me pls????

This thread is a little old, nevertheless I'm still interested in this topic. At the moment I'm trying to implement a SRF01 sensor in a project. Finding working code was pretty hard. I found this piece of code, written by James Henderson and modified by DFRobot. See link below:

http://www.dfrobot.com/wiki/index.php/SRF01_Ultrasonic_sensor_(SKU:SEN0004)

This code seemed to work. The sensor is accurate from 6-100 cm. Above 90cm it's not as accurate as it should be while the sensor should be accurate from 6cm-600cm. Anybody has any ideas how to solve this? Did I miss something important?

According to the specs of the SRF01 it can measure in inches and cm. Measuring in mm would suit my project better. Any ideas on how to get this done?

Thanks in Advance!