Example Code for Arduino-pH Calibration and Measurement

Last revision 2026/01/13

This article offers a comprehensive guide to calibrating and measuring pH using Arduino, featuring example code and essential maintenance tips for optimal sensor performance.

Hardware Preparation

Software Preparation

Wiring Diagram

Analog_pH_Sensor_V2_Wiring_Diagram

Other Preparation Work

  • For first use or after extended non-use (once a month ideally), calibrate the probe using two-point calibration with standard buffer solutions 4.0 and 7.0.

  • Before measuring another solution, wash the probe with distilled water and absorb residual water-drops with paper to prevent cross-contamination.

  • If using a temperature sensor (e.g., DS18B20) for automatic temperature compensation, implement the readTemperature() function in the code.

Catuion

  • The BNC connector and the signal conversion board must be kept dry and clean, otherwise, it will affect the input impedance, resulting in an inaccurate measurement. If it is damp, it needs to be dried.

  • The signal conversion board cannot be directly placed on a wet or semiconductor surface, otherwise it will affect the input impedance, resulting in the inaccurate measurement. It is recommended to use the nylon pillar to fix the signal conversion board and allow a certain distance between the signal conversion board and the attached surface.

  • The sensitive glass bubble in the head of the pH probe should avoid touching with any hard material. Any damage or scratches will cause the electrode to fail.

  • After completing the measurement, disconnect the pH probe from the signal conversion board. The pH probe should not be connected to the signal conversion board without the power supply for a long time.

Sample Code

Please download DFRobot_PH Library first,then install it.

/*
 * file DFRobot_PH.ino
 * @ https://github.com/DFRobot/DFRobot_PH
 *
 * This is the sample code for Gravity: Analog pH Sensor / Meter Kit V2, SKU:SEN0161-V2
 * In order to guarantee precision, a temperature sensor such as DS18B20 is needed, to execute automatic temperature compensation.
 * You can send commands in the serial monitor to execute the calibration.
 * Serial Commands:
 *   enterph -> enter the calibration mode
 *   calph   -> calibrate with the standard buffer solution, two buffer solutions(4.0 and 7.0) will be automaticlly recognized
 *   exitph  -> save the calibrated parameters and exit from calibration mode
 *
 * Copyright   [DFRobot](http://www.dfrobot.com), 2018
 * Copyright   GNU Lesser General Public License
 *
 * version  V1.0
 * date  2018-04
 */

#include "DFRobot_PH.h"
#include <EEPROM.h>

#define PH_PIN A1
float voltage,phValue,temperature = 25;
DFRobot_PH ph;

void setup()
{
    Serial.begin(115200);  
    ph.begin();
}

void loop()
{
    static unsigned long timepoint = millis();
    if(millis()-timepoint>1000U){                  //time interval: 1s
        timepoint = millis();
        //temperature = readTemperature();         // read your temperature sensor to execute temperature compensation
        voltage = analogRead(PH_PIN)/1024.0*5000;  // read the voltage
        phValue = ph.readPH(voltage,temperature);  // convert voltage to pH with temperature compensation
        Serial.print("temperature:");
        Serial.print(temperature,1);
        Serial.print("^C  pH:");
        Serial.println(phValue,2);
    }
    ph.calibration(voltage,temperature);           // calibration process by Serail CMD
}

float readTemperature()
{
  //add your code here to get the temperature from your temperature sensor
}

Calibration

To ensure accuracy, the probe needs to be calibrated for its first use and after not being used for an extended period of time (once a month ideally).

This section uses two-point calibration and therefore requires two standard buffer solutions of 4.0 and 7.0.

The following steps show how to operate two-point calibration.

  1. Upload the sample code to the Arduino board, then open the serial monitor, after you can see the temperature and pH. If you added a temperature sensor, be sure to write the corresponding function and call it.

  2. Wash the probe with distilled water, then absorb the residual water-drops with paper. Insert the pH probe into the standard buffer solution of 7.0, stir gently, until the values are stable.

  3. After the values are stable, the first point can be calibrated. Specific steps are as follows:

    1. Input enterph command in the serial monitor to enter the calibration mode.
    2. Input calph commands in the serial monitor to start the calibration. The program will automatically identify which of the two standard buffer solutions is present: either 4.0 and 7.0. In this step, the standard buffer solution of 7.0 will be identified.
    3. After the calibration, input exitph command in the serial monitor to save the relevant parameters and exit the calibration mode.
      Note: Only after inputing exitph command in the serial monitor can the relevant parameters be saved.
    4. After the above steps, the first point calibration is completed. The second point calibration will be performed below.
  4. Wash the probe with distilled water, then absorb the residual water-drops with paper. Insert the pH probe into the standard buffer solution of 4.0, stir gently, until the values are stable.

  5. After the values are stable, the second point can be calibrated. These steps are the same as the first calibration step. The specific steps are as follows:

    1. Input enterph command in the serial monitor to enter the calibration mode.
    2. Input calph commands in the serial monitor to start the calibration. The program will automatically identify which of the two standard buffer solutions is present: either 4.0 and 7.0 In this step, the standard buffer solution of 4.0 will be identified.
    3. After the calibration, input the exitph command in the serial monitor to save the relevant parameters and exit the calibration mode.
      Note: Only after inputing exitph command in the serial monitor can the relevant parameters be saved.
    4. After the above steps, the second point calibration is completed.
  6. After completing the above steps, the two-point calibration is completed, and then the sensor can be used for actual measurement. The relevant parameters in the calibration process have been saved to the EEPROM of the main control board.

Result

After uploading the code and opening the serial monitor (115200 baud), you will see temperature (default 25°C) and pH values printed every second

pH Probe Maintenance Notes

  1. When the probe is used for the first time or used for some time, the probe needs to be immersed in the 3NKCL solution for 8 hours.

  2. The glass bubble in the head of the pH probe(in the plastic protection grid) shall not come in contact with the hard objects, and any breakage or rubbing will invalidate the probe.

  3. When the measurement is finished, the protective cap should be put on, and a small amount of 3mol/L KCL solution should be put inside the protective cap to keep the glass bulb moist.

  4. The plug of the pH probe must be kept clean and dry, absolutely prevent the output ends short-circuit, otherwise, it will result in inaccurate measurement or probe failure.

  5. The probe should avoid long-term immersion in distilled water, protein, acid fluoride solution, and prevent contact with silicone oil.

  6. After long-term use, if you found that the percentage of the theoretical slope of the probe (PTS) slightly reduced, you can soak the lower end of the probe in 4%HF (hydrofluoric acid) for 3-5 seconds, wash with distilled water, and then soak in the 0.1MOL/L HCL solution for a few hours, rinse clean with deionized water.

  7. The probes are passivated by the presence of contamination-sensitive bulbs in the solution under testing conditions or by plugging the liquid boundary. The phenomenon is that the theoretical slope (PTS) is reduced, the response time is long and the readings are unstable. Therefore, according to the nature of the contaminants, they should be cleaned with an appropriate solution to make them fresh.

  8. Attention should be paid to the selection of the cleaning agent. The soluble polycarbonate cleaning liquid, such as carbon tetrachloride, trichloroethylene, four furans and so on, may stained glass ball bubble surface, and make the probe failure, please use these with great caution!

Was this article helpful?

TOP