楼主: 伊问风
|
[常见问题] Sharp GP2Y10 空气质量传感器连接Arduino2560及相关问题 |
如果不接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); } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed