Previously we have published a few articles on Neopixel Ring, such as spindicator/chase effect with NeoPixel ring. Also, we have published one article on Get Time & Date From NTP Server (you can read about NTP server here).
If you combine these two principles, you can build a simple clock based around an ESP32 Arduino to fetch time from an NTP server and a Neopixel Ring to display hours, minutes and seconds. However, instead of the NTP library, we will use the TimeClient library. This library connects to the internet via WiFi and scrapes the header of Google’s default page to obtain the time from the header.
The original code was written by Mike McRoberts (on Hackster) for ESP8266 (his website thearduinoguy.org, his GitHub username thearduinoguy). I have modified it a bit to make it work for ESP32. This is the simplest method to get the clock but this has certain limitations. With this type of project, you can add a dynamic lighting on your analog clock, like the below image (you will probably use the hands in real life):
---
Components and Libraries to Build ESP32 NeoPixel Ring Clock
You need an ESP32 development board, one 24 LED NeoPixel ring and a few jumper wires to connect ESP32 with the 24 LED NeoPixel ring. Your connection will be:
GND -> GND
3.3v -> Vin
Pin 23 -> DI
You need to install two libraries to get it correctly complied. From Arduino IDE, choose Sketch > Include Library > Manage Libraries. Search for “esp8266-weather-station” and install it. The TimeClient.h will be used from this library.
Then search for “json streaming parser” and install that library. JsonListener.h will be used from this library.
Below is the sketch for Arduino IDE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include #include #include "TimeClient.h" #define PIN 23 long lastUpdate = millis(); long lastSecond = millis(); String hours, minutes, seconds; int currentSecond, currentMinute, currentHour; char ssid[] = "xxxx"; // your network SSID (name) char pass[] = "xxxx"; // your network password // to set for India change zero to +0530 const float UTC_OFFSET = 0; TimeClient timeClient(UTC_OFFSET); Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN); void setup() { Serial.begin(115200); Serial.println(); Serial.println(); strip.begin(); strip.setBrightness(128); strip.show(); // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); timeClient.updateTime(); updateTime() ; lastUpdate = millis(); lastSecond = millis(); } void loop() { if ((millis() - lastUpdate) > 1800000) updateTime(); if ((millis() - lastSecond) > 1000) { strip.setPixelColor(currentSecond / 2.5, 0, 0, 0); strip.setPixelColor(currentMinute / 2.5, 0, 0, 0); strip.setPixelColor(currentHour * 2, 0, 0, 0); strip.show(); lastSecond = millis(); currentSecond++; if (currentSecond > 59) { currentSecond = 0; currentMinute++; if (currentMinute > 59) { currentMinute = 0; currentHour++; if (currentHour > 12) currentHour = 0; } } String currentTime = String(currentHour) + ':' + String(currentMinute) + ':' + String(currentSecond); Serial.println(currentTime); strip.setPixelColor(currentSecond / 2.5, 0, 0, 255); strip.setPixelColor(currentMinute / 2.5, 0, 255, 0); strip.setPixelColor(currentHour * 2, 255, 0, 0); strip.show(); } } void updateTime() { hours = timeClient.getHours(); minutes = timeClient.getMinutes(); seconds = timeClient.getSeconds(); currentHour = hours.toInt(); if (currentHour >= 12) currentHour = currentHour - 12; currentMinute = minutes.toInt(); currentSecond = seconds.toInt(); lastUpdate = millis(); } |