码迷,mamicode.com
首页 > 其他好文 > 详细

Arduino显示PM2.5

时间:2017-03-12 00:58:11      阅读:1011      评论:0      收藏:0      [点我收藏+]

标签:arduino   val   amp   技术   display   serial   UI   mode   code   

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

代码如下:

#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:

技术分享

Arduino显示PM2.5

标签:arduino   val   amp   技术   display   serial   UI   mode   code   

原文地址:http://www.cnblogs.com/nightnine/p/6536662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!