ต้องติดตั้ง
Lib ของ
Driver for the SSD1306 and SH1106 based 128x64 pixel OLED
#include <SH1106Wire.h>
#include <OLEDDisplayFonts.h>
#include <OLEDDisplay.h>
#include <SSD1306.h>
#include <SSD1306Wire.h>
#include <OLEDDisplayUi.h>
//#include "SSD1306.h"
int measurePin = 2;
int ledPower = 17;
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
float tDust=0;
SSD1306 display(0x3c, 4, 15);
int loopAvr=5;
void setup(){
pinMode(16,OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
display.drawString(0,0,"Raw:");
display.display();
// for sensor
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
}
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 - 3.3V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (3.3 / 1024);
// linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
// Chris Nafis (c) 2012
dustDensity = 17.0 *( calcVoltage - 0.1);
display.clear();
Serial.print("Raw Signal Value (0-1023): ");
Serial.print(voMeasured);
String SerialData="";
SerialData = String(voMeasured,0);
display.drawString(6,0,SerialData);
Serial.print(" - Voltage: ");
Serial.print(calcVoltage);
if(loopAvr==0)
{
String DustData="";
tDust+=dustDensity;
DustData = String(tDust/5.0,3);
tDust=0;
loopAvr=5;
Serial.print(" - Dust Density: ");
Serial.println(dustDensity);
display.drawString(0,20,"PM2.5");
display.drawString(50,20,DustData);
display.display();
} else {
tDust+=dustDensity; loopAvr--;
}
delay(1000);
}