pH Sensor (SEN0161 V1.1) Problems

Hello Everyone,

I am relatively new to arduino. I am currently trying to get the pH sensor to work. The pH sensor is this one: Arduino Link

When testing with pH solutions that I bought from Amazon (because this purchase did not come with the pH calibration items), none of the readings were that accurate.

I am not sure if this is my pH sensors issue, my computers issue, or my pH solution's issue. Please help. I am new to this, so any help is appreciated.

The pH solutions were bought from this link: Amazon Link

When using the code attached below, when I tested the pH sensor in the 7.0 pH solution, it gave me this: Voltage: 1.98 pH: 6.94

When using the code (nothing changed), with the 4.0 pH solution, it gave me this: Voltage: 1.73 pH: 6.06

I have watched multiple youtube videos on how to do this as well as followed the official wiki from DFRobot. I have also been dipping the pH probe into water before testing from the 7.0 pH solution to the 4.0 pH solution to make sure that is does not get cross-contaminated. Please help.

/*
  # This sample code is used to test the pH meter V1.1.
  # Editor : YouYou
  # Ver    : 1.1
  # DAT    : 2014.06.23
  # Product: analog pH meter V1.1
  # SKU    : SEN0161
*/
#define SensorPin A0            //pH meter Analog output to Arduino Analog Input 2
#define Offset 0.00            //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex = 0;
void setup(void)
{
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("pH meter experiment!");    //Test the serial monitor
}
void loop(void)
{
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue, voltage;
  if (millis() - samplingTime > samplingInterval)
  {
    pHArray[pHArrayIndex++] = analogRead(SensorPin);
    if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;
    voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024 + 0.05;
    pHValue = 3.5 * voltage + Offset;
    samplingTime = millis();
  }
  if (millis() - printTime > printInterval)  //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  {
    Serial.print("Voltage:");
    Serial.print(voltage, 2);
    Serial.print("    pH value: ");
    Serial.println(pHValue, 2);
    digitalWrite(LED, digitalRead(LED) ^ 1);
    printTime = millis();
  }
}


double avergearray(int* arr, int number)
{
  int i;
  int max, min;
  double avg;
  long amount = 0;
  if (number <= 0)
  {
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  }
  if (number < 5) //less than 5, calculated directly statistics
  {
    for (i = 0; i < number; i++)
    {
      amount += arr[i];
    }
    avg = amount / number;
    return avg;
  }
  else
  {
    if (arr[0] < arr[1])
    {
      min = arr[0]; max = arr[1];
    }
    else
    {
      min = arr[1]; max = arr[0];
    }
    for (i = 2; i < number; i++)
    {
      if (arr[i] < min)
      {
        amount += min;      //arr<min
        min = arr[i];
      }
      else
      {
        if (arr[i] > max)
        {
          amount += max;  //arr>max
          max = arr[i];
        }
        else
        {
          amount += arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount / (number - 2);
  }//if
  return avg;
}

Did you do the calibration at 4.0 as described on the wiki?

I have used pH sensors widely in the past, and a variant, the ammonia sensor.
The first thing to check, which I can't see from the link, is whether the electronics board has an IC on it that is an operational amplifier.
The op amp is used to convert the pH electrode output to a usable analogue output. Sometimes, dual op amps do the job of conversion and gain in the same device.
The output of a pH electrode is about 60-mV per pH unit at a standard temperature.
This gives you a voltage range of +/- 0.42 volts for pH 0 to 14 (0-mV at pH 7).
So you need some gain and offset if using a 0 to 5-V input.
If the board is designed with a suitable output, just testing with a multlimeter is good enough.
Generally, pH is calibrated with up to 3 standard buffer solutions, and at least 2 to determine the output slope, Buffer solutions are usually 4.0, 7.0 and 10.0 pH.
You choose the pair that suits you. 4 to 10 gives the best slope.
pH output is temperature dependent, so temperature is measured at the same time or automatically compensated for in the electronics. 20.0 or 25.0 degrees Celsius are standard temperatures.
Before the op amp, the signal can be distorted by just flexing the cable, and for best results, the op amp is built into the cap of the electrode.
Using a well designed amplifier, tests I carried out could easily transmit a signal 50-metres or more with nothing than Black and Decker mower cable.

That is more than 99% correct, pretty good for a hobbyist device. The 4.0, though, is way off, more than 150%. That suggests a user error or a contaminated sample.

Perhaps deal with a chemical supply house to get valid calibration samples.

Yes, I put the pH probe into the 4.0 solution and waited until it settled down, then tried turning the knob on the board in the middle, but it did not change anything.

I also have a 10.0 pH solution. Would trying to test it with that help? Also, I've been seeing things about finding like the slope of the equation and the y-intercept to more accurately depict pH, but I'm not exactly sure on the specifics. Is there anything that I could do with the readings I have been getting to do that?

Sorry, I know nothing about that.

Where did you get that code?
It is different from the code on the wiki.

Which Arduino are you using?

Why don't you so the simple thing and check the output from the module?
If that's not right, no amount of code will put it right.

If you don't understand the basics of pH electrode output and its response to acids and alkalies, you aren't going to get far.
You also need to understand that what the module is doing, or is supposed to be doing, is converting a high impedance mV output to something the Arduino can read.
If you had an electrometer, probably not, you could see how the electrode behaves.
Of course it has slope, it's 59-mV per pH unit at a known temperature.

Hello,

This forum is where I got the code. Because I bought this sensor off of Arduino and not DFRobot, I think the versions are not the same (V1.1 on Arduino's website and V1.0 on DFRobot's website).

I am currently using an Arduino Uno R4 Wifi.

Thanks for letting me know what I need to clarify more now.

Well that person never got it to work, so I don't know why you would use that code. Use the code on the Wiki and follow the instructions on how to calibrate.
If it still does not work, come back here and tell us what happened.

Hi Everyone,

This issue has been resolved. I ended up just using the V1 code provided on DFRobot and adjusting the gain potential device (knob), and it worked out fine. I also tested it for a 10.01pH solution, and it was close enough (10.05). I ended up with an offset of 0.3. While I felt that it was a little much, it still worked in the end. Thank you all for your support.

Hello,

I was testing it for about 2 hours before you sent this message using the code given on DFRobot's official wiki. Turns out it worked. Thank you!

Great!
Have a nice day.

Have you calibrated it with at least two buffer solutions?
You need 4.0 and 10.0 pH buffers to check the slope.
You also need to take temperature into consideration. It's a must
The slope changes with temperature.
The gain of the op amp amplifies the mV output of the electrode and takes care of the high impedance.
Offset takes care of the swing of electrode output from negative to positive.
If those things aren't right, nor will the results be right.
The board has one user adjustable pot on it. I can't see what it does. I'm guessing it's gain.
On the plus side, it does look quite well made with the right connectors on the electrode.
You'll need to look after it, don't let it dry out and store it in a neutral buffer like pH 7.0
You can store it in something like potassium chloride solution, but you'll have to look that one up.
It's essential to keep the board dry especially at the electrode end.
Another tip.
If pH is important for you, buy a box of universal pH test strips to dip into samples and buffers now and again.

Could you tell me at approximately what resistance or knob position the trimpot needs to be set to obtain the correct gain? I need it as a reference, because I’m experiencing the same issue you had before it was fixed. When I turn the trimpot potentiometer, there is no noticeable change. In my case, the output is stuck at around 1.900–1.987 mA.

im new to this sensor and having the same issue, u said u using the V1 code provided on DFRobot, but i try to search it and it show PAGE NOT FOUND in here https://wiki.dfrobot.com/ph_meter_v1.1_sku_sen016 , so can u show me the code cuz im also using ph meter v1.1

Did you explore any further than Error 404?
The rest of the website works fine including all the products listed, sensors etc.
It might pay you to find out a bit more on how pH electrodes work and why you need an amplifier.