print("Hello, ESP32!")
print("PROJECT HAND SANITIZER MACHINE")
print("CREATE BY Putsu mangsawat")
# Import all libraries
import ultrasensor_library
import oled_library
from machine import Pin, SoftI2C, PWM
from utime import sleep
# Declare Pin
led_blue = Pin(18, Pin.OUT)
led_red = Pin(5, Pin.OUT)
TRIG = Pin(14, Pin.IN)
ECHO = Pin(27, Pin.OUT)
Buzzer_Pin = Pin(2, Pin.OUT)
pir = Pin(13, Pin.IN)
servoPin = Pin(12, Pin.OUT)
pin_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
# Let's create a name for our OLED screen
# name = library name, class name
screen = oled_library.SSD1306_I2C(width=128, height=64, i2c=pin_oled)
# Declare object name for sensors with libraries
sensor_detected = ultrasensor_library.HCSR04(trigger_pin=TRIG, echo_pin=ECHO)
# Declare object name for servo motor
servo_motor = PWM(servoPin, freq=50)
# Function to move servo to the left
def move_servo_left():
int pos = 0;
void loop() {
for (pos = 0; pos <= 180; pos += 1) {
servo_motor.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo_motor.write(pos);
delay(15);
}
}
# Duty cycle for left position (adjust according to your servo)
servo_motor.duty(40) # Example duty cycle, adjust as needed
sleep(1) # Adjust time as needed
# Duty cycle for stopping the servo
servo_motor.duty(0)
# Main program
while True:
# Ultrasensor part
print('\n=====DISTANCE OF INCOMING OBJECT=====\n')
distance_in_cm = sensor_detected.distance_cm()
print('An object is detected within:', distance_in_cm, 'cm')
# Buzzer part
if distance_in_cm > 20:
print("SANITIZER IS NORMAL")
screen.fill(1)
screen.text("High sanitizer level:/", 10, 20, 0)
screen.show()
led_blue.on()
sleep(0.5)
led_blue.off()
sleep(0.5)
elif 10 <= distance_in_cm < 20:
print("SANITIZER IS LOW")
tone_buzzer = PWM(Buzzer_Pin, freq=1000, duty=50)
sleep(0.05)
tone_buzzer = PWM(Buzzer_Pin, freq=1000, duty=0)
sleep(0.05)
screen.fill(1)
screen.text("Low sanitizer level:/", 10, 20, 0)
screen.show()
led_red.on() # Turn on the red LED when water level is low
sleep(0.2)
led_red.off()
sleep(0.2)
# Move servo to the left when motion is detected
move_servo_left()
sleep(2)
motion = pir.value()
if motion == 1:
print("\n\tPlace a hand\n")
screen.fill(1)
screen.text("Place a hand:/", 10, 20, 0)
screen.text("!!!", 40, 40, 0)
screen.show()
led_blue.on()
sleep(2)
led_blue.off()