SRF02 Ultra Sonic Range Finder With I2C Interface
Posted: Wed Mar 24, 2010 3:07 pm
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
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
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.
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
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
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.