hoeveler

DFRobot v1.1 pH meter

Jun 26th, 2023
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.86 KB | Source Code | 0 0
  1. /*
  2.  # This sample code is used to test the pH meter V1.1.
  3.  # Editor : Andrew Hoeveler
  4.  # Ver    : 1.1
  5.  # Product: analog pH meter
  6.  # SKU    : SEN0161
  7. */
  8. #include <SoftwareSerial.h>
  9.  
  10. #define SensorPin A0            //pH meter Analog output to Arduino Analog Input 0
  11. #define Offset 0.89             //deviation compensate
  12. #define LED 13
  13. #define samplingInterval 20
  14. #define printInterval 8000
  15. #define ArrayLength  40         //times of collection
  16.  
  17. int pHArray[ArrayLength];       //Store the average value of the sensor feedback
  18. int pHArrayIndex=0;
  19.  
  20.  
  21. SoftwareSerial espSerial(11, 12);  // RX, TX of Arduino Uno
  22.  
  23. void setup()
  24. {
  25.   pinMode(LED,OUTPUT);
  26.   Serial.begin(9600);  // Initialize the Arduino serial monitor
  27.   espSerial.begin(9600);  // Initialize the SoftwareSerial for ESP32
  28.   Serial.println("Testing serial monitor: pH meter starting");    //Test the serial monitor
  29. }
  30.  
  31. void loop()
  32. {
  33.   // if (espSerial.available())
  34.   // {
  35.   //   char data = espSerial.read();  // Read data from ESP32
  36.   //   Serial.print("Received data from ESP32: ");
  37.   //   Serial.println(data);
  38.  
  39.   //   // Send response back to ESP32
  40.   //   espSerial.write("Response from Arduino");
  41.   // }
  42.  
  43.   static unsigned long samplingTime = millis();
  44.   static unsigned long printTime = millis();
  45.   static float pHValue,voltage;
  46.  
  47.   if(millis()-samplingTime > samplingInterval)
  48.   {
  49.       pHArray[pHArrayIndex++]=analogRead(SensorPin);
  50.       if(pHArrayIndex==ArrayLength)pHArrayIndex=0;
  51.       voltage = avergearray(pHArray, ArrayLength)*5.0/1024;
  52.       pHValue = 3.5*voltage+Offset;
  53.       samplingTime=millis();
  54.   }
  55.  
  56. //Every 8000 milliseconds, print a numerical to both Serial Monitor and espSerial, convert the state of the LED indicator
  57.   if(millis() - printTime > printInterval)
  58.   {
  59.   Serial.print("Voltage: ");
  60.   Serial.print(voltage, 2);
  61.   Serial.print("    pH value: ");
  62.   Serial.println(pHValue, 2);
  63.  
  64.   espSerial.println(pHValue, 2);
  65.  
  66.   digitalWrite(LED, digitalRead(LED) ^ 1);
  67.   printTime = millis();
  68.   }
  69. }
  70.  
  71. double avergearray(int* arr, int number)
  72. {
  73.   int i;
  74.   int max,min;
  75.   double avg;
  76.   long amount=0;
  77.   if(number<=0){
  78.     Serial.println("Error number for the array to averaging!/n");
  79.     return 0;
  80.   }
  81.   if(number<5){   //less than 5, calculated directly statistics
  82.     for(i=0;i<number;i++){
  83.       amount+=arr[i];
  84.     }
  85.     avg = amount/number;
  86.     return avg;
  87.   }else{
  88.     if(arr[0]<arr[1]){
  89.       min = arr[0];max=arr[1];
  90.     }
  91.     else{
  92.       min=arr[1];max=arr[0];
  93.     }
  94.     for(i=2;i<number;i++){
  95.       if(arr[i]<min){
  96.         amount+=min;        // arr < min
  97.         min=arr[i];
  98.       }else {
  99.         if(arr[i]>max){
  100.           amount+=max;    // arr > max
  101.           max=arr[i];
  102.         }else{
  103.           amount+=arr[i]; // min <= arr <= max
  104.         }
  105.       }//if
  106.     }//for
  107.     avg = (double)amount/(number-2);
  108.   }//if
  109.   return avg;
  110. }
  111.  
  112. // Usage instructions:
  113. // 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.
  114. // 2. Open the serial monitor of the Arduino IDE - you should see the pH values appear there.
  115. //    wait about one minute to stabilize, then slowly adjust the gain potentiometer so that the readings stabilize at 4.00.
  116. // 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.
  117. // 4. Wait about one minute to stabilize, then record the pH value.
  118. // 5. Compare with 7.00 - the difference should be changed into the "Offset" in the sample code.
  119. //    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.
  120.  
  121. // VALUES RECORDED:
  122. // reference 4.0 = 4.00 (with adjusted/calibrated potentiomter)
  123. // reference 7.0 = 6.11
  124.  
Advertisement
Add Comment
Please, Sign In to add comment