#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*
* Connections
* SSD1306 OLED | Arduino Uno
* ---------------------------
* VCC | +5V (Vcc/power/+ve)
* GND | GND (Ground/-ve/0v)
* SCL | A5 (Serial Clock Line)
* SDA | A4 (Serial Data Line)
*/
const int SCREEN_WIDTH = 128; // OLED display width, in pixels
const int SCREEN_HEIGHT = 64; // OLED display height, in pixels
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
while(true);
}
}
void loop() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
// Convert the analog value into lux value:
int analogLightValue = analogRead(A0);
float voltage = analogLightValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// Convert the analog value into celsius value:
int analogTempValue = analogRead(A1);
float celsius = 1 / (log(1 / (1023. / analogTempValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// Display static text
display.setCursor(0, 10);
display.println("---Light level---");
display.setCursor(0, 20);
display.println(String(lux)+" lux");
display.setCursor(0, 40);
display.println("---Temperature---");
display.setCursor(0, 50);
display.println(String(celsius)+" degree C");
display.display();
}