Build Project

Agriculture

ESP32 Soil-Water pH: Two-Buffer Measurement Lesson

Use a defined SEN0161-V2 interface on classic ESP32 ADC1; record both buffer points, reject invalid readings and limit pH estimates to the recorded interval.

AdvancedAges 13+Setup time variesParts cost variesLow-voltage measurement lesson — unverified hardware
Two-buffer pH lesson logical overview; not a physical layout, photograph or recorded measurement
Jump to a section

What You Will Build

A Serial-only pH interpolation lesson for the DFRobot Gravity SEN0161-V2 board and its liquid probe. Raw ADC counts, ADC-pin millivolt estimates and calculated pH are separate. It does not report pH before valid two-buffer records, extrapolate beyond them, compensate temperature or claim laboratory accuracy. The overview is a logical illustration, not a photograph or recorded result.

Before You Start

Classic ESP32-WROOM-32/32E DevKit with free GPIO34 (ADC1 input only), Arduino-ESP32 3.3.2 review baseline, Arduino IDE 2.x and built-in ADC functions only. Not a latest-release claim or a pin map for S3/C3/C5. No external sensor library is needed for this explicit recorded-point calculation.

Parts

  • Classic ESP32-WROOM DevKitUSB-powered; 3.3 V logic, USB power and GPIO34 ADC1; module has separate 5 V supply.
  • DFRobot Gravity SEN0161-V2 board and supplied liquid pH probeFollow documented board labels. Other versions need their own interface review.
  • 15 kΩ and 10 kΩ resistors, both 1%, and 100 nF capacitorOne divider and node capacitor, not arbitrary resistor values.
  • Regulated 5.0 V module supply (±5%), breadboard, leads and USB data cableSeparate module supply positive from USB/controller positive; common ground. Power both off before rewiring.
  • Fresh pH 4/pH 7 buffers, thermometer and probe-care suppliesUse certificate values at actual temperature; separate aliquots; never return used buffer.

Wiring

Power the identified module from a separate regulated 5.0 V supply, ±5%, not from a GPIO. Its documented supply range is 3.3–5.5 V and output envelope is 0–3.0 V. USB powers the ESP32; never join the two positive supply outputs. Module AOUT feeds 15 kΩ (1%) then GPIO34; 10 kΩ (1%) and 100 nF connect this node to GND. Grounds are common. This divider scales the signal, not the module supply. Power both sides together while connected; power off USB and module supply before rewiring. Never connect a bare electrode or unknown output directly to GPIO34.

Logical DFRobot Gravity SEN0161-V2 board and supplied liquid pH probe circuit: separate regulated 5 V, common ground, 15k/10k divider and 100 nF at GPIO34; not physical pin order or tested hardware.

Open wiring diagram full size

  1. 1

    Switch USB and module supply off. Identify exact module, rail and connector labels.

  2. 2

    Module +/VCC → external regulated 5.0 V (±5%); module −/GND → common ESP32/external-supply GND. Never join positive rails. Connect the liquid probe only to BNC; keep BNC and interface dry.

  3. 3

    Module A/AOUT → 15 kΩ → GPIO34 node; 10 kΩ and 100 nF separately node → GND. Never bypass the divider.

  4. 4

    Check logical diagram before powering. Serial 115200; first leave the record-enable flag false and inspect raw counts/pin-mV diagnostics.

  5. 5

    Record both stable endpoints and conditions with this same chain before enabling calculation. Invalid/outside-interval signals deliberately give no calculated result.

Wiring Reference

SignalESP32 PinDirectionNotes
Module + / VCCNo GPIO; external regulated 5.0 V (±5%)SupplyIdentified module 3.3–5.5 V supply; distinct from signal rating.
Module − / GNDGNDCommon returnDivider bottom/capacitor share ground.
Module A / AOUT15 kΩ → GPIO34; 10 kΩ and 100 nF node → GNDADC1 inputDivided 0–3.0 V documented envelope; GPIO34 has no internal pull resistor.

Libraries and Setup

  • Arduino-ESP32 3.3.2 built-in ADC APIsNo DFRobot library or EEPROM dependency. Explicit recorded-point calculation, not library defaults.

Configure the Example

With CALIBRATION_RECORDED=false, rinse and condition the probe according to its instructions, then place in fresh pH 7 buffer. Keep BNC/board dry. Observe repeated ADC_pin_mV_est windows and slow drift; record stable means, times and temperature. Repeat in a fresh pH 4 aliquot after rinsing. Enter YOUR pin-mV means in PH7_PIN_MV/PH4_PIN_MV and certified buffer pH at the common temperature in PH7_VALUE/PH4_VALUE; only then enable the flag. Do not copy nominal manufacturer voltages or worksheet numbers. The identified V2 has higher output in acid buffer; equal/reversed/very close points are rejected. A small short-window span does not prove equilibrium.

pH = pH7 + (Vpin−V7)×(pH4−pH7)/(V4−V7). Coefficients come from your records, not a universal sensor formula. One point adjusts zero but cannot determine slope. Only interpolate within the two recorded endpoints. No temperature compensation is implemented: keep sample/buffers at the same recorded temperature and consult certificates; changed conditions require new records. Two-point fitting is not independent full-range accuracy verification. A separate check solution/reference instrument and uncertainty analysis would be needed.

This is a liquid probe, not a dry-soil spear. Use a documented laboratory soil-water extract/slurry method, recording soil/water ratio, water type, mixing/settling time and temperature. No universal recipe or direct comparison of differing sample preparations is implied. Protect the glass bulb from solids and follow probe care/storage guidance. No fertilizer or crop-treatment advice.

Code

Install the libraries above and match the configuration and wiring before uploading.

esp32-soil-ph-monitor.ino
#include <Arduino.h>
#include <math.h>
// Classic ESP32-WROOM DevKit; Arduino-ESP32 3.3.2 review baseline.
constexpr uint8_t SENSOR_PIN = 34; // ADC1 input only; no internal pull resistor.
// AOUT -- 15k (1%) -- GPIO34 -- 10k (1%) -- GND; 100 nF GPIO34 to GND.
// Raw and mV are separate conversions, averaged over the same short window.
float rawMean = 0, pinMv = 0;
bool readWindow() {
  uint32_t rawSum = 0, mvSum = 0, minMv = 100000, maxMv = 0;
  bool rails = false;
  for (int i = 0; i < 32; ++i) {
    uint16_t raw = analogRead(SENSOR_PIN);
    uint32_t mv = analogReadMilliVolts(SENSOR_PIN);
    rawSum += raw; mvSum += mv;
    if (raw <= 16 || raw >= 4079) rails = true;
    if (mv < minMv) minMv = mv;
    if (mv > maxMv) maxMv = mv;
    delay(5);
  }
  rawMean = rawSum / 32.0f; pinMv = mvSum / 32.0f;
  // Lesson screening, not electrical protection or sensor-health proof.
  return !rails && minMv >= 250 && maxMv <= 2200 && maxMv - minMv <= 20;
}

// No measured endpoint data is supplied. Record YOUR ADC-pin mV with this chain.
constexpr bool CALIBRATION_RECORDED = false;
constexpr float PH7_PIN_MV = 0.0f, PH4_PIN_MV = 0.0f;
// Use buffer certificate values at the actual common measurement temperature.
constexpr float PH7_VALUE = 7.00f, PH4_VALUE = 4.00f;
bool validCalibration() {
  return CALIBRATION_RECORDED && isfinite(PH7_PIN_MV) && isfinite(PH4_PIN_MV) &&
    PH7_PIN_MV >= 250 && PH4_PIN_MV <= 2200 && PH4_PIN_MV - PH7_PIN_MV >= 100 &&
    isfinite(PH7_VALUE) && isfinite(PH4_VALUE) &&
    PH7_VALUE >= 6.5f && PH7_VALUE <= 7.5f && PH4_VALUE >= 3.5f && PH4_VALUE <= 4.5f;
}
void setup() {
  Serial.begin(115200);
  analogReadResolution(12);
  analogSetPinAttenuation(SENSOR_PIN, ADC_11db);
  Serial.println("SEN0161-V2 limited-interval pH lesson; no accuracy verification");
}
void loop() {
  bool valid = readWindow();
  Serial.printf("raw_counts=%.1f ADC_pin_mV_est=%.1f\n", rawMean, pinMv);
  if (!valid) Serial.println("INVALID_SIGNAL: rail, range or short-window noise; no pH");
  else if (!validCalibration()) Serial.println("CALIBRATION_REQUIRED: record both stable buffer mV; no pH");
  else if (pinMv < PH7_PIN_MV || pinMv > PH4_PIN_MV)
    Serial.println("OUTSIDE_BUFFER_INTERVAL: no extrapolated pH");
  else {
    float ph = PH7_VALUE + (pinMv - PH7_PIN_MV) *
      (PH4_VALUE - PH7_VALUE) / (PH4_PIN_MV - PH7_PIN_MV);
    if (!isfinite(ph)) Serial.println("INVALID_CALCULATION: no pH");
    else Serial.printf("pH_interpolated_est=%.2f; no temperature compensation\n", ph);
  }
  delay(5000); // Reporting cadence, not a claim that the electrode stabilized.
}

How the Code Works

Acquisition diagnostics remain available before calibration. CALIBRATION_RECORDED defaults false and both pin-mV endpoints zero. Finite/range/separation checks precede division; interpolation is limited to the two buffers. Screening cannot establish sensor health: disconnected probes or stuck signals may look plausible.

Expected Output

Serial prints raw_counts/ADC_pin_mV_est then CALIBRATION_REQUIRED with unset constants. After valid records an in-interval signal may print pH_interpolated_est; invalid/noisy/outside-buffer signals print rejection, not pH. These are prospective message types, not recorded measurements or proof of calibration.

Troubleshooting

  • No calculated result Read the named state; record both stable endpoints with the same circuit. Zero defaults deliberately fail; enabling the flag alone is insufficient.
  • INVALID_SIGNAL Power off; check module, common ground, divider and GPIO34. Rail/zero, outside 250–2200 mV or >20 mV within-window span is rejected. Do not force a result.
  • OUTSIDE interval Do not clamp or extrapolate. Recheck conditions and references; pH beyond the two-buffer interval requires a separately justified range.
  • Plausible but stuck reading A disconnected electrode, floating module input or stuck signal may look plausible. Use controlled references and independent instruments; software cannot prove connection/health.

Checks to Perform on Your Build

  • Documentation/API and numerical review only; no compilation or hardware/calibration result supplied.
  • Identify actual module and verify rail/interface with suitable instruments before trusting output.
  • Record reference conditions and unedited diagnostics separately from expected messages.
  • Check rejected readings give no calculated pH.

Practical Design Notes

Raw counts are uncalibrated 12-bit codes, not volts or a physical quantity. analogReadMilliVolts uses the core ADC calibration path to estimate voltage at the divided pin; it is not independent system calibration or a voltmeter accuracy guarantee. Raw and mV use separate conversions. Classic ESP32 at 11 dB has a documented measurement range of about 150–3100 mV, distinct from 3.3 V logic. The sketch accepts only 250–2200 mV and rejects rail samples or more than 20 mV span within 32 samples. These are conservative lesson screening choices, not manufacturer sensor specifications. Slow drift and plausible wiring faults can pass.

Vpin=Vout×Rb/(Rt+Rb). For Rt=15 kΩ and Rb=10 kΩ, ratio=0.4 nominal; with 1% resistors the largest ratio is 10.1/(14.85+10.1)=0.40481. Documented 3.0 V output gives at most 1.2145 V. A 5.0 V supply at +5% gives 5.25 V; even output-to-supply pass-through gives only 2.1253 V at the ADC. At the module maximum 5.5 V, the bound is 2.2265 V, below the classic 11 dB range ceiling around 3.1 V; the sketch rejects above 2.2 V. This bounded calculation is not protection from arbitrary supplies, miswiring or transients. The divider also pulls a disconnected signal wire low, but does not prove electrode health. Nominal module voltage is 2.5×pin voltage; resistor-ratio uncertainty alone is about ±1.2%, before ADC, loading and sensor errors. Endpoint records are ADC-pin values from this complete chain; changing any part invalidates them.

Upgrade Ideas

  • Assess uncertainty with a separate check/reference and recorded evidence; none is supplied here.

FAQs

Does this establish sensor accuracy?

No. ADC calibration and two recorded endpoints do not independently validate accuracy, drift or sample preparation.

Does rejection detect every disconnected sensor?

No. It rejects obvious rail/range/noise problems, but a plausible voltage can still be faulty.

Review, Testing, and References

Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-11-17. Reviewed: wiring logic, Arduino code structure, beginner safety, and learning sequence.

Educational level: Advanced. Estimated completion time: Setup time varies. This project is for learning and prototyping; production or unattended hardware needs additional engineering review.