ESP8266 pH Sensor Project
This project demonstrates how to use an ESP8266 microcontroller to interface with a pH sensor. The pH sensor measures the acidity or alkalinity of a solution and outputs an analog signal that the ESP8266 reads and converts into a pH value.
How It Works
A pH probe is an electrochemical cell. A thin glass membrane at its tip develops a potential difference that depends on the hydrogen-ion activity of the solution, measured against a stable internal reference electrode. The relationship follows the Nernst equation: about 59.16 mV per pH unit at 25 °C, with 0 mV at pH 7.
That signal cannot be read directly. The probe's source impedance is enormous — on the order of 100 MΩ — so any ordinary input loads it down to nothing. A signal-conditioning board such as the PH-4502C provides the required high-impedance buffer amplifier and shifts the ±414 mV range up into something an ADC can sample.
The ESP8266 (NodeMCU) samples with a 10-bit ADC, so analogRead() returns 0–1023 across 3.3 V at the NodeMCU A0 header (the bare chip reads 0–1 V). Because the useful span is only a few hundred millivolts spread over the full ADC range, a stable supply and careful averaging matter more here than with most sensors.
Calibration is mandatory and perishable. Probes drift as the glass membrane ages, so a two-point calibration with pH 6.86 and pH 4.00 buffer solutions should be repeated regularly — monthly for continuous use.
Components Needed
- ESP8266 (NodeMCU)
- pH Sensor module
- ESP8266 (NodeMCU)
- USB cable for programming and power
- ESP8266 (NodeMCU)
Wiring to the ESP8266 (NodeMCU)
Connect Po to A0, V+ to 5 V and GND to ground, then screw the probe onto the BNC connector. The PH-4502C is a 5 V board and its output can exceed 3.3 V, so a divider between Po and the ADC is required on this board — connecting it directly risks damage.
The two trimmers on the PH-4502C are not interchangeable. The one nearest the BNC sets the offset — adjust it with the probe in pH 7 buffer until the output reads mid-scale. The other sets the alarm threshold and is irrelevant for analog reading.
Keep the probe cable away from mains wiring, motors and relays. The signal is tiny and high-impedance, which makes it an excellent antenna for electrical noise.
| Board pin | ESP8266 (NodeMCU) pin | Function |
|---|---|---|
| Po (analog out) | A0 | Buffered pH voltage |
| V+ | 5V | Supply — the PH-4502C expects 5 V |
| G / GND | GND | Common ground |
| BNC | — | Probe connection |
Build and Upload
After uploading the code, open the Serial Monitor (baud rate: 9600).
The Serial Monitor will display the pH value of the solution connected to the pH sensor.
Immerse the pH sensor in the solution to observe the pH value.
Example Code
Two-point calibrated pH reading with median filtering. Upload it with the board set to ESP8266 (NodeMCU) and open the Serial Monitor at 115200 baud.
const int PH_PIN = A0;
const float VREF = 3.3;
const int ADC_MAX = 1023;
// From two-point calibration — replace with your own measured values
const float V_PH7 = 2.50; // volts measured in pH 6.86 buffer
const float V_PH4 = 3.04; // volts measured in pH 4.00 buffer
float slope() { return (7.0 - 4.0) / (V_PH7 - V_PH4); }
float readVolts() {
int s[9];
for (int i = 0; i < 9; i++) { s[i] = analogRead(PH_PIN); delay(10); }
for (int i = 0; i < 8; i++)
for (int j = i + 1; j < 9; j++)
if (s[j] < s[i]) { int t = s[i]; s[i] = s[j]; s[j] = t; }
return s[4] * VREF / ADC_MAX; // median of nine
}
void setup() { Serial.begin(115200); }
void loop() {
float v = readVolts();
float ph = 7.0 + (v - V_PH7) * slope();
Serial.print("V=");
Serial.print(v, 3);
Serial.print(" pH=");
Serial.println(ph, 2);
delay(1000);
}
Applications
A ph sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Hydroponic and aquaponic nutrient monitoring, where pH drives nutrient uptake
- Aquarium and pond water-quality logging
- Swimming pool chemistry management
- Soil slurry testing in agriculture
- Process control in brewing, fermentation and small-scale chemistry
Working with the ESP8266 (NodeMCU)
The ESP8266 (NodeMCU) is built around the ESP8266 and runs on 3.3 V logic with roughly 80 KB usable of SRAM and 4 MB on most NodeMCU boards of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The ESP8266 is a 3.3 V part and is not 5 V tolerant — feeding a 5 V sensor output straight into a GPIO can damage the chip. Use a divider or level shifter.
There is only one ADC channel, so reading several analog sensors needs an external multiplexer such as a CD4051 or an ADS1115.
D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) are strapping pins sampled at boot; holding them at the wrong level stops the board starting.
Built-in WiFi means the same sketch can publish readings to a dashboard without extra hardware.
| ESP8266 (NodeMCU) characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 3.3 V | Sensor outputs above this level need a divider or level shifter |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | a single analog channel, A0 | Determines how many analog sensors can share the board |
| PWM outputs | any GPIO via software PWM | Needed for brightness, speed and tone control |
| I²C pins | D2 (SDA, GPIO4) and D1 (SCL, GPIO5) by default | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | any GPIO except D0 (GPIO16) | Required for counting fast or asynchronous events |
| Serial | one hardware UART plus a transmit-only second port | Monitor runs at 115200 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- The reading barely moves between buffers — the probe is dead or the BNC is not seated. A healthy probe shows a clear difference between pH 4 and pH 7.
- Readings drift steadily upward or downward — the probe needs recalibration, or it has dried out.
- Values jump when a pump or heater switches — electrical noise is coupling in. Ground the solution with a stainless probe and separate the cable from power wiring.
- pH reads correctly at room temperature but not when hot — the Nernst slope is temperature dependent. Add a temperature sensor and compensate.
- The probe was stored dry — glass electrodes must be kept in storage solution. A dried probe may never recover.
- The sketch compiles but the board resets or behaves erratically — a 5 V module output is being driven into a 3.3 V pin. Measure the signal before connecting it.
- Readings differ from an Arduino tutorial for the same part — the 10-bit ADC returns 0–1023, not 0–1023, so any constant copied from an Uno example needs rescaling.
Taking It Further on the ESP8266 (NodeMCU)
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the ESP8266 (NodeMCU) specifically:
The obvious extension on an ESP8266 is to stop printing to Serial and publish instead. A few lines using ESP8266WiFi and an HTTP client will push each reading to a dashboard such as ThingSpeak, or to an MQTT broker for home automation.
Running an onboard web server turns the board into its own display: serve a small HTML page that fetches the latest reading over AJAX, and any phone on the network becomes the instrument panel.
For battery operation, deep sleep is essential — the ESP8266 draws around 70 mA with WiFi active but under 20 µA asleep. Wire D0 (GPIO16) to RST so the board can wake itself, take a reading, publish and sleep again.
Notes and Practical Limits
Never store a pH probe in distilled water. It leaches ions out of the glass membrane and ruins it. Use the manufacturer's storage solution, or failing that, pH 4 buffer.
Temperature compensation is not optional for accurate work. At 50 °C the Nernst slope is about 64 mV per pH unit rather than 59 mV, which is a significant error if ignored.