Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Smart Alarm Clock with I2C LCD Display and Bluetooth Connectivity

View and Edit Project In Cirkit DesignerImage of ESP32-Based Smart Alarm Clock with I2C LCD Display and Bluetooth Connectivity

Circuit Documentation

Summary

This circuit is designed around the ESP32 microcontroller, which is a versatile and powerful chip with a wide range of I/O capabilities. The circuit includes a 7805 voltage regulator to provide a stable 5V supply, a real-time clock (RTC) module (DS3231) for timekeeping, a 4-channel 5V relay module for controlling external devices, and a buzzer for audible alerts. Additionally, the circuit features an LCD screen with I2C interface for display purposes, a step-down power converter to convert 12V to 5V, and various resistors for current limiting and pull-up/pull-down purposes. An AC supply is used to provide power to the circuit, and a pushbutton and rocker switch are included for user input. The HC-05 Bluetooth module allows for wireless communication. The circuit is designed to be interfaced with a phone, although the phone's specific role is not detailed in the provided information.

Component List

Wiring Details

ESP32 (30 pin)

7805

Resistor

RTC DS3231

Relay 4 Channel 5v

Buzzer

12v to 5v Step Down Power Converter

LCD screen 16x2 I2C

AC Supply

Pushbutton

Rocker Switch (SPST)

HC-05 Bluetooth Module

Documented Code

Main Program (main.ino)

from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from DS3231 import DS3231
import time

I2C_LCD_ADDR = 0x27
totalRows = 2
totalColumns = 16

i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)  # initializing the I2C method for ESP32

lcd = I2cLcd(i2c, I2C_LCD_ADDR, totalRows, totalColumns)
ds = DS3231(i2c)
ds.alarm1(match=ds.AL1_EVERY_S)

buzzer = Pin(4, Pin.OUT)

def get_time():
    time_now = ds.datetime()
    hour = time_now[4]
    minute = time_now[5]
    sec = time_now[6]
    tim = ""
    if hour < 10:
        tim = tim + "0"
    tim = tim + str(hour) + ":"
    if minute < 10:
        tim = tim + "0"
    tim = tim + str(minute) + ":"
    if sec < 10:
        tim = tim + "0"
    tim = tim + str(sec)
    return tim
    
def set_time(hour, minute):
    datetime = (2000, 1, 1, hour, minute, 0, 0)
    ds.datetime(datetime)
set_time(23, 1)
ds.alarm2((2, 23, 2), match=ds.AL2_MATCH_HM)
while True:
    if ds.check_alarm(1) == 1:
        time_now = get_time()
        print(time_now)
        lcd.clear()
        lcd.putstr(time_now)
    if ds.check_alarm(2) == 1:
        print("alarm")
        while True:
            buzzer.value(1)
            time.sleep(1)
            buzzer.value(0)
            time.sleep(1)

This code initializes the I2C interface for the ESP32 and sets up the LCD and RTC modules. It defines functions to get and set the time and continuously checks for alarms. When an alarm is triggered, it displays the time on the LCD and activates the buzzer.

LCD API (LCD.cpp)

This file provides an API for interfacing with an HD44780 compatible character LCD. It defines the command set and functions for controlling the LCD, such as clearing the display, moving the cursor, and writing characters or strings to the display.

I2C LCD (I2clcd.cpp)

This file implements the LCD API for an HD44780 character LCD connected via a PCF8574 I2C expander. It includes functions for initializing the LCD, writing commands and data, and controlling the backlight.

DS3231 RTC (DS3231.cpp)

This file contains the driver for the DS3231 RTC module. It provides functions for setting and getting the date and time, configuring alarms, enabling or disabling the square wave output, and checking the oscillator stop flag for time validity.

(Note: The code files are provided as Python scripts but are named with .cpp and .ino extensions, which are typically associated with C++ and Arduino code. The actual code content is written in Python, which suggests that the file extensions may be incorrect or that the code is intended for use with MicroPython or a similar Python interpreter for microcontrollers.)