SRF02 Ultra Sonic Range Finder With I2C Interface

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
Harry Tuttle
Posts: 27
Joined: Sat Aug 01, 2009 3:15 pm
Has thanked: 2 times
Been thanked: 10 times
Contact:

SRF02 Ultra Sonic Range Finder With I2C Interface

Post by Harry Tuttle »

The SRF02 and it’s similar siblings provide simple and affordable range finding in a small package. The default address of the SRF02 can be changed allowing up to 16 devices to be connected to the same two wire I2C (Inter-Integrated Circuit) bus.
Depending on which transmit command is sent to the device, the returned result will be in centimetres, inches, or microseconds.

More reading

I2C Bus Explained:
http://www.robot-electronics.co.uk/htm/ ... 2c_bus.htm

SRF Ultra Sonic Range Finders:
http://www.robot-electronics.co.uk/acat ... ngers.html

The Processor

Microchip’s 16F877A supports I2C Bus as both master and slave on Port C and has plenty of scope for adding other components.
The connections are,
Pin 18 is SCL the Synchronous serial clock input/output
Pin 23 SDA the data input/output.

More reading

16F877A Datasheet:
http://ww1.microchip.com/downloads/en/D ... 39582b.pdf

Connections
1.jpg
(56.09 KiB) Downloaded 10995 times

The E-blocks EB006 MCU Multiprogrammer is fitted with EB005 LCD Board on Port A (J2).
Port C (J4) is connected to two 1800 ohm resistors which are used to pull up the SCL and SDA lines to 5volt.

(Make up a male D connector with wires connected to pin 4 and 5. ( RC3 & RC4 )
Take these to your breadboard or veroboard and connect the resistors as shown using the 5volt line from the programmer board. Loop out three wires and connect to the SRF02, also connect GND back to the programmer board.)

The Software
SRF02.fcf
Version3
(11.5 KiB) Downloaded 1314 times
Calculation
The Variables Usonic_R and Usonic_W are set to the default address of the SRF02, 0xE1 and 0xE0 respectively. If a device with a different address is used you only have to alter these variables.

ReadSoftWareVersion
To check that the device is connected the software version is read.
If there is a problem with the connections or device address, 255 will be returned and “FAULT” displayed for 2 seconds. The programme will then loop back and try again.

If all is well “Version 5” or similar will be displayed for 2 seconds.

The main part of the programme is started.

UltraSonicTX
By sending 0x51 (decimal 81) to Command Register 0 a ranging session is started that will give a result in centimetres.
If 0x50 (decimal 80) is sent the result will be in Inches.
If 0x52 (decimal 82) is sent the result will be in micro-seconds.

Delay of 70mSec allow ranging to take place. ( The device will not respond while ranging so if you do not want your processor tied up read the software version until the device responds. See SRF02 link above)

UltraSonicRX
Read registers 2 and 3 as RangeHighByte and RangeLowByte. This is the 16 bit range in centimetres split into two bytes.

Calculation
Combine RangeHighByte and RangeLowByte into the integer Range.
RangeHighByte is shifted on byte left then RangeLowByte is added.

If RangeHighByte was 0000 0000 and RangeLowByte was 0000 0001
The range would be 1cm. (1 in binary)

If RangeHighByte was 0000 0000 and RangeLowByte was 1111 1111
The range would be 255cm. (1111 1111 in binary)

If RangeHighByte was 0000 0001 and RangeLowByte was 0000 0000
The range would be 256cm. (10000 0000 in binary)

If RangeHighByte was 0000 0001 and RangeLowByte was 0000 0001
The range would be 257cm. (10000 0001 in binary)

DisplayResult
The range is then sent to the LCD.

Connection Point
Loop back for another ranging session.

KeithSloan
Posts: 114
Joined: Fri Jul 27, 2007 10:50 am
Been thanked: 1 time
Contact:

Re: SRF02 Ultra Sonic Range Finder With I2C Interface

Post by KeithSloan »

I tried to use this program to understand the I2C component and must admit it had me confused.

When reading the software version why is 0x00 initially written? ( The Program displays Register 0x02) very confusing.
I cannot see anything about this in the SRF02 technical specs.

Harry Tuttle
Posts: 27
Joined: Sat Aug 01, 2009 3:15 pm
Has thanked: 2 times
Been thanked: 10 times
Contact:

Re: SRF02 Ultra Sonic Range Finder With I2C Interface

Post by Harry Tuttle »

Hi Keith,
My mistake the Component Macro lable should read "Register 0x00", a copy paste mishap.

Register (or Location) 0 if read returns the software version, but it is also the command register the only register that can be written to.
11 unique commands can be sent to Register 0, for more details see:

http://www.robot-electronics.co.uk/htm/srf02techI2C.htm

Of the 11 possible commands, to get a range in cm the value 0x51 is sent to Register 0.
The device sends out a ping, starts counting and listens until the the echo returns.
It then works out the time taken and from this the distance in cm.
This value is then written to Register 2 and 3. To allow time for this to happen and for the ping to fade away I have added a 70mS delay.
The values stored in Register 2 and 3 are then read from the device in the UltraSonicRX macro ( I should have called it ReadRange perhaps!!)

The programme worked fine in hardware, if you have a SRF02 give it a go.
I found I2C very confusing (which is why I have posted these examples to save others some time) but it is so nice to be able to read values straight from a device without having to do lots of maths or use lookup tables.

Hope this helps,
ben

DISCLAIMER:I am sure that there is room for improvement in my programmes!!

KeithSloan
Posts: 114
Joined: Fri Jul 27, 2007 10:50 am
Been thanked: 1 time
Contact:

Re: SRF02 Ultra Sonic Range Finder With I2C Interface

Post by KeithSloan »

Hi Harry

Sorry still confused. When reading the software version, It looks like you start with a write to register 0. ( your label has 0x02). What is the purpose of this write I cannot see from the documentation. Surely you could just do a read of register 0 to get the software version.

My assertion is that if you look at ReadSoftwareVersion thread and delete the three component macro's that follow
Start_IC2 i.e. delete those labeled Usonic_W, Register 0x02 and Restart I2C the application will still work and that
these three component macro's are redundant.

I have an SRF08 and was trying to understand the I2C flowcode as I am still new to flowcode. The result was that
I was initially totally confused about what was going on.

It would be good to tidy up this issue so that others that follow don't get confused as I did.

Keith

Harry Tuttle
Posts: 27
Joined: Sat Aug 01, 2009 3:15 pm
Has thanked: 2 times
Been thanked: 10 times
Contact:

Re: SRF02 Ultra Sonic Range Finder With I2C Interface

Post by Harry Tuttle »

Did you read the explanation here:

http://www.robot-electronics.co.uk/htm/ ... 2c_bus.htm

I was looking at your other post and see that you made an I2C board and were using ultrasonic rangers back in 2007, any chance that you could post the code?

KeithSloan
Posts: 114
Joined: Fri Jul 27, 2007 10:50 am
Been thanked: 1 time
Contact:

Re: SRF02 Ultra Sonic Range Finder With I2C Interface

Post by KeithSloan »

Okay now understand - I will have to eat crow - I see to read a register you first need to write to the register.

I just checked my code and do seem to be doing writes first. So long since I wrote it I had forgotten. I am sure I got my info from some microchip documentation. Like you say its quite a few years ago.The robot electronics documentation seem quite clear.

I can email you the Assembler code, but it was just a crude hack to test things out. Contains a lot of debugging code to output different vales to a 7 segment display so I could tell where it was getting to as I debugged it.

Send me a pm with an email address and I can send it

Isra
Posts: 2
Joined: Tue Apr 08, 2014 4:10 pm
Contact:

Re: SRF02 Ultra Sonic Range Finder With I2C Interface

Post by Isra »

Hello,

I am working on SRF02 as well. Trying to interface it with PIC18f4520 via I2c using CCs compiler. I am able to communicate with the sensor, but the readings that it gives back is not the distance in cms. When any obstacle is very close to it, it gives unstable reading. But once the object is about 20 cms away then it start giving reading like 10 then 11. But it doesn't seem to be correct reading. Below is my coding. Any help would be very much appreciated. Thanks a bunch !!!

int main()
{

unsigned int8 rangehigh;
unsigned int8 rangelow;
long int range;

setup_adc(ADC_OFF);
output_high(PIN_D7); //to enable/switch on LCD display
output_low(PIN_D5); //needs to be set with Flex_lcd.c file
lcd_init();
set_tris_b(0x00);

while(1)
{
output_toggle(PIN_B0);

//Starts ranging
i2c_start();
i2c_write(0xE0); // Address of SRF02 on I2c bus
i2c_write(0x00); // Address of Command Register
i2c_write(0x51); // Command to tell SRF02 to initiate range in cm
i2c_stop();

delay_ms(100); // wait for ranging to finish

//Read High Byte
i2c_start();
i2c_write(0xE0); // SRF02 address plus one, this tells it that we are reading
i2c_write(0x02); // Address of register that holds that data and we want to start reading from

i2c_start();
i2c_write(0xE1); // Number of bytes to read, in this case range high byte
rangehigh = i2c_read(1);
rangelow = i2c_read(0);
i2c_stop();

range = make16(rangehigh,rangelow);

lcd_gotoxy(1,1);
printf(lcd_putc,"high %x, Low %x",rangehigh, rangelow);

lcd_gotoxy(1,2);
printf(lcd_putc,"distance is %x",range);

}
}

Post Reply