Overview
In this tutorial, we will design our own DIY IoT Based Water pH Meter using pH Sensor & ESP32 WiFi Module. In some earlier tutorial, we interfaced pH Sensor with Arduino to make a portable ph Meter. The pH reading data was displayed on OLED Screen.
This is a completely different tutorial as we will be using a better and advanced pH Sensor. The pH Sensor used here is an analog type pH Sensor that gives a linear pH reading in 0pH to 14pH range. Instead of displaying the data on an OLED screen, we will send the data to ThingSpeak Server. The ThingSpeak provides instant visualizations of data posted by our device, i.e. ESP32. Since ESP32 is a 12-bit controller with built-in ADC, so it can measure data more precisely compared to Arduino which has a 10-bit ADC.
It incorporates a high-resolution pH probe that samples liquid pH at user-defined intervals and transmits the result to remote servers. The pH probe comes with a 5 long cable and can be dipped in any solution. The response time is lesser than a minute. We can incorporate this IoT Based pH Meter in Aquariums, Hydroponics, laboratories, etc. So let us learn how we can interface pH Sensor with ESP32 & make our own pH meter. The tutorial also covers the pH Sensor Calibration method.
Bill of Materials
Following are the materials required for making the IoT Based pH Meter. You can purchase all the components online using the following links.
S.N. | Components Name | Quantity | Purchase Links |
---|---|---|---|
1 | ESP32 WiFi Module | 1 | Amazon | AliExpress |
2 | Ph Sensor Kit | 1 | graylogix.in |
4 | 9V DC Supply | 1 | Amazon | AliExpress |
5 | Connecting Wires | 10 | Amazon | AliExpress |
6 | Breadboard | 1 | Amazon | AliExpress |
PH Sensor KIT for Water
This Analog pH Meter Kit with the industrial real-time electrode is specially designed for Arduino, ESP8266, ESP32 & other Microcontrollers. It uses an industry electrode and has a built-in simple, convenient & practical connection. It has a long life (up to 1 year), which makes it very suitable for long-term online monitoring.
The signal connector board has an LED that works as the Power Indicator. The board also has a BNC-type connector and a PH sensor interface. To use it, just connect the pH sensor with a BNC connector, then plug the PH interface into the analog input port of any microcontroller. If programmed, you will get the pH value easily. You can also use a multimeter to measure the output voltage.
This industrial pH electrode is made of a sensitive glass membrane with low impedance. It can be used in a variety of PH measurements with fast response and excellent thermal stability. It has good reproducibility, is difficult to hydrolysis, and can eliminate basic alkali error.
In 0pH to 14pH range, the output voltage is linear. The reference system which consists of the Ag/AgCl gel electrolyte salt bridge has a stable half-cell potential and excellent anti-pollution performance. The ring PTFE membrane is not easy to be clogged, so the electrode is suitable for long-term online detection.
PH Sensor Specifications
- Module Power: DC 9.00V 1A
- Measuring Range: 0-14PH
- Accuracy: ± 0.1pH (24 )
- Response Time: 1min
- Industry pH Electrode with BNC Connector
- Power Indicator LED
- Output: Analog values in the range of (0.5V to 3V)
- Alkali Error: 0.2PH
- Internal Resistance: 250MO
Calibration Instruction
This instrument is calibrated for the PH sensor provided along with this Kit. And may not give accurate results if the PH sensor probe is changed. Hence calibration is required if the PH sensor Probe is changed. The results are directly proportional to the temperature of the solution.
The sensor can be calibrated at values: PH4 = 1.5V, PH7 = 2.0V & PH9 = 2.5V. To use this sensor, lace the PH Sensor probe vertically and keep it for a long time before using it. It will give excellent results if the sensor probe is used multiple times in the period of 2 to 3 days.
Rinse the sensor prove tube in deionized water and dab it with the tissue paper before immersing it in the test solution. Will take approximately 30 to 40 seconds to stabilize the results. Once the reading is noted down and before placing the probe in the storage solution, again rinse the probe tip in the deionized water, dab it with the dry tissue paper and then place the storage cap.
Precautions
PH sensor probe Tube tip is very sensitive and should not be touched with hands and should not be placed on the ground. The bulb type tip is very fragile and should be kept in the storage solution if not used.
Interfacing & Testing PH Sensor with ESP32
Before proceeding for IoT pH Meter, let us do a basic interfacing and testing of pH sensor with ESP32 WiFi Module. This is a simple connection diagram.
To power the pH Sensor use an external 9V battery or a 9V DC Supply. Connect the output pin of the pH Sensor signal board to VP of ESP32 which can be used as an A0 pin. The output of the sensor ranges between 0.5V to 3V so the sensor can be used with analog pins of ESP32.
Basic Test Code
The pH sensor is an analog sensor. So we will be converting the analog output voltage into a pH value. Here is a simple ESP32 & pH Sensor Code.
Copy the code and paste it on the editor of Arduino IDE. Select the ESP32 from Board Manager and also select the COM port. Now you can upload the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
const int potPin=A0; float ph; float Value=0; void setup() { // put your setup code here, to run once: Serial.begin(115200); pinMode(potPin,INPUT); delay(1000); } void loop(){ Value= analogRead(potPin); Serial.print(Value); Serial.print(" | "); float voltage=Value*(3.3/4095.0); ph=(3.3*voltage); Serial.println(ph); delay(500); } |
After uploading the code, open the Serial Monitor, the sensor values must be visible.
We have tested the lemon juice whose Ph came around 2.8 ~ 3.0
We also tested a basic detergent solution the Ph for this was greater than 8.
We also tested Ph of Milk which was around 5.
There is no need for calibration of the sensor if you haven’t changed the probe which was already provided with the Ph sensor Kit.
Otherwise, you can do that by tuning the 5k resistor on the Ph module as highlighted in the image below.
IoT Based Water pH Meter using pH Sensor & ESP32
Now let us write another code to build an IoT Based pH Meter. Using this code we can monitor the pH value from any part of the world. We will be using the Thingspeak server for online monitoring of the pH data.
ThingSpeak provides very good tool for IoT based projects. By using the ThingSpeak site, we can monitor our data and control our system over the Internet, using the Channels and web pages provided by ThingSpeak. So first you need to sign up for ThingSpeak. So visit https://thingspeak.com and create an account.
Then create a new channel and set up a widget for pH Value.
Then create the API keys. This key is required for programming modifications and setting your data.
Source Code/Program
The following is the code for IoT Based pH Meter using pH Sensor & ESP32.
1 2 3 |
String apiKey = "*************"; const char *ssid = "*************"; const char *password = "*************"; |
You need to update the API key by pasting the API Key copied previously from Thingspeak Dashboard. Also update the Wi-Fi credentials, Wi-Fi name in SSID, and its password in password variable.
Now copy the following code and upload it to the ESP32 Board.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <WiFi.h> const int potPin=A0; float ph; float Value=0; String apiKey = "*************"; // Enter your Write API key from ThingSpeak const char *ssid = "*************"; // replace with your wifi ssid and wpa2 key const char *password = "*************"; const char* server = "api.thingspeak.com";// don't change this WiFiClient client; void setup() { // put your setup code here, to run once: Serial.begin(115200); pinMode(potPin,INPUT); delay(1000); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(2000); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // put your main code here, to run repeatedly: Value= analogRead(potPin); Serial.print(Value); Serial.print(" | "); float voltage=Value*(3.3/4095.0); ph=(3.3*voltage); Serial.println(ph); delay(500); if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com { String postStr = apiKey; postStr += "&field1="; postStr += String(ph); postStr += "\r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); } } |
Once the code is uploaded, the ESP32 will try connecting to the WiFi Network. Every 15 seconds the data will be uploaded to the Thinkspeak server.
We can view the uploaded values in the private view section on the Thinkspeak dashboard as in the image.
Video Tutorial & Guide
Incase if you are not satisfied with the Analog Ph Sensor, then you can use Atlas Scientific Ph Sensor which has better accuracy & result due to I2C & UART interface.
1 Comment
How to reduced that 15 seconds count at the end? Where should I change the code to obtain the sensed values much sooner?