Ultrasonic oxygen sensor serial communication problems

Hi all,

I'm currently trying to use an oxygen sensor (datasheet of it is attached). This is meant to be done by sending a 9 byte HEX array and receiving an 11 byte array back, from which using 2 of those bytes the oxygen concentration can be calculated. However, when I run my code I do not receive the 11 bytes that I should and I am not sure why that is. Any help would be appreciated.

Thanks you all <3

#include <AltSoftSerial.h>

AltSoftSerial altSerial(8, 9); // RX, TX

byte reading[10]; // byte array to store 11 bytes 
int result; // final result from reading
boolean dataAvailable = false; //to check if I received 11 bytes of data
float O2concentration; //concentraiton calculated 


void setup() {
  // put your setup code here, to run once:
altSerial.begin(19200);
Serial.begin(9600);

}

void loop() {

byte message[] = { 0x55, 0xAA, 0x7E, 0x02, 0x4F, 0x43, 0x94, 0x0E, 0x0D }; //Sending this HEX array to sensor
altSerial.write(message, sizeof(message));


int MAX_MILLIS_TO_WAIT = 1000;
unsigned long starttime;
starttime = millis();

while ( (altSerial.available()<11) && ((millis() - starttime) < MAX_MILLIS_TO_WAIT) )
{
  // Wait until I get all 11 bytes
}


if(altSerial.available() < 11)
{
     // the data didn't come in - handle that problem here
     Serial.println("ERROR - Didn't get 11 bytes of data!");
}


else
{
     for(int n=0; n<10; n++){
        reading[n] = altSerial.read(); // Then: Get them.
     }
     dataAvailable = true;
}



  if (dataAvailable == true) {
    
    result = reading[6] * 256 + reading[7]; // converting array to readable stuff
    O2concentration = result/10; // measured concentration in %
  }
  Serial.print(O2concentration);
  Serial.println("%");
  delay(1000);

}

USOM-100 Ultrasonic Oxygen Sensor(2).pdf (306 KB)

1 Like

Well, this makes no sense at all:

"byte reading[10]; // byte array to store 11 bytes "

Paul

Can you tell which AltSoftSerial library you use ? From where did you get it ?

Why is this line compiling ? AltSoftSerial altSerial(8, 9); // RX, TX
I think it should be: AltSoftSerial altSerial; // 8 = RX, 9 = TX

Can you show a photo ? so we can check the wiring.
Sensor pin 1 = 5V
Sensor pin 2 = TXD to Arduino pin 8 (AltSoftSerial RX)
Sensor pin 3 = RXD to Arduino pin 9 (AltSoftSerial TX)
Sensor pin 4 = GND

I think that the AltSoftSerial should work reliable and the data you send is correct.

The Serial Interface of the sensor is TTL level. That is okay, but is the signal level inverted or not ?

Make the array to receive data for example 16 bytes, or 20 or 30. But not 10.

Don't rush to the final result.
You could print to the serial monitor how many bytes have been received. It makes a big difference if some bytes are recieved or nothing at all.

Do you know what this line does ? O2concentration = result / 10;
The integer 'result' is divided by integer '10'. The result is converted to float. I thing you want: O2concentration = (float) result / 10.0;

The text layout of your sketch is a big mess. Could you fix that ? Make the indents, the empty lines, and everything nice and correct. If you make a habit to make it look good, that will help a lot to see what the code is doing. It will make it a lot easier to spot a bug.

That sensor is not cheap (20 to 80 dollars). I can not find that someone else has used it with an Arduino. If you are the first one to try it with an Arduino, you might expect some troubles.