pH Sensor Arduino: PH-4502C Wiring, Calibration and Code
pH sensor Arduino guide: PH-4502C wiring, offset adjustment, two-point calibration with pH 4 and 7 buffers, voltage-to-pH formula, temperature and ESP32.
Pair a pH sensor with an Arduino and you get a meter that tells you, as a number, how acidic or alkaline a liquid is: aquarium water, a hydroponic nutrient solution, pool water or the lemon juice in a school experiment. The most common hobby kit is a glass pH electrode plus a blue amplifier board called the PH-4502C. The electrode produces a signal of just a few millivolts; the board brings it into the 0–5 V range the Arduino can read, and after calibrating with two buffer solutions you convert that voltage into pH.
In this guide you'll learn how a pH electrode works, the pins and two potentiometers of the PH-4502C, the offset adjustment, two-point calibration with pH 4 and pH 7 buffers and the formula that turns voltage into pH. You'll see in a table how temperature affects the reading, build working code for the Arduino Uno, ESP32 (with a voltage divider) and Raspberry Pi Pico, and finally go through the care and storage rules that keep an electrode alive for a long time.
What is pH, and how does a pH electrode work?
pH is a logarithmic measure of the hydrogen ion concentration in a solution and runs from 0 to 14: 7 is neutral, below is acidic and above is alkaline (basic). Because the scale is logarithmic, a liquid at pH 5 is ten times more acidic than one at pH 6. The thin glass membrane at the tip of the electrode produces a small voltage proportional to the pH difference between its two sides; the Ag/AgCl reference electrode and the KCl filling inside make that voltage measurable. According to the Nernst equation, at 25 °C each pH unit is worth about 59.16 mV, and at pH 7 the electrode voltage is zero: positive in acids, negative in bases. Since the membrane's internal resistance is hundreds of megaohms, the signal can't go straight into the Arduino; you need an amplifier with a very high input impedance, such as the PH-4502C.
| Liquid | Approximate pH |
|---|---|
| Lemon juice | 2.0–2.5 |
| Vinegar | 2.5–3.0 |
| Cola | 2.5 |
| Coffee | 5.0 |
| Rainwater | 5.6 |
| Hydroponic nutrient solution (target) | 5.5–6.5 |
| Distilled water (exposed to air) | 5.5–7.0 |
| Tap water | 6.5–8.5 |
| Pool water (target) | 7.2–7.6 |
| Seawater | 8.1 |
| Baking soda solution | 8.3 |
| Bleach | 12–13 |
The PH-4502C and analog pH kits
At one end of the PH-4502C board is the BNC connector the electrode screws into, and at the other a six-pin header. The board has two potentiometers: on most boards the one nearer the BNC is the offset and sets the output voltage at pH 7, while the other sets the threshold of the Do output. The board amplifies the electrode signal about three times and sits it on a 2.5 V midpoint; as a result, each pH unit equals a change of about 0.17–0.18 V at the Po output, and the voltage falls as pH rises. Other analog kits on the market (DFRobot Gravity, for example) use a different midpoint and slope; two-point calibration corrects all of these differences automatically.
| PH-4502C pin | Function |
|---|---|
| V+ | Supply, 5 V |
| G | Ground (one of the two G pins is enough) |
| Po | Analog pH output, goes to the ADC |
| Do | Digital threshold output, changes state when the set pH limit is crossed |
| To | Board temperature output; shows the board's temperature, not the liquid's |
| Feature | PH-4502C | DFRobot pH V1 | DFRobot pH V2 |
|---|---|---|---|
| Supply | 5 V | 5 V | 3.3–5.5 V |
| Output range | 0–5 V | 0–5 V | 0–3 V |
| Output at pH 7 | About 2.5 V (set with the offset pot) | About 2.0 V | About 1.5 V |
| As pH rises, voltage | Falls | Rises | Falls |
| Direct to ESP32 and Pico | No, needs a divider | No, needs a divider | Yes |
Wiring: Arduino, ESP32 and Pico
| PH-4502C | Arduino Uno | ESP32 | Raspberry Pi Pico |
|---|---|---|---|
| V+ | 5V | 5V (VIN) | VBUS (pin 40) |
| G | GND | GND | GND |
| Po | A0 | GPIO 34 via the divider | GP26 (ADC0) via the divider |
| Do | D2 (optional) | Not used | Not used |
| Voltage divider | Not needed | Po → 10 kΩ → GPIO 34 → 20 kΩ → GND | Po → 10 kΩ → GP26 → 20 kΩ → GND |
Offset adjustment: setting pH 7 to 2.5 V
- Remove the electrode and power the board with 5 V; measure the Po pin with a multimeter or the Arduino's A0 input.
- Short the centre contact of the BNC connector to its outer metal body with a piece of wire. This mimics an electrode producing 0 mV, in other words pH 7.
- Turn the offset potentiometer with a small screwdriver until Po reads 2.50 V.
- Remove the shorting wire and screw on the electrode; the calibration in the next step will do the fine-tuning.
Two-point calibration: pH 4 and pH 7 buffers
No electrode gives exactly the theoretical values, and its slope drops as it ages, so a pH reading without calibration is meaningless. Calibration uses buffer solutions of known pH. The powder buffers that come with kits are usually pH 4.00, 6.86 and 9.18, each sachet dissolved in 250 ml of distilled water. For most measurements the pH 4.00 and 6.86 (or 7.00) pair is enough; if you'll mostly measure above pH 7, use the 9.18 buffer as a check too.
The formula for the line through the two points is pH = pHn + (V − Vn) × (pHa − pHn) / (Va − Vn). Here pHn and Vn are the neutral buffer's value and the voltage you measured in it, and pHa and Va the same for the acidic buffer. For example, if you measured 2.50 V at pH 7 and 3.03 V at pH 4, the slope is (4 − 7) / (3.03 − 2.50) = −5.66 pH/V, and a sample reading 2.80 V has a pH of 7 + 0.30 × (−5.66) = 5.30. Because the formula works out the sign of the slope by itself, it works just the same with kits whose output moves the other way.
| pH | Electrode voltage (25 °C) | PH-4502C Po (typical) | ESP32 and Pico pin (after the divider) |
|---|---|---|---|
| 0 | +414 mV | 3.74 V | 2.49 V |
| 4 | +177 mV | 3.03 V | 2.02 V |
| 7 | 0 mV | 2.50 V | 1.67 V |
| 10 | −177 mV | 1.97 V | 1.31 V |
| 14 | −414 mV | 1.26 V | 0.84 V |
- Rinse the electrode with distilled water and dry the tip by gently dabbing it with a tissue; don't wipe it, because that charges the glass membrane with static.
- Dip the electrode into the neutral buffer (6.86 or 7.00), stir gently and wait 1–2 minutes for the value to settle.
- Type n in the Serial Monitor and send it; the code saves the current voltage to EEPROM as the neutral point.
- Rinse the electrode again, dip it into the pH 4.00 buffer, wait and send a.
- To check, put the electrode back into the neutral buffer; the reading shouldn't differ from the buffer value by more than 0.05 pH.
1#include <EEPROM.h>
2
3const int PH_PIN = A0; // PH-4502C Po çıkışı / Po output
4const float VREF = 5.0; // Uno ADC referansı / Uno ADC reference
5const float BUFFER_ACID = 4.00; // asidik tampon / acidic buffer
6const float BUFFER_NEUTRAL = 6.86; // elindeki nötr tampon: 6.86 ya da 7.00 / your neutral buffer: 6.86 or 7.00
7
8float vNeutral = 2.50; // nötr tampondaki gerilim / voltage in the neutral buffer
9float vAcid = 3.03; // pH 4 tamponundaki gerilim / voltage in the pH 4 buffer
10
11float readVoltage() {
12 int samples[20];
13 for (int i = 0; i < 20; i++) {
14 samples[i] = analogRead(PH_PIN);
15 delay(30);
16 }
17 for (int i = 0; i < 19; i++) { // küçükten büyüğe sırala / sort ascending
18 for (int j = i + 1; j < 20; j++) {
19 if (samples[j] < samples[i]) {
20 int t = samples[i];
21 samples[i] = samples[j];
22 samples[j] = t;
23 }
24 }
25 }
26 long sum = 0;
27 for (int i = 5; i < 15; i++) sum += samples[i]; // ortadaki 10 ölçüm / middle 10 readings
28 return sum / 10.0 * VREF / 1023.0;
29}
30
31float voltageToPh(float v) {
32 if (fabs(vAcid - vNeutral) < 0.05) return NAN; // kalibrasyon geçersiz / invalid calibration
33 float slope = (BUFFER_ACID - BUFFER_NEUTRAL) / (vAcid - vNeutral); // pH / V
34 return BUFFER_NEUTRAL + (v - vNeutral) * slope;
35}
36
37void setup() {
38 Serial.begin(9600);
39 float a, b;
40 EEPROM.get(0, a);
41 EEPROM.get(4, b);
42 if (!isnan(a) && !isnan(b) && a > 0 && a < 5 && b > 0 && b < 5 && fabs(a - b) > 0.1) {
43 vNeutral = a; // kayıtlı kalibrasyonu yükle / load saved calibration
44 vAcid = b;
45 }
46 Serial.println(F("Kalibrasyon / Calibration: n = notr tampon / neutral buffer, a = pH 4"));
47}
48
49void loop() {
50 float v = readVoltage();
51 Serial.print(F("V = "));
52 Serial.print(v, 3);
53 Serial.print(F(" pH = "));
54 Serial.println(voltageToPh(v), 2);
55
56 if (Serial.available()) {
57 char c = Serial.read();
58 if (c == 'n') {
59 vNeutral = v;
60 EEPROM.put(0, vNeutral); // kalıcı kaydet / save permanently
61 Serial.println(F("Notr kaydedildi / Neutral saved"));
62 } else if (c == 'a') {
63 vAcid = v;
64 EEPROM.put(4, vAcid);
65 Serial.println(F("pH 4 kaydedildi / pH 4 saved"));
66 }
67 }
68 delay(1000);
69}For every reading the code takes 20 samples, sorts them, throws away the five lowest and five highest and averages the middle ten; this suppresses the odd spikes caused by a pump or motor far better than a simple average. The calibration values are stored as floats at EEPROM addresses 0 and 4, so you don't have to recalibrate every time you switch the Arduino off and on. With an empty EEPROM the stored value reads as NAN, in which case the code uses the defaults of 2.50 V and 3.03 V. If your neutral buffer is 7.00, change the BUFFER_NEUTRAL line to 7.00.
How temperature affects pH readings
Temperature affects the reading in two ways. The first is the electrode's slope: the Nernst slope is proportional to absolute temperature, so the 59.16 mV/pH at 25 °C drops to 56.18 mV/pH at 10 °C. A system calibrated at 25 °C makes an error on a cold sample that grows the further the pH is from 7; near pH 7 the error is almost zero. The second is that the sample's own pH really does change with temperature; that's a chemical change and no correction removes it. That's why pH values are usually reported together with the temperature they were measured at.
| Temperature | Nernst slope | A pH 4 sample reads (uncorrected) | A pH 10 sample reads (uncorrected) |
|---|---|---|---|
| 0 °C | 54.20 mV/pH | 4.25 | 9.75 |
| 10 °C | 56.18 mV/pH | 4.15 | 9.85 |
| 20 °C | 58.16 mV/pH | 4.05 | 9.95 |
| 25 °C | 59.16 mV/pH | 4.00 | 10.00 |
| 30 °C | 60.15 mV/pH | 3.95 | 10.05 |
| 40 °C | 62.13 mV/pH | 3.85 | 10.15 |
| 50 °C | 64.12 mV/pH | 3.75 | 10.25 |

Measuring pH with the ESP32: voltage divider and temperature compensation
On the ESP32, connect Po to GPIO 34 through a divider made of a 10 kΩ resistor (top) and a 20 kΩ resistor (bottom); that pin belongs to ADC1 and keeps working while Wi-Fi is on. Since the divider reduces the voltage to 2/3, the code multiplies the reading by 1.5 to get the module's real output. For reading we use analogReadMilliVolts(), which applies the ADC calibration stored in the ESP32 at the factory and gives a much more linear result than raw analogRead. For temperature, connect a waterproof DS18B20 to GPIO 4 with a 4.7 kΩ pull-up resistor; details are in DS18B20 Temperature Sensor. To see how the divider is calculated, use the voltage divider calculator.
1#include <OneWire.h>
2#include <DallasTemperature.h>
3
4const int PH_PIN = 34; // bölücü çıkışı, ADC1 / divider output, ADC1
5const int TEMP_PIN = 4; // DS18B20 veri, 4,7 kΩ pull-up / data, 4.7 kΩ pull-up
6const float DIVIDER = 1.5; // 10 kΩ üst + 20 kΩ alt: Vmodül = Vpin × 1,5 / module V = pin V × 1.5
7const float BUFFER_ACID = 4.00;
8const float BUFFER_NEUTRAL = 6.86;
9const float CAL_TEMP = 25.0; // kalibrasyon sırasındaki sıcaklık / temperature during calibration
10float vNeutral = 2.50; // kalibrasyonda ölçtüğün gerilimler / voltages from your calibration
11float vAcid = 3.03;
12
13OneWire oneWire(TEMP_PIN);
14DallasTemperature tempSensor(&oneWire);
15
16float readModuleVoltage() {
17 uint32_t sum = 0;
18 for (int i = 0; i < 32; i++) {
19 sum += analogReadMilliVolts(PH_PIN); // fabrika kalibrasyonlu okuma / factory-calibrated reading
20 delay(10);
21 }
22 return sum / 32.0 / 1000.0 * DIVIDER;
23}
24
25void setup() {
26 Serial.begin(115200);
27 tempSensor.begin();
28}
29
30void loop() {
31 tempSensor.requestTemperatures();
32 float t = tempSensor.getTempCByIndex(0);
33 if (t == DEVICE_DISCONNECTED_C) t = CAL_TEMP; // sensör yoksa düzeltme yapma / no correction without a sensor
34 float v = readModuleVoltage();
35 float phCal = BUFFER_NEUTRAL + (v - vNeutral) * (BUFFER_ACID - BUFFER_NEUTRAL) / (vAcid - vNeutral);
36 float ph = 7.0 + (phCal - 7.0) * (273.15 + CAL_TEMP) / (273.15 + t); // Nernst eğimi düzeltmesi / Nernst slope correction
37 Serial.printf("T %.1f C | V %.3f | pH %.2f\n", t, v, ph);
38 delay(2000);
39}The correction formula is pH = 7 + (pHc − 7) × (273.15 + Tc) / (273.15 + T): pHc is the value from the calibration line, Tc the temperature during calibration and T the sample temperature. The formula assumes the electrode's zero point is at pH 7 and corrects only its slope. If you calibrate with the ESP32, simply read the uncorrected voltage in the Serial Monitor and write it into vNeutral and vAcid; if you like, you can also move the serial-command calibration from the Arduino example over here using the Preferences library. Adding a 100 nF capacitor between GPIO 34 and GND helps a lot to reduce ADC noise.
Measuring pH with the Raspberry Pi Pico and MicroPython
The Pico's ADC is also limited to 3.3 V, so you connect the same 10 kΩ / 20 kΩ divider to GP26 (ADC0). In MicroPython read_u16() returns a value from 0 to 65535; scale it to 3.3 V and multiply by 1.5 to get the module's output voltage. For calibration follow the Arduino steps and write the two voltages you measure into the v_neutral and v_acid variables in the code.
1from machine import ADC
2import time
3
4adc = ADC(26) # GP26 = ADC0, bölücü çıkışı / divider output
5DIVIDER = 1.5 # 10 kΩ üst, 20 kΩ alt / 10 kΩ top, 20 kΩ bottom
6BUFFER_ACID, BUFFER_NEUTRAL = 4.00, 6.86
7v_acid, v_neutral = 3.03, 2.50 # kalibrasyonda ölçtüğün gerilimler / voltages from your calibration
8
9def module_voltage():
10 total = 0
11 for _ in range(32):
12 total += adc.read_u16()
13 time.sleep_ms(10)
14 return total / 32 * 3.3 / 65535 * DIVIDER
15
16while True:
17 v = module_voltage()
18 ph = BUFFER_NEUTRAL + (v - v_neutral) * (BUFFER_ACID - BUFFER_NEUTRAL) / (v_acid - v_neutral)
19 print("V = {:.3f} pH = {:.2f}".format(v, ph))
20 time.sleep(2)Electrode care and storage
A cheap lab-type electrode lasts 1–2 years if you look after it; stored the wrong way, it can die within a few weeks. The most important rule is that the glass membrane must never dry out and should always stay moist. A new electrode usually comes with KCl solution inside its protective cap; that cap is the electrode's bed and should go back on as soon as you finish measuring.
| Situation | What to do |
|---|---|
| Between measurements | Rinse with distilled water and dry by dabbing gently with a tissue (no wiping) |
| Storage | Put a few drops of 3 M KCl storage solution in the protective cap and fit it |
| Electrode has dried out | Soak it in 3 M KCl for at least 12–24 hours, then recalibrate |
| Oily or sticky dirt | Rinse with warm water and a little washing-up liquid, then plenty of distilled water |
| Limescale and mineral deposits | Soak for a few minutes in 0.1 M HCl (with gloves and goggles), rinse thoroughly |
| Very slow response, calibration won't hold | The electrode may have reached the end of its life; replace it |
pH sensor project ideas
- Hydroponic pH control: when the pH leaves the 5.5–6.5 range, add a few drops of pH down or pH up with a small water pump.
- Water quality station: measure pH together with a TDS sensor and temperature; run the two sensors one after the other, not at the same time.
- Aquarium monitor: measure pH hourly with an ESP32 and send an alert via Telegram when it goes out of range.
- Soil pH: mix one part soil with two and a half parts distilled water, let it settle and measure the water on top; for nutrients, have a look at the NPK soil sensor as well.
- School experiment: measure the pH of lemon juice, vinegar, milk, baking soda and soapy water and compare the results with a red cabbage indicator.
- Pool water monitor: keep an eye on the 7.2–7.6 pH range and show it on an LCD and your phone.
Common mistakes and how to fix them
| Symptom | Likely cause | Fix |
|---|---|---|
| The value stays around 7 and never changes | Loose BNC, electrode not connected or dried out | Tighten the BNC, soak the electrode in KCl |
| The value keeps drifting | The electrode hasn't settled, the temperature is changing or a pump in the water leaks current | Wait 1–2 minutes, switch the pump off while measuring |
| Reads high in acids and low in bases | The sign of the slope was assumed wrongly | Do a two-point calibration; the formula finds the direction itself |
| Correct in the buffer, nonsense in the sample | Electrode not rinsed, buffer contaminated | Rinse with distilled water before every dip |
| Unsteady value on the ESP32 | ADC noise, Wi-Fi load, an ADC2 pin | Use an ADC1 pin, average many samples, add a 100 nF capacitor |
| Less than 0.3 V between pH 4 and 7 | The electrode is old or dead | Replace the electrode |
| The ESP32 pin gets warm or the board resets | Po connected without a divider | Add the 10 kΩ / 20 kΩ divider immediately |
At first a pH sensor seems delicate and fussy, but with a proper offset adjustment, regular two-point calibration and moist storage it will give you reliable readings for years. In the BlueGrays app you'll find step-by-step projects with circuit diagrams and code for analog reading, a digital voltmeter with an LCD, an NTC thermometer, a smart irrigation station and an ESP32 temperature logger; adding a pH sensor to any of them as a second measurement is easy.
Frequently asked questions
How do you calibrate a pH sensor with Arduino?
First dip the electrode into a pH 7 (or 6.86) buffer and save the voltage, then rinse it and do the same in a pH 4 buffer. The line through those two points converts the measured voltage into pH. For alkaline measurements it's a good idea to check with a pH 9.18 or 10 buffer.
What do the two potentiometers on the PH-4502C do?
On most boards the potentiometer nearer the BNC sets the offset, meaning the output voltage at pH 7; with the BNC shorted you turn it until Po reads 2.50 V. The other one sets the pH value at which the Do digital output changes state.
Can I connect a pH sensor directly to an ESP32?
The PH-4502C's output can reach 5 V, so it shouldn't be connected directly; a 10 kΩ and 20 kΩ voltage divider brings it down to a safe level. Kits that run on 3.3 V with a 0–3 V output (such as the DFRobot V2) can be connected directly.
Why shouldn't a pH electrode be stored in distilled water?
Distilled water draws the ions out of the glass membrane and the KCl filling solution out of the electrode. Over time the electrode becomes slow, its slope drops and it stops holding a calibration. The right storage liquid is a 3 M KCl solution.
How long does a pH electrode last?
A hobby-grade lab electrode usually lasts 1–2 years with regular care. Electrodes that stay submerged permanently, or are used in dirty or very hot liquids, last less. If the response keeps getting slower and the voltage difference between pH 4 and 7 shrinks, it's time to replace it.
Does temperature affect pH readings?
Yes. The electrode's slope changes with temperature: a system calibrated at 25 °C reads a pH 4 sample at 10 °C as about 4.15. You can remove this error with a temperature sensor and the Nernst correction, but no correction removes the real change in the sample's pH with temperature.
Projects to build after reading this
See all projects
Measuring Light Intensity with an LDR
Want to see how bright your room is as a number? The LDR, a small part that reacts to light, measures the light around it, and the Arduino writes the value to your computer screen every half second.

Digital Voltmeter with LCD Display
Find out how much voltage a battery or power adapter gives with a meter you built yourself.

Smart Irrigation Station
A gardener that waters your plants on its own, so they don't dry out while you're on vacation.

Temperature Measurement with NTC and 2x16 Lcd Display
Measure the temperature around you with a sensor that looks like a tiny bead. This part, called an NTC, lets electricity through more easily as it warms up; the Arduino reads that change, turns it…

ESP32 Temperature Data Logger (DHT22 + DS1307 RTC + microSD + OLED)
Build a timed temperature and humidity logger with the ESP32! A DHT22 sensor reads temperature and humidity once a minute, and each reading is saved to a microSD card in CSV format with a real-time stamp from the DS1307 RTC module. The OLED display shows the latest temperature, humidity, timestamp and total number of records live, in large text.

ESP32 DHT22 Temperature and Humidity (with LCD Display)
This project measures ambient temperature and humidity using a DHT22 sensor and displays the data in real time on a 16x2 I2C LCD. Data is read from the sensor using the one-wire protocol via the Adafruit DHT library. This project serves as the foundation for IoT weather stations, smart home automation, or environmental monitoring systems in greenhouses. Communication with the DHT22 is established via the ESP32's GPIO 4 pin.

Using LCD Screen with I2C
Give your Arduino its own screen and make it write its first message. The two-line LCD screen hooks up with just a few wires; a greeting appears on the top line, then a second message on the bottom…
More articles
BlueGrays app
Build this circuit step by step on your phone
Wiring diagram, code and parts list on one screen. 500+ Arduino, ESP32, ESP8266, Pico, mBot and micro:bit projects ready to build.
Scan with your phone
Point your camera at the code and your phone's app store opens.
or open the store

