domingo, 24 de janeiro de 2016

Sensor de PH - Arquivo 006

Referência:
http://dfrobot.com/wiki/index.php/PH_meter(SKU:_SEN0161)
https://code.google.com/p/phduino/

Lista de Materiais:
IDE Arduino 1.0.6
1x  Board Arduino UNO
1x  Protoboard 830 furos
1x  Cabo USB-A male to USB-B male 
1x  Conj. de fios jumper p/ protoboard
1x  Kit de PH - PH Meter SEN0161 - DFRobot

Montagem:





Firmware 01:
#define SensorPin 0                               //pH meter Analog output to Arduino Analog Input 0

unsigned long int avgValue;                 //Store the average value of the sensor feedback
float b;
int buf[10],temp;
//*******************************************************************************
void setup(){
  pinMode(13,OUTPUT);  
  Serial.begin(9600);  
  Serial.println("Ready");                     //Test the serial monitor
}
//*******************************************************************************
void loop(){
  for(int i=0;i<10;i++){                        //Get 10 sample value from the sensor for smooth the value
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++){                         //sort the analog from small to large
    for(int j=i+1;j<10;j++){
      if(buf[i]>buf[j]){
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++) avgValue+=buf[i];                       //take the average value of 6 center sampl       
  float phValue=(float)avgValue*5.0/1024/6;               //convert the analog into millivolt
  phValue=3.5*phValue;                                               //convert the millivolt into pH value
  Serial.print("    pH:");  
  Serial.print(phValue,2);
  Serial.println(" ");
  digitalWrite(13, HIGH);       
  delay(800);
  digitalWrite(13, LOW); 
}

Firmware 02:
#define SensorPin            A0       //pH meter Analog output to Arduino Analog Input 0
#define Offset                  0.00     //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();
  }
  //Every 800 milliseconds, print a numerical, 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);
        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);
  }
  return avg;
}

Download: não necessário

Um comentário:

  1. Muito bom o tutorial Mori. Poderia aprofundar como utilizar o sensor de pH em outros tutoriais?

    ResponderExcluir