TIMOTHY MWALA
Published © GPL3+

Automated LED Lighting System with Real-Time Clock

Build Your Own Automated LED Lighting system with a Real-Time Clock

BeginnerProtip1 hour53
Automated LED Lighting System with Real-Time Clock

Story

Read more

Code

Automated LED lighting system with real time clock

C/C++
#include <Wire.h>                // Include the Wire library for I2C communication
#include <RTClib.h>              // Include the RTClib library for real-time clock functionality
#include <Adafruit_GFX.h>        // Include the Adafruit GFX library for graphics
#include <Adafruit_SSD1306.h>    // Include the Adafruit SSD1306 library for OLED display

#define SCREEN_WIDTH 128        // Define the width of the OLED display
#define SCREEN_HEIGHT 64        // Define the height of the OLED display

#define OLED_RESET    -1       
#define SCREEN_ADDRESS 0x3C     

#define LED_PIN 4               // Define the pin connected to the LED

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS3231 rtc;                                                           

void setup() {
  Serial.begin(9600);           
  
  pinMode(LED_PIN, OUTPUT);      // Set the LED pin as an output
  
  if (!rtc.begin()) {           
    Serial.println("Couldn't find RTC");  found
    while (1);                 
  }

  if (rtc.lostPower()) {        // Check if the RTC lost power
    Serial.println("RTC lost power, let's set the time!"); // Print message indicating RTC lost power
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));       // Set the time to compile time
  }

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { // Initialize the OLED display
    Serial.println(F("SSD1306 allocation failed"));        
    for (;;);                                             
  }

  display.display();            
  delay(2000);               
  display.clearDisplay();       
}

void loop() {
  DateTime now = rtc.now();     // Get the current time from the RTC
  
  display.clearDisplay();       // Clear the display buffer
  display.setTextSize(1);      
  display.setTextColor(SSD1306_WHITE); 
  display.setCursor(0, 0);      
  display.println("Current Time:"); 
  display.setTextSize(2);      
  display.setCursor(5, 10);    
  display.print(now.hour(), DEC); // Display hour
  display.print(':');           
  if (now.minute() < 10) {      
    display.print('0');          
  }
  display.print(now.minute(), DEC); // Display minute
  display.print(':');            
  if (now.second() < 10) {     
    display.print('0');          
  }
  display.println(now.second(), DEC); // Display second
  display.setTextSize(1);       
  display.setCursor(0, 33);     
  display.println("Current Date:"); 
  display.setTextSize(2);       
  display.setCursor(5, 45);     
  display.print(now.day(), DEC); // Display day
  display.print('/');           
  display.print(now.month(), DEC); // Display month
  display.print('/');            
  display.println(now.year(), DEC); 
  display.display();            
  
  // Check if the current time is between 19:00:00 and 06:00:00
  if (now.hour() >= 19 || now.hour() < 6) {
    digitalWrite(LED_PIN, HIGH); // Turn on the LED
  } else {
    digitalWrite(LED_PIN, LOW);  // Turn off the LED
  }
  
  delay(1000);                   // Delay for 1 second
}
```

Credits

TIMOTHY MWALA

TIMOTHY MWALA

15 projects • 11 followers
SmartHome Nerd

Comments