Example Code for UNIHIKER K10-Hydroponic System Sensing
How to build and calibrate a K10 hydroponic pH monitor: wire the SEN0169-V2 pH sensor and DS18B20 to UNIHIKER K10, install Arduino IDE and DFRobot_PH, then upload the sample code that reads ADC voltage, applies temperature-compensated pH conversion, supports serial commands enterph/calph/exitph for two-point calibration with pH 7.0 and 4.0 buffers, and displays temperature and pH on both serial monitor and K10 screen; it’s like tuning a digital thermometer so nutrient solution readings stay accurate, while careful probe cleaning, KCl storage and dry BNC connections maintain long-term stability
Hardware Preparation
- SEN0169-V2 Gravity: Analog pH Sensor V2 x1
- Standard Buffer Solution 4.0(Prepare it yourself) x1
- Standard Buffer Solution 7.0(Prepare it yourself) x1
- DFR0992-EN UNIHIKER K10 x1
- PH2.0-3P Male-to-Male Cable x1
- KIT0021 Gravity: Waterproof DS18B20 Temperature Sensor Kit x 1
- Gravity: Digital Sensor Cable(Comes with KIT0021) x1
Software Preparation
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.
Calibration 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 P0
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)/4096.0*3300; // 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.
-
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.
-
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.
-
After the values are stable, the first point can be calibrated. Specific steps are as follows:
- Input
enterphcommand in the serial monitor to enter the calibration mode.
- Input
calphcommands 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.
- After the calibration, input
exitphcommand in the serial monitor to save the relevant parameters and exit the calibration mode.
Note: Only after inputingexitphcommand in the serial monitor can the relevant parameters be saved.
- After the above steps, the first point calibration is completed. The second point calibration will be performed below.
- Input
-
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.
-
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:
- Input
enterphcommand in the serial monitor to enter the calibration mode. - Input
calphcommands 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. - After the calibration, input the
exitphcommand in the serial monitor to save the relevant parameters and exit the calibration mode.
Note: Only after inputingexitphcommand in the serial monitor can the relevant parameters be saved. - After the above steps, the second point calibration is completed.
- Input
-
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.
Sample Code
#include <Arduino.h>
#include "unihiker_k10.h"
#include <OneWire.h>
#include "DFRobot_PH.h"
#include <EEPROM.h>
// ---------- Pin configuration ----------
// DS18B20 data pin. Update this if your wiring differs.
constexpr uint8_t DS18B20_PIN = P1;
// pH sensor analog output pin.
constexpr uint8_t PH_PIN = P0;
// For ESP32-based boards, ADC full scale is usually 4095 and Vref is 3300mV.
// If your board uses different ADC reference, adjust these two constants.
constexpr float ADC_MAX = 4095.0f;
constexpr float ADC_REF_MV = 3300.0f;
UNIHIKER_K10 k10;
OneWire ds(DS18B20_PIN);
DFRobot_PH ph;
uint8_t screen_dir = 2;
float readTemperatureDS18B20();
void setup() {
Serial.begin(115200);
k10.begin();
k10.initScreen(screen_dir);
k10.creatCanvas();
k10.setScreenBackground(0x000000);
ph.begin();
k10.canvas->canvasText("K10 pH + Temp Demo", 1, 0x00FF66);
k10.canvas->canvasText("Init sensors...", 2, 0xFFFFFF);
k10.canvas->updateCanvas();
}
void loop() {
static unsigned long lastSampleMs = 0;
if (millis() - lastSampleMs < 1000U) {
return;
}
lastSampleMs = millis();
// 1) Read DS18B20 temperature (used for pH temperature compensation)
float temperatureC = readTemperatureDS18B20();
if (temperatureC < -100.0f) {
// Fallback value if sensor read fails, so pH can still be computed.
temperatureC = 25.0f;
}
// 2) Read pH probe voltage (mV)
float voltageMv = analogRead(PH_PIN) / ADC_MAX * ADC_REF_MV;
// 3) Temperature compensation is applied inside readPH(voltage, temperature)
float phValue = ph.readPH(voltageMv, temperatureC);
// Keep serial calibration command behavior from DFRobot example.
ph.calibration(voltageMv, temperatureC);
Serial.print("temperature:");
Serial.print(temperatureC, 2);
Serial.print(" C voltage:");
Serial.print(voltageMv, 1);
Serial.print(" mV pH:");
Serial.println(phValue, 2);
String line1 = "Temp: " + String(temperatureC, 2) + " C";
String line2 = "pH: " + String(phValue, 2);
String line3 = "ADC mV: " + String(voltageMv, 1);
String line4 = "Comp: pH(T) enabled";
k10.canvas->canvasClear();
k10.canvas->canvasText("K10 pH + Temp Demo", 1, 0x00FF66);
k10.canvas->canvasText(line1, 2, 0xFFFFFF);
k10.canvas->canvasText(line2, 3, 0x66CCFF);
k10.canvas->canvasText(line3, 4, 0xAAAAAA);
k10.canvas->canvasText(line4, 5, 0xFFCC33);
k10.canvas->updateCanvas();
}
float readTemperatureDS18B20() {
byte data[9];
byte addr[8];
if (!ds.search(addr)) {
ds.reset_search();
return -1000.0f;
}
if (OneWire::crc8(addr, 7) != addr[7]) {
return -1000.0f;
}
// 0x10: DS18S20, 0x28: DS18B20
if (addr[0] != 0x10 && addr[0] != 0x28) {
return -1000.0f;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // Start temperature conversion
delay(750);
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read scratchpad
for (uint8_t i = 0; i < 9; i++) {
data[i] = ds.read();
}
ds.reset_search();
int16_t raw = (static_cast<int16_t>(data[1]) << 8) | data[0];
return static_cast<float>(raw) / 16.0f;
}
Result
After uploading the code and opening the serial monitor (115200 baud), you will see temperature of DS18B20 and pH values printed every second
pH Probe Maintenance Notes
-
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.
-
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.
-
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.
-
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.
-
The probe should avoid long-term immersion in distilled water, protein, acid fluoride solution, and prevent contact with silicone oil.
-
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.
-
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.
-
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!
Next Steps
Looking for a different controller? See the Arduino version or FireBeetle ESP32 version.
- See how this fits into a complete automated hydroponics setup — EC monitoring, controller selection, and budget planning
- Browse all DFRobot liquid sensors
Was this article helpful?
