ph监测传感器连接arduino,stm32,51单片机使用
一.传感器介绍
溶液的酸碱度(PH值)是溶液的一个重要特性。工业级PH变送器价格昂贵;市面上的PH测试笔是成熟产品,无法进行二次设计开发; PH复合电极输出mV级的电压信号,单片机无法直接进行识别处理,基于这些现状我们设计了这款PH传感器模块。该模块价格低廉、使用方便、测量精度高、可直接输出0~5V或0~3V模拟电压信号。
二.使用用
2.1
arduino接线使用及相关代码。
#define SensorPin A1 //pH meter Analog output to Arduino Analog Input 1
#define Offset 17.677 //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 = -5.8887*voltage+Offset;
if(pHValue<=0.0){pHValue=0.0;}
if(pHValue>14.0){pHValue=14.0;}
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;
}
2.2
51单片机
STC89+PCF8591-1602显示
2.3
stm32单片机
STM32+OLED+PH
51单片机和stm32用例代码百度网盘自取
链接: https://pan.baidu.com/s/1WohMwgf42Ut2y09Ih3Nr2Q?pwd=4ss6 提取码: 4ss6 复制这段内容后打开百度网盘手机App,操作更方便哦
作者:疯狂的大只因