Connecting Multiple SRF08 Sonar Modules to the Basic Atom 

Introduction
The SRF08 modules use the I2C bus for communication. This example shows how to connect two SRF08's to the Basic Atom. It is expandable to up 16 SRF08's on the I2C bus. The SDA (data) and SCL (clock) lines are connected to pins 13 and 14 on the Basic Atom. The Basic Atom's I2CIN and I2COUT commands are used for communication with the SRF08's.

The Basic Atom's internal 5v regulator is not suitable for powering much external circuitry. I therefore recommend you use a separate 5v regulator as shown below..

Circuit Schematic for connecting two SRF08 Sonar Modules to the Basic Atom

The schematic above shows a 4k7 pull-up resistor on the SDA line to Vdd. For better noise immunity, this can be reduced to 1k8.

Changing the SRF08 I2C Address
Before you can use the SRF08's you will need to re-program their I2C addresses from the default address of 0xE0 they are supplied with. The program below will do this. Make sure you only have one SRF08 connected when you do this. You only have to change the SRF08_NEW_ADDRESS constant in the program below to the address you want. For example if you want the SRF08 to be at hex address 0xF2, then change SRF08_NEW_ADDRESS to read;

SRF08_NEW_ADDRESS    Con 0xF2      ' Sonar I2C Address

Now download the program to the Basic Atom, you will see rapid brief flashes on the Red Led on the SRF08 indicating that the change of address was successful. To see the results on screen, select terminal 1, set the baud rate to 9600, select the COM port your using and click "connect". You will see the LDR and first range displayed on screen. It is wise to make a note of the new address on the SRF08 itself. It is easy to forget which is which otherwise.
To use the example code described later on this page, set one SRF08 to address 0xE0 and the other to 0xE2. 
The following program can be downloaded here.

'***********************************************************
'**                                                       **
'**    SRF08 Demonstration Software for the Basic Atom    **
'**            To change the SRF08 I2C address            **
'**                                                       **
'**           Copyright 2002 - Devantech Ltd              **
'**      Commercial use of this software is prohibited    **
'**      Private and educational use only is permitted    **
'**                                                       **
'**          Written by Gerald Coe - April 2002           **
'**                                                       **
'***********************************************************

SRF08_NEW_ADDRESS    Con 0xe2      ' Sonar I2C Address

SDA      Con P8        ' Define Data pin
SCL      Con P9        ' Define Clk pin
CmdReg   Con 0         ' Sonar Command register
LightReg Con 1         ' Sonar Light sensor register
RangeReg Con 2         ' Sonar 1st Range register

Light    Var Byte
Range    Var Word      ' 16 bit variable for Range

Main
     Gosub ChangeAddress ' Only call this once to change I2C address, with a single SRF08 connected.
Loop
    i2cout SDA, SCL, SRF08_NEW_ADDRESS, CmdReg, [81]
    pause 66
    i2cin SDA, SCL, SRF08_NEW_ADDRESS, LightReg, [Light, Range.HighByte, Range.LowByte]
    Serout S_OUT, i9600, ["SRF08 at 0x", HEX SRF08_NEW_ADDRESS, " - Light = 0x", HEX Light, ", Range = ", DEC03 Range, " cm, ", 13] ' Use terminal window
goto Loop


' This subroutine will change the SRF08's I2C address to whatever you have set
' in the SRF08 Con statement above. Since we don't know what the current address
' of the SRF08, we use the General Broadcast address (00) to communicate with it.
' Make sure you only have one SRF08 connected when you do this.
' The new address is stored on the SRF08, so this only needs to be done once.
' I suggest you write the new address on a label on the SRF08.
ChangeAddress
    i2cout SDA, SCL, 0, CmdReg, [0xA0]   ' {
    i2cout SDA, SCL, 0, CmdReg, [0xAA]   ' { Required Sequence to change Address
    i2cout SDA, SCL, 0, CmdReg, [0xA5]   ' {
    i2cout SDA, SCL, 0, CmdReg, [SRF08_NEW_ADDRESS]  ' Send new Address
Return


Displaying Light Sensor and Range readings in a PC Terminal window
Now that you have your SRF08's re-programmed to their new I2C addresses (0xE0 and 0xE2) the following sample code will display the light sensor reading and the 1st range reading, for each SRF08 in a terminal window on the PC .
The sample code below can be downloaded here.

'***********************************************************
'**                                                       **
'**    SRF08 Demonstration Software for the Basic Atom    **
'**                                                       **
'**           Copyright 2002 - Devantech Ltd              **
'**     Commercial use of this software is prohibited     **
'**     Private and educational use only is permitted     **
'**                                                       **
'**         Written by Gerald Coe - April 2002            **
'**                                                       **
'***********************************************************

SDA      Con P8     ' Define Data pin
SCL      Con P9     ' Define Clk pin
SRF08a   Con 0xE0   ' 1st Sonar I2C Address
SRF08b   Con 0xE2   ' 2nd Sonar I2C Address
CmdReg   Con 0      ' Sonar Command register
LightReg Con 1      ' Sonar Light sensor register
RangeReg Con 2      ' Sonar 1st Range register

Light    Var Byte   ' Light sensor
Range    Var Word   ' 16 bit variable for Range

Main
    i2cout SDA, SCL, SRF08a, CmdReg, [81]
    pause 66
    i2cin SDA, SCL, SRF08a, LightReg, [Light, Range.HighByte, Range.LowByte]
    Serout S_OUT, i9600, ["Light1 = 0x", HEX Light, ", Range1 = ", DEC Range, " cm, "] ' Use terminal window

    i2cout SDA, SCL, SRF08b, CmdReg, [81]
    pause 66
    i2cin SDA, SCL, SRF08b, LightReg, [Light, Range.HighByte, Range.LowByte]
    Serout S_OUT, i9600, ["Light2 = 0x", HEX Light, ", Range2 = ", DEC Range, " cm", 13] ' Use terminal window
goto main

 

You can find more information on the SRF08 here