Created
September 12, 2019 16:26
-
-
Save suadanwar/8467a040c6e2526f3fb7c5debc17bbc4 to your computer and use it in GitHub Desktop.
This sample code is to Display Temperature and Humidity on OLED using DHT22 and Arduino tutorial.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include "DHT.h" | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 64 // OLED display height, in pixels | |
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | |
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
#define DHTPIN 2 | |
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |
DHT dht(DHTPIN, DHTTYPE); | |
#define PIEZO 8 | |
#define ALARM 1000 | |
int Sound[] = {ALARM, ALARM}; | |
int SoundNoteDurations[] = {4, 4}; | |
#define playSound() playMelody(Sound, SoundNoteDurations, 2) | |
char inChar; | |
String inString; | |
void setup() { | |
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { | |
Serial.println(F("SSD1306 allocation failed")); | |
for (;;); // Don't proceed, loop forever | |
} | |
display.clearDisplay(); | |
dht.begin(); | |
} | |
void loop() { | |
// Wait a few seconds between measurements. | |
delay(2000); | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit (isFahrenheit = true) | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println(F("Failed to read from DHT sensor!")); | |
return; | |
} | |
// Compute heat index in Fahrenheit (the default) | |
float hif = dht.computeHeatIndex(f, h); | |
// Compute heat index in Celsius (isFahreheit = false) | |
float hic = dht.computeHeatIndex(t, h, false); | |
if (hic >= 41) { | |
displayOled(); | |
playSound(); | |
} | |
else { | |
displayOled(); | |
noTone(PIEZO); | |
} | |
} | |
void displayOled() { | |
// Read humidity | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit (isFahrenheit = true) | |
float f = dht.readTemperature(true); | |
// Compute heat index in Fahrenheit (the default) | |
float hif = dht.computeHeatIndex(f, h); | |
// Compute heat index in Celsius (isFahreheit = false) | |
float hic = dht.computeHeatIndex(t, h, false); | |
display.clearDisplay(); | |
display.setTextColor(WHITE); | |
display.setTextSize(1); | |
display.setCursor(5, 15); | |
display.print("Humidity:"); | |
display.setCursor(80, 15); | |
display.print(h); | |
display.print("%"); | |
display.setCursor(5, 30); | |
display.print("Temperature:"); | |
display.setCursor(80, 30); | |
display.print(t); | |
display.print((char)247); // degree symbol | |
display.print("C"); | |
display.setCursor(5, 45); | |
display.print("Heat Index:"); | |
display.setCursor(80, 45); | |
display.print(hic); | |
display.print((char)247); // degree symbol | |
display.print("C"); | |
display.display(); | |
} | |
void playMelody(int *melody, int *noteDurations, int notesLength) | |
{ | |
pinMode(PIEZO, OUTPUT); | |
for (int thisNote = 0; thisNote < notesLength; thisNote++) { | |
int noteDuration = 1000 / noteDurations[thisNote]; | |
tone(PIEZO, melody[thisNote], noteDuration); | |
int pauseBetweenNotes = noteDuration * 1.30; | |
delay(pauseBetweenNotes); | |
noTone(PIEZO); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment