Arduino显示PM2.5

这代码一般都是复制过来,在小改下就行了

代码如下:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>  
#include <Adafruit_GFX.h>  
#include <Adafruit_SSD1306.h>  
  
#define OLED_RESET 4  
Adafruit_SSD1306 display(OLED_RESET);  
  
#define LOGO16_GLCD_HEIGHT 16 //定义显示高度  
#define LOGO16_GLCD_WIDTH  16 //定义显示宽度 

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;

int dustVal=0;
int i=0;
float ppm=0;
char s[32];
float voltage=0;
float dustdensity=0;
float ppmpercf=0;

void setup(){
  Serial.begin(9600);
  pinMode(ledPower,OUTPUT);
 // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)  
  // init done  
  i=0;
  ppm=0;  
  display.clearDisplay(); 
}
void loop(){
  i=i+1;
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(samplingTime);
  dustVal = analogRead(measurePin); // read the dust value
  ppm=ppm+dustVal;
  delayMicroseconds(deltaTime);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);

  voltage=ppm/i*0.0049;
  dustdensity=0.17*voltage-0.1;
  ppmpercf=(voltage-0.0256)*120000;
  if(ppmpercf<0) ppmpercf=0; if(dustdensity<0) dustdensity=0; if(dustdensity>0.5)
  dustdensity=0.5;
  String dataString="";
  dataString+=dtostrf(voltage,9,4,s);
  dataString+="PM2.5 ---> ";
  dustdensity = dustdensity*1000;
  dataString+=dtostrf(dustdensity,5,2,s);
  dataString+="    -----> ";
  dataString+=dtostrf(ppmpercf,8,0,s);
  i=0;
  ppm=0;
   Serial.println(dustdensity);
  Serial.println(dataString);
  display.clearDisplay();
  display.setCursor(0,0);
  display.setTextSize(1);             //设置字体大小  
  display.setTextColor(WHITE);        //设置字体白色 
  display.print("dustDensity:"); 
  display.print(dataString);                //输出字符
  display.display();                  //显示以上 

delay(1000);
}

 

模块:

OLED12864显示屏:

模块针脚        Arduino针脚

VCC     ---->   3.3V

GND    ---->    GND

SCL     ---->    A5

SDA    ---->    A4

夏普灰尘传感器GP2Y10:

posted @ 2017-03-11 23:30  dozeoo  阅读(3674)  评论(0编辑  收藏  举报