zeldor.biz

Linux, programming and more

Copyright © 2023
Log in

Arduino SRF08

October 18, 2012 by Igor Drobot 4 Comments

The speed of the sound in the dry air (20° C) is around 343 m/s. Send a short ultrasonic pulse at Frequency of 40Khz in the air, and try to listen to the echo. Of course you won’t hear anything (only a little click noise), but with an ultrasonic sensor the back pulse can be detected. If you know the time of the forth & back travel of the ultrasonic wave, you know the distance, divide the distance by two and you know the range from the ultrasonic sensor to the first obstacle in front of it.

Specifications
Voltage – 5v only required
Current – 15mA Typ. 3mA Standby.
Frequency – 40KHz
Range – 1cm – 6m.
Max Analogue Gain – Variable 94 to 1025 in 32 steps.
Connection – Standard I2C Bus.
Light Sensor – Front Facing light sensor.
Timing – Fully timed echo, freeing host controller of task.
Echo – Multiple echo – keeps looking after first echo.
Units – Range reported in uS, cm or inches.
Size – 43mm x 20mm x 17mm height.

If you asking what is it for a free pin?
The free (Do Not Connect pin) should be left unconnected. It is actually the CPU MCLR line and is used once only in production for programming;)

Connection schematic (click to resize):

Fritzing Sampler

Arduino Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <wire.h>
#define srfAddress 0x70                           // Address of the SRF08
#define cmdByte 0x00                              // Command byte
#define lightByte 0x01                            // Byte to read light sensor
#define rangeByte 0x02                            // Byte for start of ranging data
 
byte highByte = 0x00;                             // Stores high byte from ranging
byte lowByte = 0x00;                              // Stored low byte from ranging
 
void setup(){
  Wire.begin();                                   // Waits to make sure everything is powered up before sending or receiving data
  Serial.begin(9600);
  delay(100);
}
 
void loop(){
  int rangeData = getRange();                     // Calls a function to get range
  Serial.print("Range:");
  Serial.print(rangeData);
  Serial.print("\n\n");
 
  int lightData = getLight();                     // Calls a function to get range
  Serial.print("Light:");
  Serial.print(lightData);
  Serial.println();
  delay(70);                                      // Wait before looping
}
 
int getRange(){                                   // This function gets a ranging from the SRF08
 
  int range = 0; 
 
  Wire.beginTransmission(srfAddress);             // Start communticating with SRF08
  Wire.send(cmdByte);                             // Send Command Byte
  Wire.send(0x51);                                // Send 0x51 to start a ranging
  Wire.endTransmission();
 
  delay(100);                                     // Wait for ranging to be complete
 
  Wire.beginTransmission(srfAddress);             // start communicating with SRFmodule
  Wire.send(rangeByte);                           // Call the register for start of ranging data
  Wire.endTransmission();
 
  Wire.requestFrom(srfAddress, 2);                // Request 2 bytes from SRF module
  while(Wire.available() &lt; 2);                    // Wait for data to arrive
  highByte = Wire.receive();                      // Get high byte
  lowByte = Wire.receive();                       // Get low byte
 
  range = (highByte &lt;&lt; 8) + lowByte;              // Put them together
 
  return(range);                                  // Returns Range
}
 
int getLight(){                                   // Function to get light reading
 
  Wire.beginTransmission(srfAddress);
  Wire.send(lightByte);                           // Call register to get light reading
  Wire.endTransmission();
 
  Wire.requestFrom(srfAddress, 1);                // Request 1 byte
  while(Wire.available() &lt; 0);                    // While byte available
  int lightRead = Wire.receive();                 // Get light reading
 
  return(lightRead);                              // Returns lightRead 
}</wire.h>

#include <wire.h> #define srfAddress 0x70 // Address of the SRF08 #define cmdByte 0x00 // Command byte #define lightByte 0x01 // Byte to read light sensor #define rangeByte 0x02 // Byte for start of ranging data byte highByte = 0x00; // Stores high byte from ranging byte lowByte = 0x00; // Stored low byte from ranging void setup(){ Wire.begin(); // Waits to make sure everything is powered up before sending or receiving data Serial.begin(9600); delay(100); } void loop(){ int rangeData = getRange(); // Calls a function to get range Serial.print("Range:"); Serial.print(rangeData); Serial.print("\n\n"); int lightData = getLight(); // Calls a function to get range Serial.print("Light:"); Serial.print(lightData); Serial.println(); delay(70); // Wait before looping } int getRange(){ // This function gets a ranging from the SRF08 int range = 0; Wire.beginTransmission(srfAddress); // Start communticating with SRF08 Wire.send(cmdByte); // Send Command Byte Wire.send(0x51); // Send 0x51 to start a ranging Wire.endTransmission(); delay(100); // Wait for ranging to be complete Wire.beginTransmission(srfAddress); // start communicating with SRFmodule Wire.send(rangeByte); // Call the register for start of ranging data Wire.endTransmission(); Wire.requestFrom(srfAddress, 2); // Request 2 bytes from SRF module while(Wire.available() &lt; 2); // Wait for data to arrive highByte = Wire.receive(); // Get high byte lowByte = Wire.receive(); // Get low byte range = (highByte &lt;&lt; 8) + lowByte; // Put them together return(range); // Returns Range } int getLight(){ // Function to get light reading Wire.beginTransmission(srfAddress); Wire.send(lightByte); // Call register to get light reading Wire.endTransmission(); Wire.requestFrom(srfAddress, 1); // Request 1 byte while(Wire.available() &lt; 0); // While byte available int lightRead = Wire.receive(); // Get light reading return(lightRead); // Returns lightRead }</wire.h>

Open console to see the measurements results:

Filed Under: DIY, Linux, Programming Tagged With: arduino, Sonar, SRF08, Ultra Sonic

Categories

Archives

Tags

apache2 Apple arduino ARM Automation backup bash Cisco Cluster Corosync Database Debian Debian squeeze DIY DNS Fedora FTP Fun Icinga Ipv6 KVM Linux LVM MAC OS X Monitoring MySQL Nagios Nginx openSUSE OpenVPN PHP Proxy Python python3 qemu RAID rsync Samba security ssh Ubuntu virtualization Windows Windows 7 Wordpress

Comments

  1. Ahmed Ghaffar says

    April 4, 2018 at 16:34

    what is “lt” ??

  2. Chinemelu Ezeh says

    May 7, 2017 at 21:25

    Hi Zeldor,

    I have started using the srf08.

    I have noticed that when I reduce the gain and range to allow for rapid readings, I get a consistent offset in my readings so that a real life zero is mapped to another value.

    Do you know the cause of this and how I could go around this issue? Currently on startup, I measure the offset ensuring nothing is in the way of the sensor and then subtract the offset from subsequent readings.

    However, I don’t want to have to recalibrate overtime
    .
    Looking forward to a response.

    Thank you.
    Chi

  3. arcde says

    December 20, 2013 at 11:03

    do i need to definethe pin used?

  4. arcde says

    December 20, 2013 at 10:44

    hi. ive tried the coding n its not working. the serial monitor showed blank. the verify n upload works fine but it show no result on the serial monitor. where shuld i check first for problem?

Leave a Reply

Your email address will not be published. Required fields are marked *