I've built several clocks. I like the ones that connect to the Internet and set themselves. This one should be educational since it combines a bunch of different technologies to produce an very interesting clock.
First, we are using an ESP32 processor which comes with built-in WiFi and a lot of powerful features. With its WiFi capabilities, we get NTP or Epoch time from the Internet and use it to configure a DS3231 RTC (real time clock) module.
We are combining it with a 32x8 flexible WS2812B LED Matrix display and using the FastLED library to control the programmable LEDs. I wrote my own matrix routine - i.e. a routine that handles the conversion from the single strand of programmable LEDs (which this display actually is) to a matrix of LEDs addressed by their XY coordinates.
Technically, since the ESP32 is 3.3 volt logic and the LED matrix is 5 volts, we should be using a level shifter for the data line, but I found it unnecessary. The LED display responded fine to 3.3 volt logic signals.
Finally, to display the time on our LED matrix, we need a font suitable for our 32 x 8 LED matrix. And we need software to get the numbers up on the display. So there is a lot going on in our clock software to make this simple clock tick!
Speaking of ticking, our display is only big enough to display hours and minutes. So I am handling seconds in two ways. First, a blinking colon shows seconds. Also, the color of the display tells us approximately where we are in seconds. At the beginning of a minute, the display is green. During the first 30 seconds, it gradually changes from green to blue. During the second 30 seconds, it gradually changes from blue to red, changing back to green when the minute changes. These color changes are accomplished using 256 rainbow hues, a feature built into the FastLED library.
Power is supplied by a small 5 volt wall brick, but we also need 3.3 volts for the ESP32 and RTC. That is derived from an LM2596 adjustable buck regulator module. It needs to be adjusted and set to 3.3 volts out.
Construction / HardwareHardware is pretty straight forward. Our ESP32 board is the Adafruit Huzzah ESP32 Feather, though I suspect any ESP32 board would work. The schematic is shown below:
The LED display has a three wire input, a three wire output (to a potential 2nd display) and separate power connections. For our purposes, we only need the three wire input, which itself supplies power. We are only powering the LEDs at about 6% of max brightness and less than half are on at any one time, so we don't need to connect the separate power lines. So our 3 wire input to the display consists of +5 volts, ground, and a single data input which is connected to digital pin 21 of the ESP32.
The DS3231 RTC module has a place for a battery to keep the time settings when the clock is turned off, but since the clock resets the RTC to Internet time every time the clock is powered up, the battery is really unnecessary.
The buck regulator is the source of the 3.3 volts used by both the ESP32 and the RTC. It needs to be adjusted to output 3.3 volts using the on-board potentiometer. Set the voltage to 3.3 volts before hooking up to the processor and then check the voltage again under load to verify you have 3.3 volts.
The image above shows how the clock is physically wired up prior to installing the display. The box was 3D printed and has a recess around the top which just accommodates the display. A second 3D printed piece called the "display support" was placed behind the flexible display to keep it straight and give it a little support. Everything in the final assembly including the processor are all fastened in place with hot glue.
SoftwareBefore we get into any detail about software, there are some setup items that need to be taken care of. This clock sets itself through WiFi, so you need to give it your WiFi credentials: WiFi server name and password. You also need to specify your time zone as an offset in hours from GMT. (For example, I am in MST (Mountain Standard Time) which is -7 hours from GMT.) You can also change the NTP time server, if you want to, though that is not necessary.
Another detail needing your attention is the various #includes. You will likely have to install NTPClient, FastLED, and Rtc by Makuna. RtcDS3231.h is a part of this last one. The other items you may already have - I did.
With the necessary libraries installed, you should be able to compile the app. Now let's talk a little bit about how it works.
The software basically consists of three components:
1) A connection to the Internet through WiFi to an NTP time server to get Epoch time, which is defined as the number of seconds since the beginning of 1970.
2) Setting up the RTC (real time clock) and setting it to the correct time using Epoch time.
3) Displaying the time on the display in either 24 or 12 hour format. This involves creating an X-Y matrix where individual LEDs can be addressed by their X-Y coordinates, breaking the time down into its component digits, and then using a 6x8 pixel font to display each of those digits.
Our setup() routine has to deal with all three of the processes above. Our loop() routine has the somewhat easier task of just reading the time from the RTC and putting it up on the display.
There are some helper routines that assist in displaying the time:
- The setNUM routine uses our font to place a single digit in one of 4 positions on the display. It does this by storing what goes onto the display in a 32 x 8 array.
- The setColon routine just turns the 8 LEDs that make up the blinking colon on or off.
- The myHue routine manages the color of the display as a function of seconds since the last minute change.
- And finally, the refreshDisplay routine takes the content of the 32x8 array and projects it onto the display, correcting for the fact that the display is actually a single strand of LEDs zig-zagging back and forth across the matrix of LEDs.
Comments