This is an update of the previous project TM1637 4-Digit Display Clock Arduino Project . I added a temperature & humidity sensor DHT11 or DHT22.
Schematics:
/* https://arduinogeeks.blogspot.com */ | |
/* Degital 24 hour time format clock by Arduino, TM1637 4 digit 7 segment display and DS32321 RTC.*/ | |
// Add libraries: RTClib and TM1637 | |
#include "RTClib.h" | |
#include <TM1637Display.h> | |
// Define the connections pins for TM1637 4 digit 7 segment display | |
#define CLK 8 | |
#define DIO 9 | |
// Create rtc and display object | |
RTC_DS3231 rtc; | |
TM1637Display display = TM1637Display(CLK, DIO); | |
#include <Adafruit_Sensor.h> | |
#include <DHT.h> | |
// Define the connections pins | |
#define CLK 8 | |
#define DIO 9 | |
#define DHTPIN 5 | |
// Create variable | |
int temperature_celsius; | |
int temperature_fahrenheit; | |
int humidity_read; | |
// Create °C symbol | |
const uint8_t celsius[] = { | |
SEG_A | SEG_B | SEG_F | SEG_G, // Circle | |
SEG_A | SEG_D | SEG_E | SEG_F // C | |
}; | |
// Create °F symbol | |
const uint8_t fahrenheit[] = { | |
SEG_A | SEG_B | SEG_F | SEG_G, // Circle | |
SEG_A | SEG_E | SEG_F | SEG_G // F | |
}; | |
// Create H symbol | |
const uint8_t humidity[] = { | |
SEG_B | SEG_C | SEG_G | SEG_E | SEG_F // H | |
}; | |
// Uncomment whatever type you're using! | |
#define DHTTYPE DHT11 // DHT 11 | |
//#define DHTTYPE DHT22 // DHT 22 (AM2302) | |
// Create dht object of type DHT: | |
DHT dht = DHT(DHTPIN, DHTTYPE); | |
void setup() { | |
// Begin serial communication at a baud rate of 9600 | |
Serial.begin(9600); | |
// Wait for console opening | |
delay(3000); | |
// Check if RTC is connected correctly | |
if (! rtc.begin()) { | |
Serial.println("Couldn't find RTC"); | |
while (1); | |
} | |
// Check if the RTC lost power and if so, set the time: | |
if (rtc.lostPower()) { | |
Serial.println("RTC lost power, lets set the time!"); | |
// The following line sets the RTC to the date & time this sketch was compiled: | |
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | |
// This line sets the RTC with an explicit date & time, for example to set | |
// January 21, 2014 at 3am you would call: | |
rtc.adjust(DateTime(2022,4, 20, 9, 57, 0)); | |
} | |
// Setup sensor | |
dht.begin(); | |
// Set the display brightness (0-7): | |
display.setBrightness(5); | |
// Clear the display: | |
display.clear(); | |
} | |
void loop() { | |
// Get current date and time | |
DateTime now = rtc.now(); | |
// Create time format to display: | |
int displaytime = (now.hour() * 100) + now.minute(); | |
// Print displaytime to the Serial Monitor | |
Serial.println(displaytime); | |
// Display the current time in 24 hour format with leading zeros enabled and a center colon: | |
display.showNumberDecEx(displaytime, 0b11100000, true); | |
// Remove the following lines of code if you want a static instead of a blinking center colon: | |
//delay(1000); | |
//display.showNumberDec(displaytime, true); // Prints displaytime without center colon. | |
delay(5000); | |
// Read the temperature as Celsius and Fahrenheit | |
temperature_celsius = dht.readTemperature(); | |
temperature_fahrenheit = dht.readTemperature(true); | |
humidity_read = dht.readHumidity(); | |
display.clear(); | |
// Display the temperature in celsius format | |
display.showNumberDec(temperature_celsius, false, 2, 0); | |
display.setSegments(celsius, 2, 2); | |
delay(2000); | |
display.clear(); | |
// Display the temperature in fahrenheit format | |
display.showNumberDec(temperature_fahrenheit, false, 2, 0); | |
display.setSegments(fahrenheit, 2, 2); | |
delay(2000); | |
display.clear(); | |
// Display the humidity | |
// Display the humidity | |
display.showNumberDec(humidity_read, false, 2, 0); | |
display.setSegments(humidity, 1, 3); | |
delay(2000); | |
display.clear(); | |
} |