Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- # This sample code is used to test the pH meter V1.1.
- # Editor : Andrew Hoeveler
- # Ver : 1.1
- # Product: analog pH meter
- # SKU : SEN0161
- */
- #include <SoftwareSerial.h>
- #define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
- #define Offset 0.89 //deviation compensate
- #define LED 13
- #define samplingInterval 20
- #define printInterval 8000
- #define ArrayLength 40 //times of collection
- int pHArray[ArrayLength]; //Store the average value of the sensor feedback
- int pHArrayIndex=0;
- SoftwareSerial espSerial(11, 12); // RX, TX of Arduino Uno
- void setup()
- {
- pinMode(LED,OUTPUT);
- Serial.begin(9600); // Initialize the Arduino serial monitor
- espSerial.begin(9600); // Initialize the SoftwareSerial for ESP32
- Serial.println("Testing serial monitor: pH meter starting"); //Test the serial monitor
- }
- void loop()
- {
- // if (espSerial.available())
- // {
- // char data = espSerial.read(); // Read data from ESP32
- // Serial.print("Received data from ESP32: ");
- // Serial.println(data);
- // // Send response back to ESP32
- // espSerial.write("Response from Arduino");
- // }
- static unsigned long samplingTime = millis();
- static unsigned long printTime = millis();
- static float pHValue,voltage;
- if(millis()-samplingTime > samplingInterval)
- {
- pHArray[pHArrayIndex++]=analogRead(SensorPin);
- if(pHArrayIndex==ArrayLength)pHArrayIndex=0;
- voltage = avergearray(pHArray, ArrayLength)*5.0/1024;
- pHValue = 3.5*voltage+Offset;
- samplingTime=millis();
- }
- //Every 8000 milliseconds, print a numerical to both Serial Monitor and espSerial, convert the state of the LED indicator
- if(millis() - printTime > printInterval)
- {
- Serial.print("Voltage: ");
- Serial.print(voltage, 2);
- Serial.print(" pH value: ");
- Serial.println(pHValue, 2);
- espSerial.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 averaging!/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;
- }
- // Usage instructions:
- // 1. Clean the electrode with distilled water and gently dab dry with tissue paper, then put the pH electrode into the pH standard solution whose value is 4.00.
- // 2. Open the serial monitor of the Arduino IDE - you should see the pH values appear there.
- // wait about one minute to stabilize, then slowly adjust the gain potentiometer so that the readings stabilize at 4.00.
- // 3. Clean the electrode with distilled water and gently dab dry with tissue paper, then put the pH electrode into the pH standard solution whose value is 7.00.
- // 4. Wait about one minute to stabilize, then record the pH value.
- // 5. Compare with 7.00 - the difference should be changed into the "Offset" in the sample code.
- // For example if the pH value shown is 6.11 then the difference is 0.89 - and you should change the "# define Offset 0.00" into "# define Offset 0.89" in your program.
- // VALUES RECORDED:
- // reference 4.0 = 4.00 (with adjusted/calibrated potentiomter)
- // reference 7.0 = 6.11
Advertisement
Add Comment
Please, Sign In to add comment