from machine import Pin, ADC
from time import sleep, ticks_us
from wokwi_lcd2004 import LCD2004 # Import the parallel LCD library
# Ultrasonic sensor setup
trigger = Pin(16, Pin.OUT)
echo = Pin(17, Pin.IN)
# Buzzer setup
buzzer = Pin(18, Pin.OUT)
# Temperature sensor setup
temp_sensor = ADC(Pin(26)) # Using GP26 (ADC0) for the analog temperature sensor
# LED setup for temperature alert
led = Pin(19, Pin.OUT)
# LCD setup with parallel connections to the ESP32 pins
lcd = LCD2004(
rs=Pin(12), # Register Select (RS)
rw=Pin(13), # Read/Write (RW)
e=Pin(14), # Enable (E)
d4=Pin(32), # Data Pin 4
d5=Pin(33), # Data Pin 5
d6=Pin(25), # Data Pin 6
d7=Pin(26) # Data Pin 7
)
# Thresholds
WATER_LEVEL_THRESHOLD = 10 # cm
TEMP_THRESHOLD = 30 # degrees Celsius
# Function to measure distance using ultrasonic sensor
def get_distance():
# trigger pulse
trigger.low()
sleep(0.02)
trigger.high()
sleep(0.00001) # 10 microseconds pulse
trigger.low()
# Wait for echo response, add error handling
signal_off = signal_on = 0
timeout = 30000 # microseconds timeout to avoid infinite loop
while echo.value() == 0:
signal_off = ticks_us()
if ticks_us() - signal_off > timeout:
return None # Timeout error
while echo.value() == 1:
signal_on = ticks_us()
if ticks_us() - signal_on > timeout:
return None # Timeout error
# Calculate pulse duration and distance
time_passed = signal_on - signal_off
distance = (time_passed * 0.0343) / 2 # convert time to distance (cm)
return distance
# Function to read temperature in Celsius
def read_temperature():
analog_value = temp_sensor.read_u16() # Using 16-bit ADC for accuracy
voltage = (analog_value / 65535) * 3.3 # Convert to voltage based on 3.3V
temperature = (voltage - 0.5) * 100 # Adjust based on sensor specs
return temperature
# Function to control the buzzer
def trigger_buzzer(state):
buzzer.value(state) # state is 1 for ON and 0 for OFF
# Function to blink LED for temperature alert
def blink_led():
led.value(1)
sleep(0.5)
led.value(0)
sleep(0.5)
# Main loop
while True:
# Measure water level
distance = get_distance()
if distance is not None:
# Check water level and control buzzer
if distance <= WATER_LEVEL_THRESHOLD:
print(f"Water level: {distance:.1f} cm - ALERT!")
trigger_buzzer(1) # turn the buzzer ON
else:
print(f"Water level: {distance:.1f} cm")
trigger_buzzer(0) # turn the buzzer OFF
else:
print("Error: Ultrasonic sensor timeout")
# Measure temperature
temperature = read_temperature()
if temperature >= TEMP_THRESHOLD:
print(f"Temperature: {temperature:.1f} °C - ALERT!")
blink_led() # Blink the LED if temperature is too high
else:
print(f"Temperature: {temperature:.1f} °C")
led.value(0) # Ensure LED is off when temperature is normal
# Update LCD display
lcd.clear() # Clear the display before updating
lcd.set_cursor(0, 0) # Move cursor to the first line
lcd.print(f"Temp: {temperature:.1f}C") # Display temperature
lcd.set_cursor(0, 1) # Move cursor to the second line
lcd.print(f"Water: {distance:.1f}cm" if distance is not None else "Water: Err") # Display water level
# Delay of 1 second between readings
sleep(1)