This tutorial will teach us how to use a pH sensor and LCD display with Arduino UNO to measure the pH of a liquid solution, and also make the value shows on the screen.
The pH scale is used to measure the acidity and basicity of a liquid. It can have readings ranging from 1-to 14 where 1 shows the most acidic liquid and 14 shows the most basic liquid. 7 pH is for neutral substances that are neither acidic nor basic. For drinking water, the U.S. Environmental Protection Agency recommends that municipal drinking water suppliers keep their water supply at a pH of 6.5 to 8.5.
Is the water coming from your tap really healthy to drink? After this tutorial, you can know the pH value of whatever you are drinking, and whether your tap water is safe!
Required components
- Arduino Uno
- LCD1602 Module (comes in the Arduino kit)
- Teyleten Robot pH sensor
- 10k ohm resistor (comes in the Arduino kit)
- Connecting wires
- Breadboard
How does it work?
- Teyleten Robot pH sensor
This analog pH sensor is designed to measure the pH value of a solution and show the acidity or alkalinity of the substance. It is commonly used in various applications such as agriculture, wastewater treatment, industries, environmental monitoring, etc. The module has an on-board voltage regulator chip that supports the wide voltage supply of 3.3-5.5V DC, which is compatible with 5V and 3.3V of any control board like Arduino. The output signal is being filtered by hardware low jitter.
The sensor consists of two parts: Signal Conversion Module and pH electrode (as shown below).
The pH Electrode looks like a rod usually made of a glass material having a tip called “Glass membrane”. This membrane is filled with a buffer solution of known pH (typically pH = 7). This electrode design ensures an environment with the constant binding of H+ ions on the inside of the glass membrane. When the probe is dipped into the solution to be tested, hydrogen ions in the test solution start exchanging with other positively charged ions on the glass membrane, which creates an electrochemical potential across the membrane which is fed to the Signal Conversion Module. The signal conversion module read the voltage, and then in the code, it will be converted to pH value.
- LCD display
We connect the LCD display with the pH sensor using a breadboard, we connect a potentiometer to pin VO to adjust the contrast of the LCD screen.
Circuit/wiring
Here, we recommend you connect your LCD display and potentiometer to Arduino UNO first then connect the Signal Conversion Module, after calibration (which will be covered in the following content) you could connect the pH Electrode.
Code
It is really crucial to calibrate the sensor before we actually use it to measure the test solution. First you should make sure the pH value is in the correct range, for this step, you will need a wire to short the external part and the center of the probe connector on the Signal Conversion Module. This causes a 2.5 volts tension on the Po analog output pin. Because a pH of 7 means 2.5 Volts, so if the output value in this step is not 2.50, then you can set it to 2.50 using the trimmer.
- pH Sensor Calibration Arduino Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int pH_Value; float Voltage; void setup() { Serial.begin(9600); pinMode(pH_Value, INPUT); } void loop() { pH_Value = analogRead(A0); Voltage = pH_Value * (5.0 / 1023.0); Serial.println(Voltage); delay(500); } |
The pH Sensor is almost calibrated! Next, you should run the code below to test the solution in the small container of the pH Electrode, since that solution is the distilled water, you should have the output pH value around 7.0, so if you feel the value is still not accurate enough, you could adjust it in the calibration_value.
Now the the pH Sensor is calibrated perfectly under your standard!
- The complete code to measure and display the pH value:
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 63 64 |
/* This code is adapted from https://circuitdigest.com/microcontroller-projects/arduino-ph-meter and ELEGOO Lessons 15 */ #include <LiquidCrystal.h> // load the library for LCD display #define PH_PIN A0 // ph input //pins on LCD BS E D4 D5 D6 D7 LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // identify each pin on LCD display connected to according pin on Arduino float calibration_value = 21.34 +0.2 ; // adjust the calibration value based on the pH of distilled water int phval = 0; // set the initial ph value as 0 unsigned long int avgval; int buffer_arr[10],temp; // define an array for later data smoothing int delaytime = 3000; void setup() { lcd.begin(16, 2); // set the LCD display as 16 columns and 2 rows Serial.begin(9600); // the serial monitor setting } void loop() { //read 10 sample analog values and store them in an array to smooth the output value. for(int i=0;i<10;i++) { buffer_arr[i]=analogRead(PH_PIN); delay(30); } //sort the Analog values received in ascending order, because we need to calculate the running average of samples later for(int i=0;i<9;i++) { for(int j=i+1;j<10;j++) { if(buffer_arr[i]>buffer_arr[j]) { temp=buffer_arr[i]; buffer_arr[i]=buffer_arr[j]; buffer_arr[j]=temp; } } } //calculate the average of a 6 centre sample Analog values avgval=0; for(int i=2;i<8;i++) avgval+=buffer_arr[i]; //converted the average analog value into actual pH value float volt=(float)avgval*5.0/1024/6; float ph_act = -5.70 * volt + calibration_value; // print the pH value in serial monitor: Serial.println("pH Val: "); Serial.print(ph_act); // and printed on an LCD display: // set the first row starts from column 4 lcd.setCursor(4, 0); lcd.print("PH Val:"); // set the second row starts from column 6 lcd.setCursor(6, 1); lcd.print(ph_act); delay(delaytime); // read the pH value every 3 seconds } |
You now can measure the pH of any solution of your interest!
References:
https://circuitdigest.com/microcontroller-projects/arduino-ph-meter
https://circuitdigest.com/microcontroller-projects/arduino-ph-meter