一块arduino uno主控器,一块SHARP GP2Y1010AU0F compact optical dust sensor(灰尘传感器),一块SHARP i2c 1602显示器
代码:
用Arduino软件进行写入:
[mw_shl_code=cpp,true]#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
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;
void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(5, 0);
lcd.print("Boot...");
lcd.setCursor(0, 1);
for(int i=0;i<16;i++)
{
lcd.write(0xff);
delay(250);
}
}
void loop(){
digitalWrite(ledPower,LOW); // power on the LED
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
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("dustDensity:");
lcd.setCursor(0,1);
lcd.print(dustDensity);
lcd.setCursor(9,1);
lcd.print(" mg/m^3");
delay(1000);
}[/mw_shl_code]
因为有同学还是新手,这边给个详细的过程
先复制上方代码到IDE 里,
然后
就可以使用了
最后附上