Advertisement
andretafta

PH Sensor Arduino I2C

Jun 9th, 2024
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal_I2C.h>
  2.  
  3. LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  4.  
  5. int pH = A0;
  6. int buf [10];
  7.  
  8. float ph (float voltage) {
  9.   return 7 + ((2.5 - voltage) / 0.18) ;
  10. }
  11.  
  12. void setup () {
  13.   // put your setup code here, to run once:
  14.   pinMode (pH, INPUT) ;
  15.  
  16.   Serial.begin (115200) ;
  17.  
  18.   lcd.begin () ;
  19.   lcd.backlight () ;
  20.   lcd. setCursor (0, 0) ;
  21.   lcd. print ("Welcome") ;
  22.   delay (1000) ;
  23.   lcd.clear () ;
  24. }
  25.  
  26. void loop () {
  27.   // put your main code here, to run repeatedly:
  28.   lcd. setCursor (13,1) ;
  29.   lcd.print ("      ") ;
  30.  
  31.   lcd. setCursor (13, 2) ;
  32.   lcd.print ("      ") ;
  33.  
  34.   for (int i=0; i<10;i++)
  35.   {
  36.     buf[i]=analogRead(pH) ;
  37.     delay (10) ;
  38.   }
  39.  
  40.  
  41.   float avgValue=0;
  42.   for (int i=0;i<10;i++)
  43.   avgValue+=buf [i];
  44.  
  45.  
  46.   float pHVol=(float)avgValue*5.0/1024/10;
  47.   float phValue = -5.70 * pHVol + 21.34;
  48.  
  49.  
  50.   lcd. setCursor (0,0) ;
  51.   lcd. print ("pH Formula : ");
  52.   lcd. print (ph (pHVol) , 2) ;
  53.   lcd. setCursor (0,1) ;
  54.   lcd. print ("pH Cal   : ") ;
  55.   lcd. print (phValue) ;
  56.  
  57.   delay(1000);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement