Serial.readBytesUntil No constant output

Hello,

I have a PH sensor connected to my Arduino Uno via TX/RX Serial.
The PH sensor works the following: When it receives a "P", it sends something like "PH:7.00". So I have to send 1 byte to the sensor and receive 8 byte.

The problem with my code is that the information from the sensor appears only every 10 or 20 times after sending "P".

Have someone has an idea what could be the problem?

#define Baud_Rate 9600

char ph_data[8];
byte received_from_sensor=0;

void setup()
{
  Serial.begin(Baud_Rate);
  delay(500);
  
}

void loop(){
  Serial.println("P");
  delay(20); 
  if (Serial.available()) {
    received_from_sensor=Serial.readBytesUntil(13,ph_data,8);
    ph_data[received_from_sensor]=0;
    Serial.println(ph_data);
  }
  delay(500);
}

I have a PH sensor connected to my Arduino Uno via TX/RX Serial.

That sounds like a recipe for problems because they are the pins used by the hardware serial interface and there may well be some conflict between the two. Consider using SoftwareSerial for the Ph sensor to free up hardware serial for printing.

Thanks for your quick response.
Helped a little bit, I think..
But there's still the problem that the output from the ph module isn't frequent. I receive sometimes two, sometimes up to five data sets in the first ten seconds but after that nothing happens...

#include <SoftwareSerial.h>
const int rxpin = 2;                    
const int txpin = 3;                   
SoftwareSerial serial_ph(rxpin, txpin); // new serial port on pins 2 and 3

#define Baud_Rate 9600

char ph_data[8];
byte received_from_sensor=0;

void setup()
{
  Serial.begin(Baud_Rate);
  serial_ph.begin(Baud_Rate);
  delay(500);
  
}

void loop(){
  serial_ph.print("P");
  delay(30); 
  if (serial_ph.available()) {
    received_from_sensor=serial_ph.readBytesUntil(13,ph_data,8);
    ph_data[received_from_sensor]=0;
    Serial.println(ph_data);
  }
  delay(1000);
}

  if (serial_ph.available())If no serial data is available then the Ph unit did not send any or there is a hardware/wiring problem preventing the Arduino receiving it. How often is the Ph unit meant to send data and what controls it ?

If this project is on a breadboard then carefully check all of the connections. How about feeding some other data to the serial input to test it.

Provide a link to the datasheet for the sensor!

Mark

There is no real data sheet.. :frowning:
http://www.marktplaats.nl/a/hobby-en-vrije-tijd/elektronica-componenten/a1034944379-ph-sensor-kit-probe-detection-module-for-arduino.html
https://item.taobao.com/item.htm?spm=a230r.1.14.209.pKgk6T&id=42754386304&ns=1&abbucket=4#detail

Try to make the buffer longer lets say 30 bytes.

In your current setup you are writing to the 9th element if you receive 8 characters.

But in what little data there is it says

Response time: <1min

Mark

Try using the 2nd example in Serial Input Basics. It does not block the Arduino but at the same time it will wait patiently for a long time for a response from your sensor.

...R

What is the baud rate of the sensor? Have yo tried communicating directly with the sensor just using the serial monitor?