ESP32 Wifi Clock With WS2812

by Lan_Makerfabs in Circuits > LEDs

10050 Views, 19 Favorites, 0 Comments

ESP32 Wifi Clock With WS2812

206.jpg

Many times I woke up in the middle of the night and wanted to check the time. I need to turn on the lights or wake up the phone to view. When I turn on the light or phone, the light from the light or phone, dazzled me. I wanted to make a clock with the shimmer that can be placed in bedroom.

This tutorial shows how to light a NeoPixel LED and make a simple clock with 12 LED ring. I hope it is helpful for beginners to build a project.

Hardware Preparation

QQ图片20210206114026.jpg

I have an ESP32 board and WS2812 LED ring in hand so I used for it. ESP32 can be replaced by any Arduino board to light a LED. The WS2812 is also replaced by any WS2812 LEDs.

Here I use the below 2 board, you can sourcing related boards as your preference:

The Connection for LEDs

QQ图片20210206114030.jpg

One NeoPixel LED has 4 pins (VCC, GND, DI, DO). The wiring that many LED connect together is as the figure showed.

Whatever the number of LEDs connected, I can control them by connecting the three pins (VCC, GND, DI) to Arduino. The connection as the below:

  • VCC connects to the 5V of the ESP32 board.
  • GND connects to the GND of the ESP32 board.
  • DI connects to the IO21 of the ESP32 board.

Software Preparation

Install Arduino ide to programming if you do not install it yet.

There are many libraries for NeoPixel led. I used the Adafruit one (Adafruit NeoPixel).

  1. Click the Tools option, select the “manage Library”.
  2. Enter “neopixel” in the search box, then install the Adafruilt NeoPixel library.

Programming for Lighting the LED

QQ图片20210206114712.jpg

1. Open Arduino and create a new file named WS2812.

2. Choose the board and port, the port can only be selected white the Arduino board is connected to the PC.

3. Add the library to the code.

#include <Adafruit_NeoPixel.h><br>

4. Define the pin number connected to LEDs, and define the number of connected LEDs.

#define PIN         21
#define NUMPIXELS  12

5. Define an object for the LEDs.

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);<br>

6. Innilize the LEDs in the setup code.

pixels.begin();<br>

7. In the loop code, turn the LED to light green with 2s, then turn off with 1s.

  pixels.setPixelColor(2, pixels.Color(0, 70, 0));
  pixels.show();
  delay(2000);
  pixels.clear();
  delay(1000);
  • “setPixelColor()” is to set which the LED and which the color.
  • “2” is means the second LED to light.
  • “(0,70,0)” represent the green color, “(70,0,0)” represent the red, “(0,0,70)” represents the blue. The number can be changed from 0 to 255.
  • “pixels.show()” is means send the updated pixel colors to the hardware.
  • “pixels.show()” is means set all pixel colors to 'off'.

8. The program should be like below when it completed.

#include <Adafruit_NeoPixel.h>
#define PIN        21
#define NUMPIXELS 12

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  // put your setup code here, to run once:
  pixels.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  pixels.setPixelColor(2, pixels.Color(0, 70, 0));
  pixels.show();
  delay(2000);
  pixels.clear();
  delay(1000);
}

9. Upload the program to the Arduino board.

Make a Clock

Next, I will introduce a project that I used the WS2812 ring to make a clock.

I wanted to get time from the internet and show it with the LED ring. If the LED ring is a clock, the LED lighted green will be the hour hand, the blue or red one will be the minute hand. Of ofcourse, basically, you can use any other controller with Wifi connection, such as the ESP8266.

Programing for ESP32.

图形1.jpg

1. The program can be obtained from https://github.com/Makerfabs/Makepython-ESP32/tre...

2. Define the wifi name and password.

const char *ssid = "Makerfabs"; 
const char *password = "20160704"; 

3. Connect to the wifi WiFi.

begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
Serial.println(" CONNECTED");

4. Set the time zone of your area and configure the time.

const char *ntpServer = "120.25.108.11";
const long gmtOffset_sec = 8 * 60 * 60; //China+8
const int daylightOffset_sec = 0;
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); 

5. Get the time from the internet.

if (!getLocalTime(&timeinfo))
      {
          Serial.println("Failed to obtain time");
          return;
      }
  int hour = timeinfo.tm_hour;
  int min = timeinfo.tm_min;
  int sec = timeinfo.tm_sec;
  int day = timeinfo.tm_mday;
  int mon = timeinfo.tm_mon + 1;

6. Turn on the LEDs. The LED color can be set at will.

pixels.setPixelColor(hour_d, pixels.Color(10, 70, 0));
  pixels.setPixelColor(min_d, pixels.Color(min_r*15+10, 0, (5-min_r)*15));
  pixels.show();
  delay(999);

Make a Case

QQ图片20210204111132.jpg
QQ图片20210204140357.jpg

I used the Carton that used to be a packaging box to package the LED ring. First, drill 12 holes on the carton surface. Then put all that includes a battery for power in the carton and assemble it. Draw on the case to decorate it. A real clock is completed.

Finally

IMG_0198.JPG
DIY LED CLOCK-2.jpg

After making the clock, I can tell the time by distinguishing the light. If the brightness of the light is still too strong, I can adjust it through the program, or stick a piece of paper on the surface of the clock.