Originally published by Dec 3, 2019
This article was translated to English, and was originally published for deviceplus.jp.
Table of Contents
Introduction
About the ESP32 alarm
“RTC” for making the date and time tick
Date and time displayed on ESP32 LCD
Make an audible alarm with the MP3 module
Wiring the alarm clock body
Extension wiring for ESP32 alarm clock
Installing library and font files
Arduino IDE alarm clock program
Extension program
Checking operations
Conclusion
Related Articles
Introduction
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
Name
Seller
Price
ESP32-DevKitC(2 pc)
Mouser
About $10.00
DS3231 Module
Amazon
About $3.00
LIR2032 (Button charger)
Amazon
About $3.60
Speaker
Amazon
About $1.00~$3.00
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.
About the ESP32 alarm
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.
To place the main unit and extension away from each other, use the ESP32 Wi-Fi function. The two ESP32s operate as web servers and clients, and communicate with each other as shown in Figure 1.
Fig. 1 Transmission between the body and extension
“RTC” for making the date and time tick
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, as 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 it’s rechargeable.
Pic. 1 DS3231 Module
Date and time displayed on ESP32 LCD
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.
7-segment LED
LED matrix
OLED display
Character LCD display
Graphic LCD display
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.
Pic. 2 ILI9341 2.8 inch LCD display for controller
Make an audible alarm with the MP3 module
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.
Pic. 3 DFPlayer Mini
Wiring the alarm clock body
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.
Fig. 2 Wiring for alarm clock body
ESP32 pin
LCD display pin
5V
VCC
GND
GND
5
CS
4
RESET
2
DC
23
MOSI
18
SCK
19
MISO
Table 1: Connection between ESP32 and LCD
ESP32 pin
DS3231 pin
5V
VCC
GND
GND
21
SDA
22
SCL
Table 2: ESP32 and DS3231 connection
ESP32 pin
DFPlayer Mini pin
5V
VCC
GND
GND
16
TX
17
RX
Table 3: ESP32 and DFPlayer Mini connection
Extension wiring for ESP32 alarm clock
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.
Fig. 3 Extension circuit
Installing library and font files
When you finish working on the circuits, you can create the program. First, start with installing each of the below libraries.
Adafruit GFX
Adafruit ILI9341
RTCLib
DFRobotDFPlayerMini
The installation procedure is as follows.
Start up Arduino IDE.
Select “Sketch”-> “Include Library”-> “Manage Library” menu to open the Library Manager.
Enter “Adafruit GFX” in the “Filter search” field.
Adafruit GFX will be displayed in the library list. Click on the “Install” button (Figure 4).
Install each library in the same way.
There are several RTCLib and DFPlayer libraries with similar names. RTCLib installs “RTCLib by Adafruit” while DFPlayer installs “DFRobotDFPlayerMini by DFRobot.”
Fig. 4 Adafruit GFX library installation
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.
https://www.h-fj.com/deviceplus/font.zip
Arduino IDE alarm clock program
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.
・Line 17/18
Rewrite according to the SSID / password of your Wi-Fi router.
・Line 19
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.
・Line 20
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.
・Line 21
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.
Item
Setting value
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
Table 4: Example of network settings of the main unit
List 2: Example of rewriting lines 17-21
Extension program
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
Programming table
Checking operations
After the program has been written to the main console/extension unit, check the operation. The procedure is as follows.
Prepare a microSD card of 32GB or less and create a folder called “mp3.”
Prepare the MP3 file you want to use for the alarm sound, name it “0001.mp3,” and save it in the mp3 folder.
Insert the microSD card into DFPlayer Mini.
Start up the main console and extension unit.
After initialization and Wi-Fi is finished setting up, the current date and time will be displayed on the main console.
Start a web browser on a computer on the same network as the main console/extension unit, and connect to the address, “http: // IP address of the main unit/.”
The alarm setting form will be displayed on the web browser as shown in Photo 4. Specify the time and click on the “Alarm Settings” button.
Pic. 4 Alarm settings
The time for when the alarm will go off is set at “Alarm set to 10:00:00.”
Confirm that the main console sounds at the time the alarm is set.
Confirm that the LED of the extension unit lights at the time when the alarm is set.
Check that the alarm stops by pressing the switch on the handset.
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.
Conclusion
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 array of electronic work on the network.
Related Articles
Want to keep exploring the capabilities of Arduino? Check out some related articles:
USB Volume Control with Arduino
DIY Guide to Setting Up an LCD with Arduino
Build a Robot with Arduino
Top 6 DIY Projects You Can Do to Expand Your IoT Projects
The post Let’s make a “Wake Up Alarm Clock” with ESP32 and RTC! appeared first on Device Plus.