Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Programs >> how to connect 4 srf08 sensors?
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1209100412

Message started by ARTofSAINT on 04/25/08 at 05:13:32

Title: how to connect 4 srf08 sensors?
Post by ARTofSAINT on 04/25/08 at 05:13:32
i connected 1 SRF08 sensor to i2c(scl,sda)pin.
it works well.
but i have to use 4 srf08 sensors, how can i get each signal from scl sda pin?

this is code.
plz advice to me

#include <TwoWire.h>

void setup()
{
 Wire.begin();                // join i2c bus (address optional for master)
 Serial.begin(9600);          // start serial communication at 9600bps
}

int reading = 0;

void loop()
{
 // step 1: instruct sensor to read echoes
 Wire.beginTransmission(112); // transmit to device #112 (0x70)
                              // the address specified in the datasheet is 224 (0xE0)
                              // but i2c adressing uses the high 7 bits so it's 112
 Wire.send(0x00);             // sets register pointer to the command register (0x00)  
 Wire.send(0x51);             // command sensor to measure in "inches" (0x50)
                              // use 0x51 for centimeters
                              // use 0x52 for ping microseconds
 Wire.endTransmission();      // stop transmitting

 // step 2: wait for readings to happen
 delay(70);                   // datasheet suggests at least 65 milliseconds

 // step 3: instruct sensor to return a particular echo reading
 Wire.beginTransmission(112); // transmit to device #112
 Wire.send(0x02);             // sets register pointer to echo #1 register (0x02)
 Wire.endTransmission();      // stop transmitting

 // step 4: request reading from sensor
 Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

 // step 5: receive reading from sensor
 if(2 <= Wire.available())    // if two bytes were received
 {
   reading = Wire.receive();  // receive high byte (overwrites previous reading)
   reading = reading << 8;    // shift high byte to be high 8 bits
   reading |= Wire.receive(); // receive low byte as lower 8 bits
   Serial.println(reading);   // print the reading
 }

 delay(250);                  // wait a bit since people have to read the output :)
}


/*

// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
// usage: changeAddress(0x70, 0xE6);

void changeAddress(byte oldAddress, byte newAddress)
{
 Wire.beginTransmission(oldAddress);
 Wire.send(0x00);
 Wire.send(0xA0);
 Wire.endTransmission();

 Wire.beginTransmission(oldAddress);
 Wire.send(0x00);
 Wire.send(0xAA);
 Wire.endTransmission();

 Wire.beginTransmission(oldAddress);
 Wire.send(0x00);
 Wire.send(0xA5);
 Wire.endTransmission();

 Wire.beginTransmission(oldAddress);
 Wire.send(0x00);
 Wire.send(newAddress);
 Wire.endTransmission();
}

*/

Title: Re: how to connect 4 srf08 sensors?
Post by worapoht on 04/25/08 at 05:44:43
you need to pre-program each SRF08 address before using..

the below comment code is used for changed address
oldAddress is default address from factory (0x70 or 8bit form = 0xE0 in datasheet)
and newAddress is you address you want (but value is multiply by two)

example below use changeAddress(0x70, 0xE6)
finally device will change from 0x70 to 0x73 (0x73 is 0xE6 divide by 2)

/*  <----- uncomment the code to activate function
 
// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)  
// usage: changeAddress(0x70, 0xE6);  
 
void changeAddress(byte oldAddress, byte newAddress)  
{  
 Wire.beginTransmission(oldAddress);  
 Wire.send(0x00);  
 Wire.send(0xA0);  
 Wire.endTransmission();  
 
 Wire.beginTransmission(oldAddress);  
 Wire.send(0x00);  
 Wire.send(0xAA);  
 Wire.endTransmission();  
 
 Wire.beginTransmission(oldAddress);  
 Wire.send(0x00);  
 Wire.send(0xA5);  
 Wire.endTransmission();  
 
 Wire.beginTransmission(oldAddress);  
 Wire.send(0x00);  
 Wire.send(newAddress);  
 Wire.endTransmission();  
}

*/

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.