#define BLYNK_TEMPLATE_ID "TMPL3EyBT6SkV"
#define BLYNK_TEMPLATE_NAME "Door Lock"
#define BLYNK_AUTH_TOKEN "5bJryLPkKZUsKTLqY5qRJk3Eue2sdBYy"
from machine import Pin, PWM, I2C
import time
from micropython import const
import ustruct
# Servo Motor Configuration
servo_pin = Pin(12, Pin.OUT) # D1
pwm = PWM(servo_pin, freq=50)
min_duty = const(40) # Adjust these values for your servo
max_duty = const(115)
range_duty = max_duty - min_duty
def move_servo(angle):
duty = min_duty + int(range_duty * angle / 180)
pwm.duty(duty)
# Relay Configuration
relay_pin = Pin(0, Pin.OUT) # D3
def turn_relay_on():
relay_pin.on()
def turn_relay_off():
relay_pin.off()
# I2C LCD Configuration
i2c = I2C(scl=Pin(22), sda=Pin(21)) # SCL on D22, SDA on D21
LCD_I2C_ADDR = const(0x27)
def lcd_init():
i2c.writeto(LCD_I2C_ADDR, b'\x33\x32\x28\x0C\x06')
def lcd_clear():
i2c.writeto(LCD_I2C_ADDR, b'\x01')
def lcd_move_cursor(line, position):
if line == 1:
line_offset = 0x00
elif line == 2:
line_offset = 0x40
else:
return
i2c.writeto(LCD_I2C_ADDR, bytes([0x80 | (position + line_offset)]))
def lcd_write(text):
i2c.writeto(LCD_I2C_ADDR, text)
# Initialize LCD
lcd_init()
lcd_clear()
lcd_write("ESP32 + Servo")
lcd_move_cursor(2, 0)
lcd_write("Relay & LCD")
# Control the servo and relay
while True:
move_servo(90) # Move servo to 90 degrees
turn_relay_on()
lcd_move_cursor(2, 0)
lcd_write("Relay: ON ")
time.sleep(2)
move_servo(0) # Move servo to 0 degrees
turn_relay_off()
lcd_move_cursor(2, 0)
lcd_write("Relay: OFF")
time.sleep(2)