pH monitoring with Atlas Scientific pH stamp

I'm working on a hydroponic automation system and I am trying to tackle my first big milestone: reading the pH in the nutrient solution before being able to adjust it.

I am sort of stuck on the code and I know that the sensor data has to be a floating value, but I'm really not sure how to do this... (I also know it has something to do with atof?)

Here is my code so far... any help?

#include <SoftwareSerial.h>

#define rxph 2 
#define txph 3
SoftwareSerial pH (rxph, txph);
float pH_val = 0.0;  // not sure about this
String inputstringPH = "";
String sensorstringPH = "";
boolean input_stringPHcomplete = false;
boolean sensor_stringPHcomplete = false;

void setup(){ 
Serial.begin(38400);
pH.begin(38400);
inputstringPH.reserve(5);
sensorstringPH.reserve(30);
}  

void serialEvent() { 
char incharPH = (char)pH.read();
inputstringPH += incharPH;
if(incharPH == '\r') {input_stringPHcomplete = true;}
}       

 void loop(){
if (input_stringPHcomplete){
   pH.println(inputstringPH); 
   inputstringPH = "";
   input_stringPHcomplete = false;

}                 
while (pH.available()) {
char incharPH = (char)pH.read();
sensorstringPH += incharPH;
if (incharPH == '\r') {sensor_stringPHcomplete = true;}

}
 if (sensor_stringPHcomplete){
   Serial.println(sensorstringPH);
   sensorstringPH = ""; 
   sensor_stringPHcomplete = false;
   }
}

please post a link to the datasheet.

Do you have 2 sensors attached?

does this minimal sketch work?

#include <SoftwareSerial.h>

#define rxph 2 
#define txph 3

SoftwareSerial pH (rxph, txph);

void setup()
{ 
  Serial.begin(38400);
  pH.begin(38400);
}  

 void loop()
{
  if (pH.available() >0) 
  {
    char incharPH = (char)pH.read();
    Serial.print(incharPH);
  }
}

Hi,

here is the Datasheet: http://atlas-scientific.com/_files/pH_Circuit_3.0.pdf
yes this seems to work actually :D, but it is giving me a continuous reading:
3.763.523.493.523.523.583.603.563.583.603.603.583.583.563.523.583.60
I only have 1 sensor attached for now, later I'll be attaching an EC sensor http://atlas-scientific.com/_files/EC_Circuit_2.0.pdf and a temperature sensor to be able to take accurate pH and EC readings.

thanks a lot for the help XD

a quick hacl to print one value per line

int count = 0;

void loop()
{
  if (pH.available() >0) 
  {
    char incharPH = (char)pH.read();
    if (inchar =='.') count = 0;
    Serial.print(incharPH);
    count++;
    if (count==3) Serial.println();
  }
}

Give this version a try to see if there is a separator between the readings

int count = 0;

void loop()
{
  if (pH.available() >0) 
  {
    char incharPH = (char)pH.read();
    Serial.print((int) incharPH, DEC);
    Serial.print("\t");
    Serial.println(incharPH);
  }
}

Thanks,

The first one works quite well now, but it continuously spits out the reading every few milliseconds:
6.29
6.59
6.64
6.66
6.66
...

The second one gives me this:
54 6
46 .
51 3
49 1
13

54 6
46 .
...

I saw on the datasheet that the sensor could be calibrated using one of the various commands.
The solution I am testing the probe in is a pH 7.00 (@25C +/- 0.02) and the readout I am getting is only 6.66 ( it goes up to 12 when I test with pH 10).
I tried using the serial monitor to input these commands, but nothing happened...

The second one shows that you can synchronize on the char 13 (which is a newline)

now using double buffering -something like this. data is collected in val array and if full it is copied for use into buffer.

unsigned long lastTime = 0;

int count = 0;
char val[6];
char buffer[6];

void setup()
{
  Serial.begin(....);
  lastTime = millis();
}

void loop()
{
  if (pH.available() >0) 
  {
    char incharPH = (char)pH.read();
    if (inharPH == 13)
    {
      val[count] = 0;
      count = 0;
      strcpy(buffer, val);
    }
    val[count] = inCharPH;
  }

  if (millis() -1000 > lastTime)
  {
    Serial.println(buffer);
    lastTime += 1000;
  }

   // rest of code
}

I saw on the datasheet that the sensor could be calibrated using one of the various commands.

No time to dive into that now, sorry ...

No problem, this is already a lot of help. thanks so much :slight_smile:
I'm going to try to get the same logic to work on the EC stamp

This board has a continuous and on demand mode. Send it an "E" command to terminate continuous. Send it a "C" command to get a value periodically say every 10 seconds. Calibration has to be performed with a 7 and 4 or 7 and 10 standard. Do the 7 first as this fixes the intercept then do 4 as this fixes the slope.

You may also want to consider using analog ports rather than serial when you have more than one sensor (pH and EC). These are some options.

http://webpages.charter.net/tdsmeter/

You could add a temp sensor on yet a another analog port but the accuracy required for hydro use may not justify it unless you will be controlling envrionmental equipment.

Hi pjsquared,

I moved the conversation to here:
http://arduino.cc/forum/index.php/topic,98850.0.html

This board has a continuous and on demand mode. Send it an "E" command to terminate continuous. Send it a "C" command to get a value periodically say every 10 seconds. Calibration has to be performed with a 7 and 4 or 7 and 10 standard. Do the 7 first as this fixes the intercept then do 4 as this fixes the slope.

I have been trying to communicate with the board by typing the commands in the serial monitor but so far nothing...
I don't know how I did it, but a few days ago, I managed to switch off the LED (on datasheet is says to enter L0 to turn off the LEDs and L1 to turn them back on), but I still cannot manage to either turn the LED on, or reset the board.

the only way I can get a read is by resetting the arduino and unplugging/ replugging the GND on the stamp