Example Code for Arduino-Measure pH of Semisolid Material
Last revision 2026/01/12
This tutorial will demonstrate how to use this spear tip pH probe to measure the pH value of semisolid material,such as soil and food. Users can learn how to set up the hardware, and measure different semisolid materials (like fruit, meat, and wet soil) using Arduino.
Hardware Preparation
- SEN0249 Gravity: Analog Spear Tip 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 SEN0249) x1
- pH 4.0 standard buffer solution(Please prepare it independently) x 1
- pH 7.0 standard buffer solution(Please prepare it independently) x 1
Software Preparation
- Arduino IDE (V1.0.x, V1.6.x or V1.8.x), Click to Download Arduino IDE from Arduino®
Wiring Diagram

Sample Code
/***************************************************
DFRobot Gravity: Analog Spear Tip pH Sensor / Meter Kit (For Soil And Food Applications)
<https://www.dfrobot.com/wiki/index.php/Gravity:_Analog_Spear_Tip_pH_Sensor_/_Meter_Kit_(For_Soil_And_Food_Applications)_SKU:_SEN0249>
***************************************************
This product is used to measure the pH value of the semisolid,such as meat,fruit,moist soil and so on.
This sample code reads the pH value.
Created 2017-9-10
By Jason <[email protected]@dfrobot.com>
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/
/***********Notice and Trouble shooting***************
1. This code is tested on Arduino Uno and Leonardo with Arduino IDE 1.0.5 r2 and 1.8.2.
2. More details, please click this link: <https://www.dfrobot.com/wiki/index.php/Gravity:_Analog_Spear_Tip_pH_Sensor_/_Meter_Kit_(For_Soil_And_Food_Applications)_SKU:_SEN0249>
****************************************************/
#define PHSensorPin A2 //dissolved oxygen sensor analog output pin to arduino mainboard
#define VREF 5.0 //for arduino uno, the ADC reference is the AVCC, that is 5.0V(TYP)
#define OFFSET 0.00 //zero drift compensation
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; //store the analog value in the array, readed from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage,phValue;
void setup()
{
Serial.begin(115200);
pinMode(PHSensorPin,INPUT);
}
void loop()
{
static unsigned long analogSampleTimepoint = millis();
if(millis()-analogSampleTimepoint > 30U) //every 30 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(PHSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if(analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long printTimepoint = millis();
if(millis()-printTimepoint > 1000U)
{
printTimepoint = millis();
for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
{
analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
}
averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0; // read the value more stable by the median filtering algorithm
phValue = 3.5 * averageVoltage + OFFSET;
Serial.print("Voltage:");
Serial.print(averageVoltage,2);
Serial.print(" pH value:");
Serial.println(phValue,2);
}
}
int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i<iFilterLen; i++)
{
bTab[i] = bArray[i];
}
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}
Result
Open the Arduino IDE serial monitor with a baud rate of 115200. You will see the printed Voltage (in volts) and pH value (calculated from the analog input) displayed continuously. For example:
Voltage: 2.00 pH value: 7.00
Additional Information
Measuring the pH Value of Semisolid Material
After the calibration, the pH probe is ready for measuring the pH value of the semisolid material.
During measurement, if bubbles in the glass bulb of the electrode head were found, you could slightly thrown off the probe several times to make the bubbles disappear.
After inserting the pH probe into the test object, it is recommended to wait 5 minutes for reading.
The measurement method is as follows:
Measuring the pH Value of Fruit
Stab the pH probe spear tip into the flesh of a fruit. The probe should be stabbed about 10mm into the flesh of the fruit. After the measurement, pull out the probe and screw off the spear tip. Wash the probe with pure water or distilled water back and forth. Dry the probe carefully with filter papaer and screw the protection cap back to the probe for the next use.
Measuring the ph Value of Meat
Stab the pH probe spear tip into your meat.You should stab a centain depth to make sure the glass bulb of the probe has been closely touch with the meat. If the readings is unstable, you can pull the probe out, uninstall the spear tip and directly insert the probe back into the hole in the meat made by the spear tip previously. This will let the probe and the meat have close contact, and the readings will be more stable and reliable. After the measurement, pull out the probe and screw off the spear tip. Wash the probe with thin liquid soap or detergent to get rid of the fat. Then wash the probe with pure water or deionized water back and forth. Dry the probe carefully with filter paper and screw the protection cap back to the probe for the next use.
Measuring the ph Value of Wet Soil
⚠️The soil being measured must be wet! And does not contain hard or sharp material!
The probe can not directly measure dry material, so you should wet the soil with appropriate pure water or distilled water before measurement. Stab the pH probe which has been installed the spear tip into the wet soil.You should stab a centain depth to make sure the glass bulb of the probe closely touches the wet soil. After the measurement,pull out the probe and screw off the spear tip. Wash the probe with pure water or distilled water back and forth. Dry the probe carefully with filter papaer and screw the protection cap back to the probe for the next use.
Caution
-
It is forbidden to measure hard solid samples and semisolid samples containing sharp particles.
-
Please keep the plug of the probe, signal transmitter board (especially the BNC connector) clean and dry.
-
This pH probe is a laboratory electrode, which is not suitable for prolonged online testing. Therefore, do not immerse the electrode in the measured object for a long time.
-
After using the pH probe, please clean it carefully.
-
The components of this probe which touch the measured objects have a black ABS shell, a glass component and a silicone rubber material. Before testing the object, it is necessary to confirm that the measured objects are not harmful to these materials.
-
Not suitable for measurement: oily samples, samples with higher viscosity, samples with more fine particles.
-
Not suitable for non-aqueous titration.
-
The head of the stainless steel spear tip is sharp. Please use safety when using it.
Was this article helpful?
