PH sensor

04 Sep 2012

any one help how to write code in mbed for PH sensor at this link http://atlas-scientific.com/product_pages/embedded/ph.html

04 Sep 2012

just had a look at the link you supplied,

and there are three code examples

bi difficult to read on my phone but the PIC version looks quite simple

especially because most of the code is for the USART

which the MBED has librarys for.

Ceri

13 Jun 2014

I have the same Question .

13 Jun 2014

Looking at the examples you probably want something like this:

// serial ports. Edit pins as needed for your setup.
Serial phSensor(txPin, rxPin);
Serial pc(USBTX,USBRX);

// echo the data to the PC serial port
bool echoToPC = true;

// input buffer.
char dataIn[20];
int dataInCount = 0;

// the pH we got and a flag to indicate a new value. Volatile to ensure the main loop sees new values.
volatile float phReading = 0;
volatile bool gotReading = false;

// on data from the sensor
void onDataRx() {
  while (phSensor.readable()) { // while there is data

    dataIn[dataInCount] = phSensor.getc(); // read it
    if (echoToPC) // echo to the PC if enabled
      pc.putc(dataIn[dataInCount];

    if (dataIn[dataInCount] == 13) { // if the end of line character
      dataIn[dataInCount] = 0; // change it to a null (standard c string termination indicator)
      phReading = atof(dataIn);  // convert the string into a number
      gotReading = true;   // flag the data as being new.
    } else {                      // not the end of a line
      dataInCount++;         // increase the counter
      if (dataInCount == 20)  // check we haven't run out of buffer.
        dataInCount = 0;
    } 

  } // end of while there is data waiting loop
}

// send the stop auto command
void stopAutoReading() {
  phSensor.putc('e');
  phSensor.putc(13);
}

// send the start auto command
void startAutoReading() {
  phSensor.putc('c');
  phSensor.putc(13);
}

// calibrate to a pH of 7 (change to f for a pH of 4 or t for a pH of 10)
void calibratepH7() {
  stopAutoReading();
  phSensor.putc('s');
  phSensor.putc(13);
}

// calibrate to a pH of 7 (change to f for a pH of 4 or t for a pH of 10)
void calibratepH7() {
  stopAutoReading();
  phSensor.putc('s');
  phSensor.putc(13);
}



// do a single read
void readpH() {
  phSensor.putc('R');
  phSensor.putc(13);
}

// set the sensor LED state
void sensorLED(bool on) {
  phSensor.putc('L');
  if (on)
    phSensor.putc('1');
  else
    phSensor.putc('0');

  phSensor.putc(13);
}


int main () {

  // set the serial baud rates
  phSensor.baud(38400);
  pc.baud(38400); // change as needed

  wait(1); // wait for the sensor to start up.

  phSensor.attach(&onDataRx); // call the RX function whenever we get data.

  // get the sensor info
  echoToPC = true; 
  getSensorInfo();
  wait(1);

  // stop direct echoing of the sensor output (comment this line out for debugging maybe
  echoToPC = false;

  // clear the flag 
  gotReading = false;

  // make sure we start at the front of the buffer just in case the info line didn't end in a carriage return for some reason.
  dataInCount = 0;

  // set the sensor running
  startAutoReading();

  while (1) {  // loop forever

    if (gotReading) {   // if there is a new reading
      pc.printf("pH = %.1f\n",phReading);  // print it out.
      gotReading = false;
    }
  }

}

Plus or minus a few syntax errors I'm sure I made.

14 Jun 2014

thanks :D

16 Jun 2014

Looking at it again there is one obvious bug.

in onDataRx() within the if (dataIn[dataInCount] == 13) section you need to add the line dataInCount=0;

06 Mar 2017

can i know which types of pH sensor u are using? is it this code can be used for any types of pH sensor? is it possible to use it for gradino pH sensor that compatible with arduino. i am using embedded board nucleo F103RB, is it any suggestion for the pH sensor types. thanks as advance, hope to hear from u all soon.

07 Apr 2017

Hi Lee Wei Son,

First of all, sorry for the late reply.

The sensor used is the EZO ph sensor probe, here's the link : https://www.atlas-scientific.com/product_pages/kits/ph-kit.html

I recently created a library for this sensor using the I2C protol (wich you have on your nucleo F103RB board).

Here's the link to the library : https://developer.mbed.org/users/gaul2411/code/EZO_I2C_hello/

Hope this help !

08 Jun 2017

Hey Louis,

i tried using the library that you post for EZO ph sensor probe but i can't get any readings at all. And device status info and the others all showed failed.