element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
1 Meter of Pi
  • Challenges & Projects
  • Design Challenges
  • 1 Meter of Pi
  • More
  • Cancel
1 Meter of Pi
Blog Space Vegetables - #16 | PH reader
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: feiticeir0
  • Date Created: 7 Dec 2020 10:57 PM Date Created
  • Views 414 views
  • Likes 3 likes
  • Comments 1 comment
  • 1meterofpi
  • 1 meter of pi
  • ph sensor
  • 1 meter of pi - design challenge
  • space vegetables
  • ph reader
  • ph sensor arduino
  • 1meter of pi
Related
Recommended

Space Vegetables - #16 | PH reader

feiticeir0
feiticeir0
7 Dec 2020

image

 

Hi all ! Hope everyone is safe and sound !

 

This post is just to share the contraption I made to read the PH level of the water. Because the PH sensor cannot stay in water for long - like the TDS sensor - I've build a small contraption to read the PH level and display it in a TFT screen I had laying around.

Here's the schematics of the build:

 

 

image

 

The build is nothing fancy - a small wooden box with holes to the power button

 

imageimageimageimageimage

 

Code

 

To be able to use the PH sensor, it had to be calibrated. Using a PH 7.0 solution just for these purpose, I was able to calibrate it .

Note: The following code is not mine. I've taken it from  https://scidle.com/how-to-use-a-ph-sensor-with-arduino/ . It works very well. In the second listing, I've just altered the code to be used with the TFT screen.

Here's the code used for calibration.

/*
 # This sample code is used to test the pH meter V1.0.
 # Editor : YouYou
 # Ver    : 1.0
 # Product: analog pH meter
 # SKU    : SEN0161
*/
#define SensorPin A0            //pH meter Analog output to Arduino Analog Input 0
#define Offset -1.18            //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex=0;
void setup(void) {
    pinMode(LED,OUTPUT);
    Serial.begin(9600);
    Serial.println("pH meter experiment!");    //Test the serial monitor
}

void loop(void) {
    static unsigned long samplingTime = millis();
    static unsigned long printTime = millis();
    static float pHValue,voltage;
    if(millis()-samplingTime > samplingInterval) {
        pHArray[pHArrayIndex++]=analogRead(SensorPin);
        if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
            voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
        pHValue = 3.5*voltage+Offset;
        samplingTime=millis();
    }
    if(millis() - printTime > printInterval) {  //Every 800 milliseconds, print a numerical, convert the state of the LED indicator  
        Serial.print("Voltage:");
        Serial.print(voltage,2);
        Serial.print("    pH value: ");
        Serial.println(pHValue,2);
        digitalWrite(LED,digitalRead(LED)^1);
        printTime=millis();
    }
}

double avergearray(int* arr, int number) {
    int i;
    int max,min;
    double avg;
    long amount=0;
    if (number<=0) {
        Serial.println("Error number for the array to avraging!/n");
        return 0;
    }
    if (number<5) {   //less than 5, calculated directly statistics
        for(i=0;i<number;i++) {
            amount+=arr[i];
        }
        avg = amount/number;
        return avg;
    } else {
        if (arr[0]<arr[1]) {
            min = arr[0];max=arr[1];
        }
        else {
            min=arr[1];max=arr[0];
        }
        for(i=2;i<number;i++) {
            if (arr[i]<min) {
                amount+=min;        //arr<min
                min=arr[i];
            } else {
                if (arr[i]>max) {
                    amount+=max;    //arr>max
                    max=arr[i];
                } else {
                    amount+=arr[i]; //min<=arr<=max
                }
            }//if
        }//for
        avg = (double)amount/(number-2);
    } //if
    return avg;
}

 

Basically what the codes does is takes 40 samples in a question of milliseconds and shows the average value.

 

Now here's the code adapted for the LCD and with the offset necessary after calibration.

/*
 # Measure PH level
 # Editor : Feiticeir0
 # Ver    : 1.0
 # Product: analog pH meter

*/
#include <SPI.h>
#include <TFT.h>

//TFT PINS
#define cs 10
#define dc 9
#define reset 8

TFT tftScreen = TFT (cs, dc, reset);

#define SensorPin A0            //pH meter Analog output to Arduino Analog Input 0
#define Offset -1.18            //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection

int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex=0;

char vprint[4];
char pprint[4];

void setup(void) {
    pinMode(LED,OUTPUT);
    Serial.begin(9600);

    /* dont have more 5v outputs.. TEMP */
    pinMode (7, OUTPUT);
    digitalWrite(7,HIGH);
    tftScreen.begin();
    tftScreen.background(0,0,0); // clear the screen
  
    tftScreen.setTextSize(1);
    //line split voltage and PH
    tftScreen.stroke(255,255,255);
    //tftScreen.line(0,64,160,64);
    tftScreen.fill(255,255,255);
    tftScreen.rect(0,63,160,3);
    tftScreen.text("PH reading ",2,0);
    tftScreen.text("Voltage ",2,67);
    tftScreen.setTextSize(3);
}
void loop(void) {
    static unsigned long samplingTime = millis();
    static unsigned long printTime = millis();
    static float pHValue,voltage;
    if (millis() - samplingTime > samplingInterval) {
        pHArray[pHArrayIndex++]=analogRead(SensorPin);
        if (pHArrayIndex==ArrayLenth)
            pHArrayIndex = 0;
        voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
        pHValue = 3.5*voltage+Offset;
        samplingTime=millis();
    }
    if (millis() - printTime > printInterval) {   //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
        String volts = String(voltage);
        String ph = String(pHValue);
        volts.toCharArray(vprint,4);
        ph.toCharArray(pprint,4);
        tftScreen.stroke(255,255,0);
        tftScreen.text(vprint, 60,84);
        tftScreen.text(pprint, 60,20);
        delay(500);
        //set to black to erase
        tftScreen.stroke(0,0,0);
        tftScreen.text(vprint, 60, 84);
        tftScreen.text(pprint, 60,20);
        //digitalWrite(LED,digitalRead(LED)^1);
        printTime = millis();
    }
}


double avergearray(int* arr, int number){
    int i;
    int max,min;
    double avg;
    long amount=0;
    if (number<=0) {
        Serial.println("Error number for the array to avraging!/n");
        return 0;
    }
    if (number<5) {   //less than 5, calculated directly statistics
        for(i=0;i<number;i++) {
            amount+=arr[i];
        }  
    avg = amount/number;
    return avg;
    } else {
        if(arr[0]<arr[1]) {
            min = arr[0];max=arr[1];
        }
        else {
            min=arr[1];max=arr[0];
        }
        for(i=2;i<number;i++) {
            if(arr[i]<min) {
                amount+=min;        //arr<min
                min=arr[i];
            } else {
                if (arr[i]>max) {
                    amount+=max;    //arr>max
                    max=arr[i];
                } else {
                    amount+=arr[i]; //min<=arr<=max
                }
            } //if
        } //for
        avg = (double)amount/(number-2);
    }//if
    return avg;
}

 

With this, I'm able to use the sensor anytime !

 

Happy coding.

  • Sign in to reply

Top Comments

  • skruglewicz
    skruglewicz over 5 years ago +1
    nice blog post feiticeir0 As you and I have spoke on using a PH sensor for the design challenge. I don't think I'll have enough time to implement it in my Design, but I will refer back to this blog in…
  • skruglewicz
    skruglewicz over 5 years ago

    nice blog post feiticeir0

    As you and I have spoke on using a PH sensor for the design challenge. I don't think I'll have enough time to implement it in my Design, but I will refer back to this blog in the next iteration of my project

    Thanks

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube