SRF01

hello, i have 2 SRF01 plug on different pin (A5 and A4) on my arduino.

can some one tel me if i must change the default adress on one of the srf01 or if i can use the both with default adress (0x01) (because they are bug on different pin).

Thanks

That’s correct, if you use different pins you can keep the same addresses for both.

Ok another think about the serial: can i use same serial ?

My code stop just after setup, if i delete on of the srf01X begin it is run.

Any idea ?

[code]#include <SoftwareSerial.h> // For SRF01
#define txrxPin_L A0 // Defines Pin 10 to be used as both rx and tx for the SRF01_L
#define txrxPin_R A1 // Defines Pin 10 to be used as both rx and tx for the SRF01_R
#define srfAddress 0x01 // Address of the SFR01
#define getSoft 0x5D // Byte to tell SRF01 we wish to read software version
#define getRange 0x54 // Byte used to get range from SRF01 in cm
#define getStatus 0x5F // Byte used to get the status of the transducer

SoftwareSerial SRF01_L(txrxPin_L, txrxPin_L); // Sets up the SoftwareSerial with Digital Pin A0 and sets the name to “SRF01_L”
SoftwareSerial SRF01_R(txrxPin_R, txrxPin_R); // Sets up the SoftwareSerial with Digital Pin A1 and sets the name to “SRF01_R”

void setup()
{
Serial.begin(19200);

SRF01_R.begin(9600);
delay(200); // Waits some time to make sure that SRF01_R is powered up
Serial.println(“SRF01_R Test”);
SRF01_Cmd_R(srfAddress, getSoft); // Calls a function to get the SRF01 software version
while (SRF01_R.available() < 1); // Waits to get good data
int softVer_R = SRF01_R.read(); // Read software version from SRF01_R
Serial.print(“V:”);
Serial.println(softVer_R); // Prints the software version
delay(200);
Serial.println(“Initialization Complete”); // After inititalization is complete you should see the correct version of your device printed to the serial monitor

SRF01_L.begin(9600);
delay(200); // Waits some time to make sure that SRF01_R is powered up
Serial.println(“SRF01_L Test”);
SRF01_Cmd_L(srfAddress, getSoft); // Calls a function to get the SRF01 software version
while (SRF01_L.available() < 1); // Waits to get good data
int softVer_L = SRF01_L.read(); // Read software version from SRF01_R
Serial.print(“V:”);
Serial.println(softVer_L); // Prints the software version
delay(200);
Serial.println(“Initialization Complete”); // After inititalization is complete you should see the correct version of your device printed to the serial monitor

}

void loop()
{
SRF01_Range_L();
SRF01_Range_R();

}

void SRF01_Range_L(){

int max = 1; // Setup so that you can averaqe readings
int sum = 0; // Currently I am not taking an average… Max = 1
for (int count = 0; count < max; count++) {
sum = sum + doRange_L(); // Calls a function to get range from SRF01 and sums the ranges up so that you can take an avg
}

int range = sum / max;

Serial.print("Range avg = “);
Serial.print(range); // Print range result to the screen
Serial.println(” "); // Print some spaces to the screen to make sure space direcly after the result is clear
checkLock_L(); // Calls a function to check if the transducer is locked or unlocked
}

void SRF01_Range_R(){

int max = 1; // Setup so that you can averaqe readings
int sum = 0; // Currently I am not taking an average… Max = 1
for (int count = 0; count < max; count++) {
sum = sum + doRange_R(); // Calls a function to get range from SRF01 and sums the ranges up so that you can take an avg
}

int range = sum / max;

Serial.print("Range avg = “);
Serial.print(range); // Print range result to the screen
Serial.println(” "); // Print some spaces to the screen to make sure space direcly after the result is clear
checkLock_R(); // Calls a function to check if the transducer is locked or unlocked
}

void SRF01_Cmd_L(byte Address, byte cmd){ // Function to send commands to the SRF01_L
pinMode(txrxPin_L, OUTPUT); // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
digitalWrite(txrxPin_L, LOW);
delay(2);
digitalWrite(txrxPin_L, HIGH);
delay(1);
SRF01_L.write(Address); // Send the address of the SRF01_L
SRF01_L.write(cmd); // Send commnd byte to SRF01_L
pinMode(txrxPin_L, INPUT); // Make input ready for Rx
int availableJunk = SRF01_L.available(); // Filter out the junk data
for(int x = 0; x < availableJunk; x++){
byte junk = SRF01_L.read();
}
}

void SRF01_Cmd_R(byte Address, byte cmd){ // Function to send commands to the SRF01_R
pinMode(txrxPin_R, OUTPUT); // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
digitalWrite(txrxPin_R, LOW);
delay(2);
digitalWrite(txrxPin_R, HIGH);
delay(1);
SRF01_R.write(Address); // Send the address of the SRF01_R
SRF01_R.write(cmd); // Send commnd byte to SRF01_R
pinMode(txrxPin_R, INPUT); // Make input ready for Rx
int availableJunk = SRF01_R.available(); // Filter out the junk data
for(int x = 0; x < availableJunk; x++){
byte junk = SRF01_R.read();
}
}

void checkLock_L() {
SRF01_Cmd_L(srfAddress, getStatus); // Call to a function that checks if the trancducer is locked or unlocked
byte statusByte = SRF01_L.read(); // Reads the SRF01 status, The least significant bit tells us if it is locked or unlocked
int status = statusByte & 0x01; // Get status of lease significan bit
if(status == 0)
{
Serial.println(“Unlocked”); // Prints the word unlocked followd by a couple of spaces to make sure space after has nothing in
}
else
{
Serial.println("Locked "); // Prints the word locked followd by a couple of spaces to make sure that the space after has nothing in
}
}

int doRange_L() {
SRF01_Cmd_L(srfAddress, getRange); // Calls a function to get range from SRF01_L
while (SRF01_L.available() < 2); // Waits to get good data
byte highByte = SRF01_L.read(); // Get high byte
byte lowByte = SRF01_L.read(); // Get low byte
int dist_L = ((highByte<<+lowByte); // Put them together
return dist_L;
}

void checkLock_R() {
SRF01_Cmd_R(srfAddress, getStatus); // Call to a function that checks if the trancducer is locked or unlocked
byte statusByte = SRF01_R.read(); // Reads the SRF01 status, The least significant bit tells us if it is locked or unlocked
int status = statusByte & 0x01; // Get status of lease significan bit
if(status == 0)
{
Serial.println(“Unlocked”); // Prints the word unlocked followd by a couple of spaces to make sure space after has nothing in
}
else
{
Serial.println("Locked "); // Prints the word locked followd by a couple of spaces to make sure that the space after has nothing in
}
}

int doRange_R() {
SRF01_Cmd_R(srfAddress, getRange); // Calls a function to get range from SRF01_R
while (SRF01_R.available() < 2); // Waits to get good data
byte highByte = SRF01_R.read(); // Get high byte
byte lowByte = SRF01_R.read(); // Get low byte
int dist_R = ((highByte<<+lowByte); // Put them together
return dist_R;
}[/code]

The first thing to do would be to get one sensor working. Have you tried the example from the manufacturer without modifications?

There a note in the code that only one software serial port can listen at a time, so you might want to try adding something like this after your first begin:

srf01.listen();

Considering this issue with software serial, you might want to consider plugging both sensors into a single pin (and change one of the IDs).

the both srf01 working good, i test it.

I think the best way for me it s to change the serial port.

I will be back

i am back with running code:

[code]#include <SoftwareSerial.h> // For SRF01
#define txrxPin_L A0 // Defines Pin 10 to be used as both rx and tx for the SRF01_L
#define txrxPin_R A1 // Defines Pin 10 to be used as both rx and tx for the SRF01_R
#define srfAddress 0x01 // Address of the SFR01
#define getSoft 0x5D // Byte to tell SRF01 we wish to read software version
#define getRange 0x54 // Byte used to get range from SRF01 in cm
#define getStatus 0x5F // Byte used to get the status of the transducer

SoftwareSerial SRF01_L(txrxPin_L, txrxPin_L); // Sets up the SoftwareSerial with Digital Pin A5 and sets the name to “SRF01_L”
SoftwareSerial SRF01_R(txrxPin_R, txrxPin_R); // Sets up the SoftwareSerial with Digital Pin A5 and sets the name to “SRF01_R”

void setup()
{
Serial.begin(19200);
// Attach SRF01
SRF01_L.begin(9600);
delay(200); // Waits some time to make sure that SRF01_L is powered up
Serial.println(“SRF01_L Test”);
SRF01_Cmd_L(srfAddress, getSoft); // Calls a function to get the SRF01 software version
while (SRF01_L.available() < 1); // Waits to get good data
int softVer_L = SRF01_L.read(); // Read software version from SRF01_L
Serial.print(“V:”);
Serial.println(softVer_L); // Prints the software version
delay(200);
Serial.println(“Initialization Complete”); // After inititalization is complete you should see the correct version of your device printed to the serial monitor

SRF01_R.begin(9600);
delay(200); // Waits some time to make sure that SRF01_R is powered up
Serial.println(“SRF01_R Test”);
SRF01_Cmd_R(srfAddress, getSoft); // Calls a function to get the SRF01 software version
while (SRF01_R.available() < 1); // Waits to get good data
int softVer_R = SRF01_R.read(); // Read software version from SRF01_R
Serial.print(“V:”);
Serial.println(softVer_R); // Prints the software version
delay(200);
Serial.println(“Initialization Complete”); // After inititalization is complete you should see the correct version of your device printed to the serial monitor
}

void loop()
{
SRF01_Range_L();
SRF01_Range_R();
}

void SRF01_Range_L(){
SRF01_L.listen();
int max = 1; // Setup so that you can averaqe readings
int sum = 0; // Currently I am not taking an average… Max = 1
for (int count = 0; count < max; count++) {
sum = sum + doRange_L(); // Calls a function to get range from SRF01 and sums the ranges up so that you can take an avg
}

int range = sum / max;

Serial.print("Range avg L = “);
Serial.print(range); // Print range result to the screen
Serial.println(” "); // Print some spaces to the screen to make sure space direcly after the result is clear
checkLock_L(); // Calls a function to check if the transducer is locked or unlocked
}

void SRF01_Range_R(){
SRF01_R.listen();
int max = 1; // Setup so that you can averaqe readings
int sum = 0; // Currently I am not taking an average… Max = 1
for (int count = 0; count < max; count++) {
sum = sum + doRange_R(); // Calls a function to get range from SRF01 and sums the ranges up so that you can take an avg
}

int range = sum / max;

Serial.print("Range avg R = “);
Serial.print(range); // Print range result to the screen
Serial.println(” "); // Print some spaces to the screen to make sure space direcly after the result is clear
checkLock_R(); // Calls a function to check if the transducer is locked or unlocked
}

void SRF01_Cmd_L(byte Address, byte cmd){ // Function to send commands to the SRF01_L
pinMode(txrxPin_L, OUTPUT); // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
digitalWrite(txrxPin_L, LOW);
delay(2);
digitalWrite(txrxPin_L, HIGH);
delay(1);
SRF01_L.write(Address); // Send the address of the SRF01_L
SRF01_L.write(cmd); // Send commnd byte to SRF01_L
pinMode(txrxPin_L, INPUT); // Make input ready for Rx
int availableJunk = SRF01_L.available(); // Filter out the junk data
for(int x = 0; x < availableJunk; x++){
byte junk = SRF01_L.read();
}
}

void SRF01_Cmd_R(byte Address, byte cmd){ // Function to send commands to the SRF01_R
pinMode(txrxPin_R, OUTPUT); // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
digitalWrite(txrxPin_R, LOW);
delay(2);
digitalWrite(txrxPin_R, HIGH);
delay(1);
SRF01_R.write(Address); // Send the address of the SRF01_R
SRF01_R.write(cmd); // Send commnd byte to SRF01_R
pinMode(txrxPin_R, INPUT); // Make input ready for Rx
int availableJunk = SRF01_R.available(); // Filter out the junk data
for(int x = 0; x < availableJunk; x++){
byte junk = SRF01_R.read();
}
}

void checkLock_L() {
SRF01_Cmd_L(srfAddress, getStatus); // Call to a function that checks if the trancducer is locked or unlocked
byte statusByte = SRF01_L.read(); // Reads the SRF01 status, The least significant bit tells us if it is locked or unlocked
int status = statusByte & 0x01; // Get status of lease significan bit
if(status == 0)
{
Serial.println(“Unlocked”); // Prints the word unlocked followd by a couple of spaces to make sure space after has nothing in
}
else
{
Serial.println("Locked "); // Prints the word locked followd by a couple of spaces to make sure that the space after has nothing in
}
}

int doRange_L() {
SRF01_Cmd_L(srfAddress, getRange); // Calls a function to get range from SRF01_L
while (SRF01_L.available() < 2); // Waits to get good data
byte highByte = SRF01_L.read(); // Get high byte
byte lowByte = SRF01_L.read(); // Get low byte
int dist_L = ((highByte<<8)+lowByte); // Put them together
return dist_L;
}

void checkLock_R() {
SRF01_Cmd_R(srfAddress, getStatus); // Call to a function that checks if the trancducer is locked or unlocked
byte statusByte = SRF01_R.read(); // Reads the SRF01 status, The least significant bit tells us if it is locked or unlocked
int status = statusByte & 0x01; // Get status of lease significan bit
if(status == 0)
{
Serial.println(“Unlocked”); // Prints the word unlocked followd by a couple of spaces to make sure space after has nothing in
}
else
{
Serial.println("Locked "); // Prints the word locked followd by a couple of spaces to make sure that the space after has nothing in
}
}

int doRange_R() {
SRF01_Cmd_R(srfAddress, getRange); // Calls a function to get range from SRF01_R
while (SRF01_R.available() < 2); // Waits to get good data
byte highByte = SRF01_R.read(); // Get high byte
byte lowByte = SRF01_R.read(); // Get low byte
int dist_R = ((highByte<<8)+lowByte); // Put them together
return dist_R;
}
[/code]

hello all,

new question about srf01, i add servo motor and now i have “jitter”,

i read this:

pharos.ece.utexas.edu/wiki/index … 02/15/2012

some idea to resolv this ?

Thanks

Hello i use softwareservo library for fix it.

Glad to see you got it working and thanks for sharing your solutions!