Example Code for Arduino-pH Measurement
Last revision 2026/01/13
This article guides users through measuring pH levels with Arduino using the SEN0161 Gravity Analog pH Sensor, detailing hardware preparation, software setup, calibration procedures, and maintenance tips for accurate readings.
Hardware Preparation
- SEN0161 Gravity: Analog pH Sensor x1
- DFR0216-2 DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B x1
- FIT0897 Gravity: Analog Sensor Cable for Arduino(Included with SEN0161) x1
Software Preparation
- Arduino IDE (latest version recommended), Download Link
Wiring Diagram

Caution
-
Please use an external switching power supply,and the voltage as close as possible to the +5.00V. More accurate the voltage, more higher the accuracy!
-
Before the electrode in continuous use every time,you need to calibrate it by the standard solution,in order to obtain more accurate results.The best environment temperature is about 25 ℃,and the pH value is known and reliable,close to the measured value. If you measure the acidic sample, the pH value of the standard solution should be 4.00.If you measure the alkaline sample, the pH value of the standard solution should be 9.18.Subsection calibration, just in order to get a better accuracy.
-
Before the pH electrode measured different solutions, we need to use water to wash it. We recommend using deionized water.
Preparation Steps
-
Connect equipments according to the graphic,that is,the pH electrode is connected to the BNC connector on the pH meter board,and then use the connection lines,the pH meter board is connected to the analog port 0 of the Arduino controller. When the Arduino controller gets power,you will see the blue LED on board is on.
-
Upload the sample code to the Arduino controller.
-
Put the pH electrode into the standard solution whose pH value is 7.00,or directly shorted the input of the BNC connector.Open the serial monitor of the Arduino IDE,you can see the pH value printed on it,and the error does not exceed 0.3. Record the pH value printed,then compared with 7.00, and the difference should be changed into the "Offset" in the sample code. For example,the pH value printed is 6.88,so the difference is 0.12.You should change the "# define Offset 0.00" into "# define Offset 0.12" in your program.
-
Put the pH electrode into the pH standard solution whose value is 4.00.Then wait about one minute,adjust the gain potential device, let the value stabilise at around 4.00.At this time,the acidic calibration has been completed and you can measure the pH value of an acidic solution.
Note:If you want to measure the pH value of other solution,you must wash the pH electrode first! -
According to the linear characteristics of pH electrode itself, after the above calibration,you can directly measure the pH value of the alkaline solution, but if you want to get better accuracy, you can recalibrate it. Alkaline calibration use the standard solution whose pH value is 9.18.Also adjust the gain potential device, let the value stabilise at around 9.18. After this calibration, you can measure the pH value of the alkaline solution.
Sample Code
/*
# This sample code is used to test the pH meter V1.0.
# Editor : YouYou
# Ver : 1.0
# Product: analog pH meter
# SKU : SEN0161
*/
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
#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;
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;
}
Result
You'll see the pH value displayed in the serial monitor.
Usage and Maintenance
-
The electrode used for the first or long set without re-use, the electrode bulb and the sand core, immersed in the 3NKCL solution activated eight hours.
-
The electrode plug should be kept clean and dry.
-
Electrode reference solution is the 3NKCL solution.
-
Measurement should be avoided staggered pollution between solutions, so as not to affect the accuracy of measurement.
-
Electrode blub or sand core is defiled which will make PTS decline, slow response. So, it should be based on the characteristics of the pollutant, adapted to the cleaning solution, the electrode performance recovery.
-
The electrode should not be long-term immersed in acid chloride solution.
-
In order to ensure the accuracy of the pH probe, you need to use the standard solution to calibrate it regularly.Generally, the period is about half a year. If you meaure the dirty aqueous solution, you need to increase the frequency of calibration.
Additional Information
pH Electrode Characteristics
The output of pH electrode is Millivolts,and the pH value of the relationship is shown as follows (25 ℃):

Was this article helpful?
