Last active
February 25, 2025 12:08
-
-
Save MichalSkoula/f0fbc41220841790867da1fbb346fd01 to your computer and use it in GitHub Desktop.
Simple script to read temperature and humidity values from DHT11 senzor and print it on OLED via Arduino NANO. The result is a lovely thermometer.
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 <DHT.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#define pinDHT 7 | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 64 // OLED display height, in pixels | |
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) | |
#define SCREEN_ADDRESS 0x3C // See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 | |
#define typDHT11 DHT11 // DHT 11 | |
DHT mojeDHT(pinDHT, typDHT11); | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
void setup() { | |
// Initialize serial communication at 9600 baud | |
Serial.begin(9600); | |
// Start DHT sensor communication | |
mojeDHT.begin(); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { | |
Serial.println(F("SSD1306 allocation failed")); | |
for(;;); // Don't proceed, loop forever | |
} | |
} | |
void loop() { | |
// Read temperature and humidity values using readTemperature and readHumidity functions | |
// Reading takes about 250ms | |
float temp = mojeDHT.readTemperature(); | |
float humidity = mojeDHT.readHumidity(); | |
// Check if the read values are numbers using isnan function | |
if (isnan(temp) || isnan(humidity)) { | |
// Print error message if reading fails | |
Serial.println("Error reading from DHT sensor!"); | |
} else { | |
// If values are valid, print them through serial port | |
Serial.print("Temperature: "); | |
Serial.print(temp); | |
Serial.print(" degrees Celsius, "); | |
Serial.print("Humidity: "); | |
Serial.print(humidity); | |
Serial.println(" %"); | |
} | |
// Delay for clearer output | |
delay(2000); | |
// Clear the buffer | |
display.clearDisplay(); | |
// Draw text | |
display.setTextColor(SSD1306_WHITE); | |
// Draw temperature and humidity values on the display | |
display.setTextSize(1); | |
display.setCursor(5, 5); | |
display.println(F("TEMPERATURE")); | |
display.setTextSize(2); | |
display.setCursor(5, 15); | |
display.println(temp); | |
display.setTextSize(1); | |
display.setCursor(5, 40); | |
display.println(F("HUMIDITY")); | |
display.setTextSize(2); | |
display.setCursor(5, 50); | |
display.println(humidity); | |
// Show the display buffer on the screen | |
display.display(); | |
} |
Thermo & humidity meter for my filament drybox
https://www.printables.com/model/161748-simple-stand-for-oled-096
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credits: https://navody.dratek.cz/navody-k-produktum/teplotni-senzor-dht11.html