#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SHT31_ADDRESS 0x44 // SHT31 sensor address
#define RGB_LED_RED 26 // Red LED pin
#define RGB_LED_GREEN 27 // Green LED pin
#define RGB_LED_BLUE 25 // Blue LED pin
uint8_t buffer[6];
int RGB_RED = 0;
int RGB_GREEN = 0;
int RGB_BLUE = 0;
// 'apple (1)', 52x52px
const unsigned char APPLE [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x78, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x0f, 0xfe, 0x01, 0xff,
0xc0, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xf8, 0x00,
0x00, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x03, 0xff,
0xff, 0xff, 0xff, 0xf8, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x07, 0xff, 0xff, 0xff,
0xff, 0xe0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xc0,
0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f,
0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f, 0xff, 0xff,
0xff, 0xff, 0x80, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff,
0x80, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00,
0x0f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x07, 0xff,
0xff, 0xff, 0xff, 0xe0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x07, 0xff, 0xff, 0xff,
0xff, 0xf8, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x01,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xff,
0xff, 0xff, 0xf8, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xf0, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xe0, 0x00,
0x00, 0x0f, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x03,
0xfe, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x7c, 0x00, 0x00
};
// 'room-temperature (1)', 52x52px
const unsigned char Temp [] PROGMEM = {
0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x61, 0x9f, 0xe0, 0x00, 0x00, 0x00, 0x70, 0xf0, 0xff, 0xe0,
0x00, 0x00, 0x00, 0xe3, 0x9c, 0x78, 0x60, 0x00, 0x00, 0x01, 0x87, 0x0e, 0x18, 0x60, 0x00, 0x00,
0x07, 0x0c, 0x03, 0x0e, 0x60, 0x00, 0x00, 0x0e, 0x18, 0x01, 0xc7, 0x60, 0x00, 0x00, 0x1c, 0x00,
0x00, 0xe3, 0xe0, 0x00, 0x00, 0x30, 0xc0, 0x00, 0x70, 0xe0, 0x00, 0x00, 0xe1, 0x80, 0x00, 0x18,
0x70, 0x00, 0x01, 0xc7, 0x00, 0x00, 0x0e, 0x38, 0x00, 0x03, 0x0e, 0x00, 0x60, 0x07, 0x0c, 0x00,
0x0e, 0x18, 0x01, 0xf8, 0x01, 0x87, 0x00, 0x1c, 0x70, 0x03, 0x9c, 0x00, 0xe3, 0x80, 0x30, 0xe0,
0x03, 0x0c, 0x00, 0x70, 0xc0, 0xe1, 0x81, 0xf3, 0x0c, 0xf8, 0x18, 0x70, 0xc3, 0x00, 0x03, 0x0c,
0x00, 0x0c, 0x30, 0x6e, 0x00, 0x03, 0x0c, 0x00, 0x07, 0x60, 0x3e, 0x00, 0x03, 0x0c, 0x00, 0x07,
0xc0, 0x32, 0x00, 0xf3, 0x0c, 0xf0, 0x04, 0xc0, 0x02, 0x00, 0x03, 0x0c, 0x00, 0x04, 0x00, 0x02,
0x00, 0x03, 0x0c, 0x00, 0x04, 0x00, 0x02, 0x00, 0x03, 0x0c, 0x00, 0x04, 0x00, 0x02, 0x01, 0xf3,
0x6c, 0xf8, 0x04, 0x00, 0x02, 0x00, 0x03, 0x6c, 0x00, 0x04, 0x00, 0x02, 0x00, 0x03, 0x6c, 0x00,
0x04, 0x00, 0x02, 0x00, 0x03, 0x6c, 0x00, 0x04, 0x00, 0x02, 0x00, 0xf3, 0x6c, 0xf0, 0x04, 0x00,
0x02, 0x00, 0x63, 0x6c, 0x60, 0x04, 0x00, 0x02, 0x00, 0x03, 0x6c, 0x00, 0x04, 0x00, 0x02, 0x00,
0x03, 0x6c, 0x00, 0x04, 0x00, 0x02, 0x01, 0xf3, 0x6c, 0xf8, 0x04, 0x00, 0x02, 0x01, 0xf3, 0x6c,
0xf8, 0x04, 0x00, 0x02, 0x00, 0x03, 0x6c, 0x00, 0x04, 0x00, 0x02, 0x00, 0x03, 0x6c, 0x00, 0x04,
0x00, 0x02, 0x00, 0xf3, 0x6c, 0xf0, 0x04, 0x00, 0x02, 0x00, 0x73, 0x6c, 0xe0, 0x04, 0x00, 0x02,
0x00, 0x06, 0x66, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0c, 0x63, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0d,
0xfb, 0x00, 0x04, 0x00, 0x03, 0x00, 0x09, 0x99, 0x00, 0x0c, 0x00, 0x03, 0xff, 0xc9, 0x09, 0x3f,
0xfc, 0x00, 0x00, 0x00, 0x09, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0xfb, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0c, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00
};
const unsigned char Humidity [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00,
0x00, 0x00, 0x00, 0x00, 0x61, 0x80, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x01,
0x80, 0x60, 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x60, 0x08,
0x00, 0x00, 0x00, 0x0c, 0xf2, 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x92, 0x0c, 0x00, 0x00, 0x00, 0x08,
0xf6, 0x04, 0x00, 0x00, 0x00, 0x08, 0x64, 0x04, 0x00, 0x00, 0x00, 0x08, 0x0c, 0x84, 0x00, 0x00,
0x00, 0x08, 0x0b, 0xc4, 0x00, 0x00, 0x00, 0x08, 0x1a, 0x44, 0x00, 0x00, 0x00, 0x08, 0x13, 0xc4,
0x00, 0x00, 0x00, 0x0c, 0x01, 0x8c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x06,
0x00, 0x18, 0x00, 0x00, 0x01, 0xf3, 0x00, 0x33, 0xe0, 0x00, 0x07, 0x11, 0x80, 0x62, 0x38, 0x00,
0x0c, 0xf0, 0xff, 0xc3, 0xcc, 0x00, 0x1b, 0x80, 0x1e, 0x00, 0x76, 0x00, 0x16, 0x78, 0x00, 0x07,
0x9a, 0x00, 0x34, 0xd8, 0x00, 0x06, 0xcb, 0x00, 0x2d, 0x38, 0x7f, 0x87, 0x2d, 0x00, 0x2b, 0x60,
0xff, 0xc1, 0xb5, 0x00, 0x2a, 0xc1, 0xfc, 0xe0, 0xd5, 0x00, 0x3a, 0x83, 0xfc, 0xf0, 0x57, 0x00,
0x13, 0x83, 0x00, 0x10, 0x72, 0x00, 0x00, 0x06, 0x61, 0x18, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xff, 0x00, 0x7f, 0x7b, 0xde, 0xf7, 0xbd, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00,
0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x60, 0x00, 0x00, 0x00, 0x01, 0x80,
0x3e, 0x7f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(RGB_LED_RED, OUTPUT);
pinMode(RGB_LED_GREEN, OUTPUT);
pinMode(RGB_LED_BLUE, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) {
// Don't proceed, loop forever
}
}
delay(2000); // Pause for 2 seconds
display.clearDisplay();
display.drawBitmap(40, 3, APPLE, 52, 52, WHITE);
display.display();
delay(5000);
display.clearDisplay();
}
void loop() {
Wire.beginTransmission(SHT31_ADDRESS);
Wire.write(0x24); // Send command to read temperature and humidity
Wire.write(0x16); // Send command to start measurement
Wire.endTransmission();
delay(500);
int rv = Wire.requestFrom(SHT31_ADDRESS, 6);
if (rv == 6) {
for (uint8_t i = 0; i < 6; i++) {
buffer[i] = Wire.read();
}
}
uint16_t rawTemperature = (buffer[0] << 8) + buffer[1];
uint16_t rawHumidity = (buffer[3] << 8) + buffer[4];
float temperature = rawTemperature * (175.0 / 65535) - 45;
float humidity = rawHumidity * (100.0 / 65535);
// Map temperature and humidity to RGB color
if (temperature <= 29.99) {
RGB_RED = 0; // Light Blue
RGB_GREEN = 0;
RGB_BLUE = 255;
} else if (temperature >= 30.0 && temperature <= 31.99) {
RGB_RED = 0; // Green
RGB_GREEN = 255;
RGB_BLUE = 0;
} else {
RGB_RED = 255; // Red
RGB_GREEN = 0;
RGB_BLUE = 0;
}
// Set RGB LED colors
analogWrite(RGB_LED_RED, RGB_RED);
analogWrite(RGB_LED_GREEN, RGB_GREEN);
analogWrite(RGB_LED_BLUE, RGB_BLUE);
// Display temperature, humidity, and RGB values on the OLED display
display.clearDisplay();
display.setCursor(5, 0);
display.drawBitmap(5, 10, Temp, 52, 52, WHITE);
display.setCursor(55, 5);
display.setTextSize(1);
display.println("Temp is ....");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(65, 25);
display.print(temperature, 2); // Display temperature with 2 decimal places
display.setCursor(80, 50);
display.println("C");
display.display();
display.clearDisplay();
delay(2000);
display.setCursor(5, 0);
display.drawBitmap(5, 10, Humidity, 42, 42, WHITE);
display.setCursor(55, 5);
display.setTextSize(1);
display.println("humidity is ....");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(55, 25);
display.print(humidity, 2); // Display temperature with 2 decimal places
display.setCursor(75, 50);
display.println("%");
display.display();
delay(2000);
}