使用的gp2y1010f,。如果不接1602在端口监视器能正确看到数值...
2016-03-279336
AI 快速预览详细
本文讨论了使用GP2Y1010F粉尘传感器时遇到的一个常见问题:当传感器连接到1602 LCD后,数值显示不正确,一直为-0.1。文章详细解析了问题的原因,并提供了具体的解决方案。通过调整代码中的延迟时间和LED控制逻辑,可以解决这个问题。最终,用户能够确保传感器与LCD正确连接并显示正确的粉尘密度值。
|
使用的gp2y1010f,全是DF的套件。如果不接LCD在端口监视器能正确看到数值,但是一接上1602就不行了。一直是-0.1. #include <Wire.h> #include <LiquidCrystal_I2C.h> int measurePin = 0; // 连接模拟口0 int ledPower = 2; // 连接数字口2 int samplingTime = 280; int deltaTime = 40; int sleepTime = 9680; float voMeasured = 0; float calcVoltage = 0; float dustDensity = 0; LiquidCrystal_I2C lcd(0x20,16,2); //设置LCD的地址为0x20,每行16个字符,共2行 void setup(){ Serial.begin(9600); pinMode(ledPower,OUTPUT); lcd.init(); lcd.backlight(); } void loop(){ digitalWrite(ledPower,LOW); //开启内部LED delayMicroseconds(samplingTime); // 开启LED后的280us的等待时间 voMeasured = analogRead(measurePin); // 读取模拟值 delayMicroseconds(deltaTime); // 40us等待时间 digitalWrite(ledPower,HIGH); // 关闭LED delayMicroseconds(sleepTime); // 0 - 5V mapped to 0 - 1023 integer values // recover voltage calcVoltage = voMeasured * (5.0 / 1024.0); //将模拟值转换为电压值 dustDensity = 0.17 * calcVoltage - 0.1;//将电压值转换为粉尘密度输出单位 lcd.home(); lcd.print("DustDen:"); //输出粉尘数值 lcd.print(dustDensity); // 输出单位: 毫克/立方米 delay(1000); } |





#include
LiquidCrystal_I2C lcd(0x20,16,2); //设置LCD的地址为0x20,每行16个字符,共2行
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
void setup(){
Serial.begin(9600);
pinMode(2,OUTPUT);
lcd.init();
lcd.backlight();
}
void loop(){
digitalWrite(2,LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(1); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(2,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
lcd.home();
lcd.print("DustDen:"); //输出粉尘数值
lcd.print(dustDensity); // 输出单位: 毫克/立方米
delay(1000);
}
以上是正确代码,DF技术Cain解答,感谢