// +++++++++++++++++++++++++++++++++++++++ DHT22 Humidity seensor Library +++++++++++++++++++++++++++++++++++++++
#include<dht.h> // Library for Humidity sensor
#define dht_apin 7 // Sensor set on pin No 7
dht DHT; // Instance of dht called DHT created
// +++++++++++++++++++++++++++++++++++++++ Oled Display SSD1306 Library +++++++++++++++++++++++++++++++++++++++
#include <Wire.h> // Wire library for I2C sensor
#include <Adafruit_GFX.h> // Adafruit GFX graphic library
#include <Adafruit_SSD1306.h> // Library for controlling SSD1306 Display
// Display Screen set up
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Instance for screen is set as oled. Widht, Heihgt and connection set for display - reset pin
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++ Program Start +++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void setup(){
// Entry point for program, it will run just once
Serial.begin(9600); // Initiate serial communication at baud rate of 9600 bits per second
Serial.println("DHT11 Humidity & temperature Sensor"); // Console log for debugging
splash(); // Function Splash Screen is called
}
void splash(){
// Splash screen for display oled
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // loop that check if the monitor is correctly attached to the board
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
oled.clearDisplay(); // Clear Eventual Output on display
// ++ Print Temp & Humidity ++
oled.setTextSize(1); // Set a font size for the text
oled.setTextColor(WHITE); // Set a color size for the text
oled.setCursor(0, 10); // Position the cusror to a specific point
oled.print("Temp & Humidity "); // Pint on display
// ++ Print Mauro DC (Student Name) ++
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0, 40);
oled.println("Mauro DC");
oled.display(); // The output is set on display
delay(500); // Delay time to ensure sensor start
}
void loop(){
// Loop for reading the humidity and temperature from DHT22 sensor
DHT.read22(dht_apin); // DHT library set read temperature and humidity data from the sensor
// Print statements for humidity and temperature on console
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(2000); // 2 second pause from each reading
Temp_and_Humidity(); // Function for displaing output
}
void Temp_and_Humidity(){
// Temperature and humidity read on loop function are called into this function and displayed on the OLED screen
oled.clearDisplay();
// ++ Temperature Setting ++
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 10);
oled.println("Temp(C) : ");
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(50, 20);
oled.println(DHT.temperature);
// ++ Humidity Setting ++
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 40);
oled.println("Humidity(%) : ");
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(50, 50);
oled.println(DHT.humidity);
oled.setCursor(60, 90);
oled.println("%");
oled.display(); // Function for displaing output
}