Using the SRF02 Ultrasonic range finder in I2C mode

The SRF02 is a single transducer ultrasonic rangefinder. It features both I2C and a Serial interfaces. This article illustrates how to use it in I2C mode (see the I2C specifications).

WARNING To try this tutorial you have to update your Debian Linux kernel image: How to update the kernel image.

Wiring

Be carefull to insert the 2K2 resistors to avoid damage on the FOXG20 GPIO lines that are at 3.3 volt

Example on code in Python

To manage the I2C bus in Python it need to install the smbus module available as Debian package and installable typing:

debarm:~# apt-get update
...
debarm:~# apt-get install python-smbus

This is an example of code that gets the distance in centimeters:

File: http://foxg20.acmesystems.it/download/examples/sfr02.py -

#!/usr/bin/python
import smbus
import time
 
# Define a class called Ranger
 
class Ranger():
	# Select the /dev/i2c-0 device
	b = smbus.SMBus(0)
 
	# Read the distance 
	def getValue(self):
		# Write in the command register (0x00) the command 
		# 0x51 (Real Ranging Mode - Result in centimeters)
		# using the default sfr02 I2C address 0x70
		self.b.write_byte_data(0x70,0x00,0x51)
 
		# Wait as explained on the datasheet
		time.sleep(0.066)
 
		# Read the hi value
		h = self.b.read_byte_data(0x70,0x02)
 
		# Read the low value
		l = self.b.read_byte_data(0x70,0x03)
 
		# Return the range in cm	
		return h*256+l 	
 
# Create a Ranger object called sfr02 
sfr02 = Ranger()
 
# Get the distance from it
print sfr02.getValue(), "cm"

To execute it type:

debarm:~# python sfr02.py
33 cm

Example on code in C

This is an example of C code that read the range in cm and type it on the terminal session. To try this example in C you can install the GCC directly on the FOX Board G20 and compile it typing:

debarm:~# gcc sfr02.c -o sfr02
debarm:~# ./sfr02
Range=287 cm

File: http://foxg20.acmesystems.it/download/examples/sfr02.c -

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h> 
#include "/usr/include/linux/i2c-dev.h"
 
int main(void) 
{
	int fd;
	char filename[20];
	char buf[10];
	int res;
	int range=0;
 
	sprintf(filename, "/dev/i2c-0");
	fd = open(filename, O_RDWR);
	if (fd < 0) {
		printf("Error on open\n");
		exit(1);
	}
 
	if (ioctl(fd, I2C_SLAVE, 0x70) < 0) {
		printf("Error on slave address\n");
		exit(1);
	}
 
 
	buf[0] = 0x00;
	buf[1] = 0x51;
	if ((write(fd,buf,2))!=2) {
		printf("Error send the read command\n");
		exit(1);
	}
 
	// Wait for the measurement
	usleep(66000);
 
	buf[0] = 0x02;
	if ((write(fd,buf,1))!=1) {
		printf("Error on select the Range High Byte\n");
		exit(1);
	}
 
	if ((read(fd,buf,1))!=1) {
		printf("Error on read the Range High Byte\n");
		exit(1);
	}
	range = buf[0]<<8;
 
	buf[0] = 0x03;
	if ((write(fd,buf,1))!=1) {
		printf("Error on select the Range Low Byte\n");
		exit(1);
	}
 
	if ((read(fd,buf,1))!=1) {
		printf("Error on read the Range Low Byte\n");
		exit(1);
	}
	range |= buf[0];
 
 
	printf("Range=%d cm\n",range);	
	close(fd);
 
	return 0;	
}
 
tutorial/srf02.txt · Last modified: 2010/06/04 15:00 by tanzox
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki