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

代码如下:

#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 = ; //Connect dust sensor to Arduino A0 pin
int ledPower = ; //Connect 3 led driver pins of dust sensor to Arduino D2
int samplingTime = ;
int deltaTime = ;
int sleepTime = ; int dustVal=;
int i=;
float ppm=;
char s[];
float voltage=;
float dustdensity=;
float ppmpercf=; void setup(){
Serial.begin();
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=;
ppm=;
display.clearDisplay();
}
void loop(){
i=i+;
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)*;
if(ppmpercf<) ppmpercf=; if(dustdensity<) dustdensity=; if(dustdensity>0.5)
dustdensity=0.5;
String dataString="";
dataString+=dtostrf(voltage,,,s);
dataString+="PM2.5 ---> ";
dustdensity = dustdensity*;
dataString+=dtostrf(dustdensity,,,s);
dataString+=" -----> ";
dataString+=dtostrf(ppmpercf,,,s);
i=;
ppm=;
Serial.println(dustdensity);
Serial.println(dataString);
display.clearDisplay();
display.setCursor(,);
display.setTextSize(); //设置字体大小
display.setTextColor(WHITE); //设置字体白色
display.print("dustDensity:");
display.print(dataString); //输出字符
display.display(); //显示以上 delay();
}

Arduino显示PM2.5-LMLPHP

模块:

OLED12864显示屏:

模块针脚        Arduino针脚

VCC     ---->   3.3V

GND    ---->    GND

SCL     ---->    A5

SDA    ---->    A4

夏普灰尘传感器GP2Y10:

Arduino显示PM2.5-LMLPHP

05-07 15:32