空气传感器问题

2015-05-172.2万
AI 快速预览详细 收起
本文讨论了使用Sharp GP2Y1010AU0F空气质量传感器时遇到的数据异常问题。作者在多传感器并联时发现只有空气质量传感器的数据不正常,而其他传感器(如温湿度、大气压传感器)均正常工作。文章提供了详细的代码示例和调试步骤,包括独立运行空气质量程序验证传感器功能,并逐步排查多传感器并联时的问题。通过这些步骤,用户可以快速定位并解决传感器数据异常问题。
大家有谁玩过Sharp GP2Y1010AU0F 空气质量传感器呢?我现在遇到一个问题,我用面包板把紫外线,空气质量,温湿度,大气压传感器和uno全部连在了一起,想同时得到当前环境下的这些数据,现在问题来了,其他传感器都正常,就是空气传感得到的这行数据显示不正常,只有把独立的空气质量程序写进去,数据才能正常。。。
空气传感器问题图1

下面是代码,温湿度和大气压都用的I2C总线,我把他们并联起来了,没有问题。
#include <Wire.h>

const int ADDR =0x40;
int X0,X1,Y0,Y1,Y2,Y3;
double X,Y,X_out,Y_out1,Y_out2;

#define BMP180ADD 0xEE>>1  // I2C address of BMP180  
                         //write is (0xEE)     read is (0xEF)      
//yixiashikongqizhiliang chuanganqideshengmign
int measurePin = 0; //Connect dust sensor to Arduino A0 pin
//int ledPower = 2;   //Connect 3 led driver pins of dust sensor to Arduino D2

int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;

float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
//kongqich
unsigned char OSS;                           
/**********************MSB      LSB******/
int ac1;           // 0xAA     0xAB
int ac2;           // 0xAC     0xAD
int ac3;           // 0xAE     0xAE
unsigned int ac4;  // 0xB0     0xB1
unsigned int ac5;  // 0xB2     0xB3
unsigned int ac6;  // 0xB4     0xB5
int b1;            // 0xB6     0xB7
int b2;            // 0xB8     0xB9
int mb;            // 0xBA     0xBB
int mc;            // 0xBC     0xBD
int md;            // 0xBE     0xBF
float temperature;  
double pressure;   
double pressure2;
long b5;         
double altitude;  



void setup()
{
  Serial.begin(9600);
  Wire.begin();
  OSS = 2;  // Oversampling Setting           0: single    1: 2 times    2: 4 times   3: 8 times
  BMP180start();

  delay(100);  
   Wire.beginTransmission(ADDR);
   Wire.endTransmission();

  pinMode(13,OUTPUT);

}

void loop()
{
  //ziwaixian
  int sensorValue;
  int analogValue = analogRead(1);//connect UV sensors to Analog 0   
  if (analogValue<20)
  {
    sensorValue = 0;
  }
  else
  {
    sensorValue = 0.05*analogValue-1;
  }
  Serial.print("the UV Value is  ");
  Serial.println(sensorValue);//print the value to serial
  //ziwaixian

  //wenshidu
  Wire.beginTransmission(ADDR);                    
   Wire.write(0xE3);                               //发送读温度命令
   Wire.endTransmission();

   Serial.print("Temp");Serial.print("\t");Serial.println("RH");

   /**读取温度数据**/
   digitalWrite(13,HIGH);                          //LED亮开始读数据
   Wire.requestFrom(ADDR,2);                       //回传数据

   if(Wire.available()<=2);
   {
     X0 = Wire.read();
     X1 = Wire.read();
     X0 = X0<<8;
     X_out = X0+X1;
   }

   /**计算并显示温度**/
   X=(175.72*X_out)/65536;                        
   X=X-46.85;
   Serial.print(X);Serial.print("C");Serial.print("\t");

   /**发送湿度测量命令**/       
   Wire.beginTransmission(ADDR);                     
   Wire.write(0xE5);
   Wire.endTransmission();

   /**读取湿度数据**/       
   Wire.requestFrom(ADDR,2);
   if(Wire.available()<=2);
   {
     Y0 = Wire.read();Y2=Y0/100; Y0=Y0%100;
     Y1 = Wire.read();
     Y_out1 = Y2*25600;
     Y_out2 = Y0*256+Y1;
   }

   /**计算并显示湿度**/
   Y_out1 = (125*Y_out1)/65536;                     
   Y_out2 = (125*Y_out2)/65536;
   Y = Y_out1+Y_out2;
   Y=Y-6;
   Serial.print(Y);Serial.println("%");

   digitalWrite(13,LOW);
   //wenshidu

  //kongqi
  delayMicroseconds(samplingTime);

  voMeasured = analogRead(measurePin); // read the dust value

  delayMicroseconds(deltaTime);
  //digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);

  // 0 - 5V mapped to 0 - 1023 integer values
  // recover voltage
  calcVoltage = voMeasured * (5.0 / 1024.0);

  // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
  // Chris Nafis (c) 2012
  dustDensity = 0.17 * calcVoltage - 0.1;

  Serial.print("Raw Signal Value (0-1023): ");
  Serial.print(voMeasured);

  Serial.print(" - Voltage: ");
  Serial.print(calcVoltage);

  Serial.print(" - Dust Density: ");
  Serial.println(dustDensity); // unit: mg/m3
  //kongqi

  //daqiya
  calculate();
  show();
  //daqiya
  delay(5000);
}

/** calculate centure **/
void calculate()
{
  temperature = bmp180GetTemperature(bmp180ReadUT());
  temperature = temperature*0.1;
  pressure = bmp180GetPressure(bmp180ReadUP());
  pressure2 = pressure/101325;
  pressure2 = pow(pressure2,0.19029496);
  altitude = 44330*(1-pressure2);                            //altitude = 44330*(1-(pressure/101325)^0.19029496);
}

/** print reslut **/
void show()
{
  Serial.print("Temperature: ");
  Serial.print(temperature, 1);                            //10 hexadecimal
  Serial.println(" C");
  Serial.print("Pressure: ");
  Serial.print(pressure, 0);                               //10 hexadecimal
  Serial.println(" Pa");
  Serial.print("altitude:");
  Serial.print(altitude);
  Serial.println("m");
}

/**BMP180 satrt program**/
void BMP180start()
{                     /*MSB*/
  ac1 = bmp180ReadDate(0xAA);                      //get full data
  ac2 = bmp180ReadDate(0xAC);  
  ac3 = bmp180ReadDate(0xAE);  
  ac4 = bmp180ReadDate(0xB0);  
  ac5 = bmp180ReadDate(0xB2);  
  ac6 = bmp180ReadDate(0xB4);  
  b1  = bmp180ReadDate(0xB6);  
  b2  = bmp180ReadDate(0xB8);  
  mb  = bmp180ReadDate(0xBA);  
  mc  = bmp180ReadDate(0xBC);  
  md  = bmp180ReadDate(0xBE);
}

/***BMP180 temperature Calculate***/
short bmp180GetTemperature(unsigned int ut)
{
  long x1, x2;
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;  //x1=((ut-ac6)*ac5)/(2^15)
  x2 = ((long)mc << 11)/(x1 + md);                //x2=(mc*2^11)/(x1+md)
  b5 = x1 + x2;                                   //b5=x1+x2
  return ((b5 + 8)>>4);                           //t=(b5+8)/(2^4)
}

/***BMP180 pressure Calculate***/

long bmp180GetPressure(unsigned long up)
{
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;

  b6 = b5 - 4000;

  x1 = (b2 * (b6 * b6)>>12)>>11;
  x2 = (ac2 * b6)>>11;
  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;

  x1 = (ac3 * b6)>>13;
  x2 = (b1 * ((b6 * b6)>>12))>>16;
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;

  b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
  else
    p = (b7/b4)<<1;

  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;

  return p;
}


/*** Read 1 bytes from the BMP180  ***/

int bmp180Read(unsigned char address)
{
  unsigned char data;

  Wire.beginTransmission(BMP180ADD);
  Wire.write(address);
  Wire.endTransmission();

  Wire.requestFrom(BMP180ADD, 1);
  while(!Wire.available());

  return Wire.read();
}

/*** Read 2 bytes from the BMP180 ***/
int bmp180ReadDate(unsigned char address)
{
  unsigned char msb, lsb;
  Wire.beginTransmission(BMP180ADD);
  Wire.write(address);
  Wire.endTransmission();
  Wire.requestFrom(BMP180ADD, 2);
  while(Wire.available()<2);
  msb = Wire.read();
  lsb = Wire.read();
  return (int) msb<<8 | lsb;
}

/*** read uncompensated temperature value ***/
unsigned int bmp180ReadUT()
{
  unsigned int ut;
  Wire.beginTransmission(BMP180ADD);
  Wire.write(0xF4);                       // Write 0x2E into Register 0xF4
  Wire.write(0x2E);                       // This requests a temperature reading
  Wire.endTransmission();  
  delay(5);                               // Wait at least 4.5ms
  ut = bmp180ReadDate(0xF6);               // read MSB from 0xF6 read LSB from (16 bit)
  return ut;
}

/*** Read uncompensated pressure value from BMP180 ***/
unsigned long bmp180ReadUP()
{
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;

  Wire.beginTransmission(BMP180ADD);
  Wire.write(0xF4);                        // Write 0x34+(OSS<<6) into register 0xF4
  Wire.write(0x34 + (OSS<<6));             // 0x34+oss*64
  Wire.endTransmission();
  delay(2 + (3<<OSS));                     // Wait for conversion, delay time dependent on OSS

  Wire.beginTransmission(BMP180ADD);
  Wire.write(0xF6);                        // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  Wire.endTransmission();

  Wire.requestFrom(BMP180ADD, 3);
  while(Wire.available() < 3);             // Wait for data to become available
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);//16 to 19 bit
  return up;
}

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(6)
fbygg
fbygg
作者
这样啊,好的,我没课的时候试一下,谢谢
Cain
Cain
之前有遇到过一次是因为模拟口电压跳变周围会有干扰,只留一个模拟传感器试试呢,如果程序无误,可以加个滤波或者像之前那老师说的调整间隔
微笑的rockets
微笑的rockets回复fbygg
简单来说,应该看下metro库的应用,能够学会用metro库,基本上可以说进入Arduino的高级基础了。
关于metro库,请在论坛里搜索相关教程吧。
因为我也不是很熟悉的。
希望有更多的人来帮你。
fbygg
fbygg
作者
回复微笑的rockets
Arduino高级应用应该从哪里学起呢?
共6条回复,已加载2条,点击查看更多共6条回复,已加载全部
hnyzcj
hnyzcj
或者做个中断。
qq815076543322
qq815076543322回复fbygg
就是回复你 调整时间的这班组
fbygg
fbygg
作者
回复hnyzcj
好的我试一下,谢谢你:)
hnyzcj
hnyzcj
调整下时间间隔。
qq815076543322
qq815076543322
太高端 这事得陈老师来咯@@丄帝De咗臂
qq815076543322
qq815076543322回复丄帝De咗臂
哈哈 好的
qq815076543322
qq815076543322回复丄帝De咗臂
哈哈 好的
共6条回复,已加载2条,点击查看更多共6条回复,已加载全部
丄帝De咗臂
丄帝De咗臂
没玩过
- 没有更多了 -