Time Controlled Switch using ESP32 Web Server and DS3231 RTC

Esp32 Time Based Controlling System

Time Controlled Switch:

Time Controlled Switch using ESP32 Web Server and DS3231 RTC- With the help of this circuit and the Time Controlled ESP32 Web Server, now, you can convert any Switch or a relay into a Time Controlled Switch or a Time controlled relay. In fact you can convert any device into a time controlled device.

Time Controlled Switch using ESP32 Web Server and DS3231 RTC

With this system, you can not only control small loads on time basis but you can also control large loads like, Water Pumps, Heaters, Refrigerators, Street lights, Generators, Door Locks, Blinds, in fact you can automatically turn ON or turn OFF any electrical load of your choice for a specific time duration. All you need is to set the Start Time and Stop Time and then leave everything else to the controller.

For this project, you will need an ESP32 WiFi + Bluetooth module, DS3231 RTC “Real-Time Clock”, and a 5V SPDT type relay. 

Amazon Links:

ESP32 WiFi+Bluetooth Module (Recommended)

DS3231 RTC

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

What is a DS3231 RTC?

ds3231 rtc real time clock module for Time Controlled

The DS3231 is a highly accurate, low-power RTC module that provides precise timekeeping and calendar functions. It has built-in temperature compensation and a battery backup, ensuring accurate timekeeping even during power outages.

Pinout Description

The DS3231 module usually comes with six pins:

Pin Description
VCC Power supply input (typically 3.3V to 5.5V)
GND Ground connection
SDA Serial Data Line for I2C communication
SCL Serial Clock Line for I2C communication
SQW Square Wave/Interrupt Output
32K 32KHz Output

Key Specifications

Specification Detail
Voltage Supply 3.3V to 5.5V
Current Consumption Less than 500μA
Timekeeping Accuracy ±2ppm at 0°C to +40°C
Battery Backup Current 3μA at 3V
Temperature Range -40°C to +85°C
Memory 236 bytes of battery-backed SRAM
Interface I²C Serial Interface (400kHz Max)
Alarm Function Dual programmable alarms
Oscillator Integrated TCXO

32.768 kHz

esp32 dev board for Time Controlled

As usual, I am using my newly designed ESP32 Development board, because it already has most of the components needed for this project like for example a 5V relay, the ESP32 WiFi + Bluetooth module itself, and most importantly the 5V and 3A power supply. Since, this is a development board and I use it for making and testing my ESP32 based projects, so that’s why I have soldered female headers on the Left and Right sides of the ESP32 WiFi + Bluetooth module so that I can connect other sensors and breakout boards.

ds3231 with esp32 for Time Controlled

For this particular project, I have connected the DS3231 RTC “Real-Time Clock”. So, if you want to make the same Development board then you can watch my previous video available on my YouTube channel “Electronic Clinic” or you can read my previous article on the ESP32 Development board.

Anyway, you can also do the same exact connections on a breadboard. Let’s quickly go through all the connections.

Time Controlled Switch Circuit:

Time Controlled esp32 and rtc circuit diagram

Connect the VCC and GND pins of the DS3231 to the ESP32 3.3V and GND. Connect the SDA and SCL pins of the DS3231 to the ESP32 GPIOs 21 and 22.

The 5V SPDT “Single Pole and Double throw” relay input is connected to the ESP32 GPIO 13.

esp32 Time Controlled web server testing

There is no need to worry at all when you see this interface because you don’t have to design this page manually on your laptop or computer. All the code for this page is running on the ESP32 WiFi + Bluetooth module because we are using the ESP32 as a Web Server. Although this is a very basic interface, if you know the HTML coding, you can design yourself an amazing interface by modifying the code, I am about to share with you guys.

Anyways, this is the program of the ESP32 Web Server; you can modify this program according to your needs. I won’t go into too much detail because HTML coding is an entirely different field in itself. But, I am sure you would be able to make minor changes; once you read this code, you will get an idea.

Time Controlled Switch ESP32 Web Server Program:

Anyway, If this is your first time using the ESP32 WiFi + Bluetooth module then you will also need to install the ESP32 board in the Arduino IDE. For this you can read my getting started article on the ESP32 WiFi + Bluetooth Module.

Next, you will also need to install the RTClib library. For this simply copy the library name then go to the Sketch Menu, then to Include Library, and click on the Manage Libraries.

Library installation for esp32 Time Controlled

Paste the library name in the search box.

rtclib library installation for Time Controlled

You can see I have already installed this library. 

Finally, you can upload this program; in my case I have already uploaded this program.

While the ESP32 is still connected to your Laptop or computer, open the Serial Monitor and Copy the IP Address. Then, you can disconnect the ESP32 WiFi + Bluetooth Module and get it installed near the Load you want to control.

Practical Demonstration:

For the practical demonstration, I am going to control a 110/220Vac bulb.

esp32 and AC 220 bulb Time Controlled


Anyway, remember safety first, When the 110/220Vac supply is connected, never touch the relay contacts as it can be extremely dangerous. It is important to note that when working with mains voltage, proper safety precautions should always be taken and it is advisable to consult relevant electrical codes and standards.

Right now, the ESP32 WiFi + Bluetooth module, my Laptop, Tablet, and my cell phone all are connected to the WiFi router.

Accessing esp32 web server Time Controlled project on mobile desktop and tablet

I can use any of these to wirelessly open the ESP32 Web Server. There is no need for these devices to be physically connected.  Anyway, first let’s start with the Laptop.

All I need is to type or paste the IP Address. The interface will open in no-time.

set time to on and off the ac 220 bulb using esp32 Time Controlled

To turn on a Load, first, set the Start Time and then set the Stop Time. This is the time that defines for how long the device will remain ON or Active.

esp32 webserver Time Controlled project

As you can see, the Bulb just turned ON. Now, it will remain ON for exactly 1 minute.

control Ac 220 bulb from esp32 webserver using Time Controlled

Amazing, the bulb just turned OFF.

Next, I also used my Cell phone and tablet. For the practical demonstration, watch the video tutorial given below.

So, guy’s that’s all about the Time controlled Switch/Relay using ESP32 Web Server.

Watch Video Tutorial:


Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button