This article was translated to English, and was originally published for deviceplus.jp.
What alarm do you wake up to every morning? A common problem among morning folk is stopping the alarm and going straight back to sleep.
This time, we decided to use ESP32 to make a “wake up alarm clock” that should help solve this problem.
Electronic Work Recipe
Est. time：120 min
|ESP32-DevKitC(2 pc)||Mouser||About $10.00|
|DS3231 Module||Amazon||About $3.00|
|LIR2032 (Button charger)||Amazon||About $3.60|
|2.8-inch SPI connector 320×240 Pixel LCD screen||Amazon||About $15.00|
*In addition to the items above, one tact switch, one LED, and one resistor of about 100Ω will also be needed.
Itinerary for today’s article
Let’s use the ESP32 to display the date and time on the screen like a normal alarm clock so that the alarm can ring at a specified time. To make a “wake up alarm clock,” we need to place the extension button (for stopping the alarm) separately from the alarm clock body. The idea is that you would have to get up from bed and walk over to where the extension button is to turn off the alarm.
As shown in the video below, use two ESPs, one for the main unit and the other for the extension. In the video, the main unit and the handset are next to each other, but they can be separated if they’re connected by Wi-Fi.
For microcomputers such as Arduino, it’s common to be able to get the elapsed time upon booting up the program. However, the time you get is often not very accurate, so the elapsed time will reset when the power is turned off.
With ESP32, you can connect to the internet via Wi-Fi to get the date and time periodically from an NTP server on the internet and set it on ESP32. However, some microcomputers, such as Arduino Uno, don’t have an internet connection function, so it would be useful to have a mechanism that could handle date and time easier.
That’s why an IC called “RTC” (Realtime Clock) is commonly used. An RTC is an IC that makes time tick based on elements that are periodically signaled. In addition, by connecting it to an external power supply such as a battery, time can be maintained even while the power to the microcomputer is switched off.
Numerous products are in RTC, but this time, we’ll be using an RTC that’s been made into a module called “DS3231.”
The DS3231 module is popular among the RTC modules used in electronic work, and it’s readily available. Since the connection is I2C, only four wires are required. In addition to the RTC function, there’s also a temperature sensor function (however, the temperature sensor won’t be used in this article).
In addition, on the DS3231 of Photo 1, a button battery called “LIR2032” is installed so that the date and time will continue to be recorded even when the microcomputer is turned off. LIR2032 is the same battery size as CR2032, except that it’s rechargeable.
Since the clock is used to check the current date and time, it’s necessary to display the date and time in an easy-to-understand manner. The following devices are used to display the date and time.
There are different libraries and different programming methods depending upon the device. It also depends upon whether it’s suitable for what you want to make. For example, a 7-segment LED is suitable for displaying only numbers at a low cost, but won’t be suitable for a detailed display. Among the above, the graphic LCD display is the most versatile and can be used for many different projects, so we decided to use it for today’s demonstration.
There are various types of LCD display modules out there, but for today’s project, we’ll be using a controller mount called “ILI9341” and be using an SPI connection (Photo 2). In addition, LCD displays are commonly sold in 2.2-inch/ 2.4-inch /2.8-inch sizes, so adjust accordingly to the type of work you’re doing.
Since it’s an alarm clock, having a sound ring out at a specified time is a necessity. You can connect a buzzer to the ESP32 to produce a single sound, but if you’d like, you could also use your own favorite sound. For that, we’ll be using a module called “DFPlayer Mini” that can play back any MP3 data (Photo 3).
DFPlayer Mini is a module that can play MP3 in a microSD card by sending commands via a serial connection. A small speaker can be connected to the speaker output pin to produce sound.
Let’s get into the actual production. First, wire the alarm clock.
Use two breadboards, one with ESP32 and DS3231, and the other with LCD and DFPlayer Mini. The wiring of each part is as shown in Figure 2.
Since the ESP32 is wide, you can only put a jump wire on one side of a normal breadboard. Therefore, use a breadboard with only one side of the power line and one row of holes instead (such as SAD-101 from Sanhayato).
ESP32 and LCD displays are connected by SPI. ESP32 can use two SPIs (VSPI and HSPI), but for VSPI, (18/19/23 pin) is used (Table 1).
ESP32 and DS3231 are connected via I2C. In ESP32, I2C can be assigned to any pin, but standard pins (SDA = 21 and SCL = 22) are used (Table 2).
DFPlayer Mini connects serially. ESP32 can use three serials, but for this, use the combination of 16th pin and 17th pin (Table 3). Also, connect speakers to the DFPlayer Mini “SPK1” and “SPK2” pins.
|ESP32 pin||LCD display pin|
Table 1: Connection between ESP32 and LCD
|ESP32 pin||DS3231 pin|
Graph 2: ESP32 and DS3231 connection
|ESP32 pin||DFPlayer Mini pin|
Graph 3: ESP32 and DFPlayer Mini connection
Next, we’ll be doing the wiring for the extension. The wiring for this should look like Fig. 3. All you have to do is connect the switch and LED to the ESP32. Connect one side of the switch to the 3V3 pin of ESP32 and the other side to pin 4. Connect to GND via ESP32 pin 13 via resistor → LED.
On the circuit that reads the switch status, insert a pull-up resistor or pull-down resistor. However, since the ESP32 can perform pull-up/pull-down with an internal resistor, the external resistor is omitted.
When you finish working on the circuits, you can create the program. First, start with installing each of the below libraries.
The installation procedure is as follows.
There are several RTCLib and DFPlayer libraries with similar names. RTCLib installs “RTCLib by Adafruit” while DFPlayer installs “DFRobotDFPlayerMini by DFRobot.”
Additionally, install a font file to display the time in large characters. If you download and unzip the following zip file, you’ll get a file called “FreeSans40pt7b.h.”
Open Arduino IDE’s standard sketch folder, open “libraries”-> “Adafruit_GFX_Library”-> “Fonts” folder, and copy the font file there.
Next, create an alarm clock program in Arduino IDE and write it to ESP32. The contents of the program are shown in Listing 1.
List 1: Alarm clock body program
Programming table(Put here)
However, the 17th to 21st lines need to be rewritten as follows.
Rewrite according to the SSID / password of your Wi-Fi router.
Specify the IP address to be assigned to ESS32. Decide the IP address yourself according to the network configuration of your Wi-Fi router.
In normal IP addresses, four numbers are separated by a period, but in this line, it’s in the form of a function argument, so the four numbers are separated by commas.
Specify the IP address of the default gateway for the network. Usually, it’s the IP address of the Wi-Fi router. Separate the four numbers in the IP address with commas.
Rewrite according to the IP address assigned to ESP32 of the extension unit.
For example, if you want to set it up as shown in Table 4, rewrite lines 17-21 as shown in Listing 2.
|Wi-Fi Routers SSID||my_wifi|
|Wi-Fi Router password||my_password|
|IP address assigned toESP32 body||192.168.1.101|
|Default gateway IP address||192.168.1.1|
|IP address assigned to ESP32 of the extension unit||192.168.1.102|
Graph 4: Example of network settings of the main unit
List 2: Example of rewriting lines 17-21
On the other hand, the extension program looks like List 3.
Rewrite lines 5 to 9 in the same way as the alarm clock itself. However, on the 7th line, specify the IP address assigned to the extension unit. In addition, specify the IP address of the alarm clock in the “Main console IP address” on the 9th line.
List 3: Extension program
After the program has been written to the main console/extension unit, check the operation. The procedure is as follows.
To cancel the alarm setting, connect to the address “http: // IP address of the machine/” with a web browser and click on the “Alarm Off” button.
You can connect to the ESP32 network via Wi-Fi. On top of being able to connect to the internet as with today’s example, you can now connect to a wide variety of electronic work on the network.