Simple code for float from Atlas Scientific PH stamp

Since this is my first post, and being rather "new" on Arduino, I thought lets make it a contributing first post :slight_smile:

I have read through tens of posts in various fora to find a way to get the PH value, spitted out by the serial PH stamp, into a float for further manipulation. Most important for me was to get it simply to an LCD.
Info on the PH stamp you can find http://atlas-scientific.com/product_pages/embedded/ph.html

All related posts use some sort of string etc, a lot of code which in my case either didn't work (my inexperience) or was way too complex and memory hogging. Trying all sorts of code, and digging in the Arduino reference pages I stumbled upon a function that did exactly what I needed: simply transferring the ASCII characters into a float value.... serial.parseFloat()

Take a look at the code below (just a piece out of my total code for a Reef Controller).
What I only needed was the value itself.
Tested it, and works so far without visible side effects afaik

// -------------------------------------------------------------------------------
//
// Simple way to get Atlas Scientific serial PH stamp value to LCD or PC
// Author: Eric van Riet - The Netherlands
//
// ONLY THE PART FOR PH IS LISTED!**
// ASSUMED THAT OTHER LIBRARIES FOR LCD ETC ARE IN YOUR OWN CODE!!

// -------------------------Set measurement interval------------------------------
long phReadInterval = 1000;
long previousMillisPH = 0;
int PHState = HIGH;
// -------------------------------------------------------------------------------

// ---------------Variable to hold PH value---------------------------------------
float PH_Val;
// -------------------------------------------------------------------------------

void setup()
{
Serial.begin(38400); // Set baudrate for hardware serial port 0 (PC)
Serial1.begin(38400); // Set baudrate serial port 1 to 38400 (PH)
lcd.begin (20,4); // initialize the lcd

// -------------Get PH value from Atlas Scientific PH serial stamp----------------
void getPHvalue() { //get PH every interval (1000 ms)
unsigned long currentMillisPH = millis();

if(currentMillisPH - previousMillisPH > phReadInterval) {
previousMillisPH = currentMillisPH;
if (PHState == HIGH) {
PHState = LOW;
}
else {
PHState = HIGH;
Serial1.print("r\r");
while (Serial1.available() > 0) {
// Serial1.parseFloat looks for the next valid float number
// in the serial stream. In this case on hardware serial 1
PH_Val = Serial1.parseFloat();
// look for the carriage return. That's the end of your sentence:
if (Serial1.read() == '\r') {
Serial.println(PH_Val); // can be used for debug, pc display etc.
}
}
}
}
}
// -------------------------------------------------------------------------------

void loop()
{
getPHvalue();

lcd.setCursor(0,1);
lcd.print("PH value: ");
lcd.print(PH_Val);

}

I trust somebody else can find use for this.

You cannot start parsing when one character comes in. You should split the problem in 2 parts.

  1. read one word from serial - combine them into one line until a separator is found (newline or space or so)
  2. check if the word you got is a float number and convert it.

So try to recode this - note you can test the two steps separately !

Hello Rob,

I know there are other ways to do this, but this also "works" fine
My knowledge is not nearly deep enough to get all the insight, but I found this quite easy to implement.

Besides the timing I put in there it is basically only two steps:

  • send command to return value ( r followed by )
  • "listen" at serial and wait for value.

The Atlas stamp does this (part of their datasheet):
R Instructs the pH Circuit to return a single pH reading.
*This instruction takes 378 milliseconds to complete
Full proper syntax: r or R
The pH Circuit will respond: XX.XX

I am always open for new things to learn, also to conduct this in the "proper way". Any pointers are welcome

Working on a similar project. Would also like to know the correct way of doing this.

I have also worked with the atlas PH stamp and getting the float from serial was a significant hurdle for me as a beginner. Thanks to Nick Gammon's serial tutorial ( Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking ) I was able to use his code to create a sketch that read from serial without blocking.