Electro dude
Published © CC BY-NC-SA

Its will cost only 10$ to build this beautiful clock

This time I have designed something very beautiful, smart and fully customizable 😍 β€” A Cloud Based RGB Smart Clock made using WS2812B LEDs,

IntermediateProtip5 hours36
Its will cost only 10$ to build this beautiful clock

Things used in this project

Story

Read more

Code

Sample Code

C/C++
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>

// Wi-Fi Credentials
const char* ssid = "SSID";        // Replace with your Wi-Fi SSID
const char* password = "Password"; // Replace with your Wi-Fi Password

// NTP Client setup
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000); // IST offset (+5:30)

// WS2812B LED setup
#define LED_PIN 0                     // GPIO 0 for WS2812B
#define TOTAL_LEDS 56                 // Total LEDs
Adafruit_NeoPixel strip(TOTAL_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

const int TOTAL_SEGMENTS = 4;         // Number of digits
const int LEDS_PER_SEGMENT = 14;      // LEDs per digit
const int DISPLAY_SEGMENT[] = {0, 14, 28, 42}; // Start index for each digit

// Number representation (adjust for 14 LEDs per digit)
const bool DISPLAY_NUMBER[][14] = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, // 0
  {0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // 1
  {1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1}, // 2
  {1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1}, // 3
  {0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1}, // 4
  {1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}, // 5
  {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 6
  {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // 7
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 8
  {1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}  // 9
};

// Pin for LDR sensor
#define LDR_PIN A0

void setup() {
  Serial.begin(115200);

  // Connect to Wi-Fi
  Serial.print("Connecting to Wi-Fi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100); // Reduce delay for faster connection
    Serial.print(".");
  }
  Serial.println("\nConnected!");

  // Initialize NTP Client
  timeClient.begin();

  // Initialize WS2812B LEDs
  strip.begin();
  strip.show();
}

void loop() {
  // Update time
  if (!timeClient.update()) {
    timeClient.forceUpdate(); // Ensure immediate time update on start
  }

  // Get current time (HH:MM)
  String formattedTime = timeClient.getFormattedTime();
  int hour = formattedTime.substring(0, 2).toInt();
  int minute = formattedTime.substring(3, 5).toInt();

  // Adjust brightness based on LDR reading
  adjustBrightness();

  // Display time on LEDs
  displayDigit(hour / 10, 0);   // Tens of hours
  displayDigit(hour % 10, 1);   // Units of hours
  displayDigit(minute / 10, 2); // Tens of minutes
  displayDigit(minute % 10, 3); // Units of minutes

  delay(500); // Update every half second for smooth display
}

// Function to display a digit
void displayDigit(int number, int segment) {
  int startIdx = DISPLAY_SEGMENT[segment]; // Starting LED index for this digit
  for (int i = 0; i < LEDS_PER_SEGMENT; i++) {
    if (DISPLAY_NUMBER[number][i]) {
      strip.setPixelColor(startIdx + i, strip.Color(0, 0, 255)); // Blue for active segments
    } else {
      strip.setPixelColor(startIdx + i, strip.Color(0, 0, 0));   // Off for inactive segments
    }
  }
  strip.show();
}

// Function to adjust brightness based on LDR sensor value
void adjustBrightness() {
  int ldrValue = analogRead(LDR_PIN);  // Read the LDR value (0-1023)
  int brightness = map(ldrValue, 0, 1023, 10, 255); // Map LDR value to LED brightness range
  strip.setBrightness(brightness);
  Serial.print("LDR Value: ");
  Serial.print(ldrValue);
  Serial.print(" | Brightness: ");
  Serial.println(brightness);
}

Credits

Electro dude
20 projects β€’ 49 followers
Hey, I am Electro dude 😎.You can also find me. πŸ“Ή YouTube: ElectroDude Channel πŸ“Έ Instagram: @electro__dude πŸ“˜ Facebook: ElectroDude Page

Comments