Problem with SRF02 UltraSonic Sensor (Arduino Mega 2560)

Hi guys,

I am programming with arduino since 3 months.
The following first programm was working only few days ago, but since i used the second programm, the LED at the back of the SRF02 only signals me its adress but the sensors not ranging anymore. I am not sure if it really has to do with the second code but thats my only explanation.
I also watched the signal send via SDA with an oscilloscope, all i can see there is that only the adress i use in “Wire.startTransmission(adress)” is send as a byte, nothing more.
Another clue, the only part of the programm not working anymore is the if-loop, because only 1 byte is available.

First Programm:

#include <Wire.h>

void setup()
{
  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);          // Start serielle Kommunikation mit 9600bps
}



void loop()
{
  int Entfernung = 0;
  // 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.write(byte(0x00));      // sets register pointer to the command register (0x00)  
  Wire.write(byte(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.write(byte(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
  {
    Entfernung = Wire.read();  // receive high byte (overwrites previous reading)
    Entfernung = Entfernung << 8;    // shift high byte to be high 8 bits
    Entfernung |= Wire.read(); // receive low byte as lower 8 bits
    Serial.println(Entfernung);   // Entfernung anzeigen
   

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

Second Programm:

#include <Wire.h>
int Temp;
float UltraGeschw, T, GemesseneCM, KompensierteCM;

void setup()
{
  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);          // Start serielle Kommunikation mit 9600bps
}

void loop()
{
  int Laufzeit = 0;
  int Entfernung = 0;

  //Schritt 1: Temperatur berechnen
  Wire.beginTransmission(0x40); //Beginnt Kommunikation mit write: 1000 000 0                         
  Wire.write(byte(0xF3));       //Gibt Befehl: No Hold Master TemperaturMessung
  Wire.endTransmission();       //Beendet Kommunikation
  delay(90);   
  
  Wire.requestFrom(0x40, 2);  //Fordert 2 bytes von device 0x04 an
  if(2 <= Wire.available())   //falls diese 2 bytes vorhanden sind...
  {
    Temp = Wire.read();  //bytes werden gelesen und als Temp gesetzt
    Temp = Temp << 8;    //byte-Wert wird um 8 bytes verschoben
    Temp |= Wire.read(); //byte Werte werden verglichen und "aufgeodert"
  }
  
  T = -46.85 + 175.72 / 65536.0 * float(Temp);  //Temperaturmessung mit SHT25
  
  //Schritt 2: Ultraschallgeschwindigkeit umrechnen
  UltraGeschw = (331.50+0.6*T)*0.0001;          //Temperaturkompensation und Umrechnung von m/s auf cm/µs
  
  Serial.print(T);
  //Serial.print("  Grad Celsius");
  Serial.println();
  
  //Schritt 3: Sensor soll Echosignale lesen
  Wire.beginTransmission(112); // Übermittle an 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.write(byte(0x00));      // sets register pointer to the command register (0x00)  
  Wire.write(byte(0x52));      // command sensor to measure in "inches" (0x50)
                               // Benutze 0x51 für Zentimeter
                               // Benutze 0x52 für Mikrosekunden
  Wire.endTransmission();      // Beendigung der Übertragung

  //Schritt 4: Warten bis Sensor mit ausgelesen werden kann
  delay(70);                   // Datenblatt empfiehlt mindestens 65 ms

  //Schritt 5: Sensor wird angewiesen ein bestimmtes Echnosignal auszuwerten
  Wire.beginTransmission(112); // transmit to device #112
  Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  //Schritt 6: Es werden 2 bytes vom device angefordert
  Wire.requestFrom(112, 2);

  //Schritt 7: Daten werden erhalten
  if(2 <= Wire.available())    // falls 2 bytes erhalten wurden
  {
    Laufzeit = Wire.read();      // receive high byte (overwrites previous reading)
    Laufzeit = Laufzeit << 8;    // shift high byte to be high 8 bits
    Laufzeit |= Wire.read();     // receive low byte as lower 8 bits
    
    //Serial.print(Laufzeit);
    //Serial.print("  µs");
    //Serial.println();
    
    GemesseneCM = Laufzeit/58;              //Distanz ohne Temperaturkompensation
    Entfernung = Laufzeit/(2/UltraGeschw);  //Distanz mit Temperaturkompensation
   
    Serial.print(Entfernung);
    //Serial.print("  cm");  // Entfernung in cm anzeigen
    Serial.println();
    
    //Serial.print(GemesseneCM);
    //Serial.print("  cm");
    //Serial.println();
    

  delay(5000);    // Zeit in ms die gewartet wird bis Loop neu startet
  }
}

I hope you can give me an explanation why its not ranging anymore, or why the sensors not using the SDA line properly anymore.

greetings

For your information

Wire.endTransmission(); // stop transmitting

does the actual transmission. It returns an int, which can indicate an errorvalue 0== OK. please add some code to check/print the returnvalue

Problem solved. I programmed in Arduino version 1.0.4 but now I tested with arduino version 1.0.5 and there must be a change in the wire library or something like that :)

Good to hear it is working!

In the - http://arduino.cc/en/Main/ReleaseNotes - there is no mention of a fix of the Wire lib in 1.0.5. Is the working code identical to the one you posted above? Otherwise please post the working code for future reference, Thanks,