print("This project is about ESP32 with ultrasonic")
#import all libraries
import HCSR04
import ssd1306
import tm1637
from machine import Pin, SoftI2C, PWM
from utime import sleep
#Pin declaration
#Create object called utrasonic sensor using OOP
#OOP format is--> library name. Class name()
ultrasonic_sensor = HCSR04.HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=500*2*30)
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
tm = tm1637.TM1637(clk=Pin(5), dio=Pin(12))
buzzer = PWM(Pin(19), Pin.OUT)
led_red = Pin(2, Pin.OUT)
oled_width = 128 #based on spec
oled_height = 64 #based on spec
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_oled)
while True:
object_detect_in_cm = ultrasonic_sensor.distance_cm()
object_detect_in_mm = ultrasonic_sensor.distance_mm()
#Display the object distance on seriel monitor/terminal
print("\nDistance in cm: ",object_detect_in_cm)
print("\n")
print("Distance in mm: ",object_detect_in_mm)
print("............................\n")
oled.fill(1)
oled.text('Jarak:', 1, 10, 0)
oled.text(str(object_detect_in_cm), 20, 20, 0)
oled.text(" cm", 85, 20, 0)
#oled.text('Distance in mm:', 1, 30, 0)
oled.text(str(object_detect_in_mm), 35, 40, 0)
oled.text(" mm", 65, 40, 0)
#oled.text('', 38, 50, 0)
sleep(2)
oled.show()
if object_detect_in_cm < 175:
buzzer.init(freq=1, duty=0)
else:
for i in range(20):
# Tornado siren pattern
buzzer.init(freq=500, duty=512) # Start with a high-pitched tone
sleep(0.1)
led_red.on()
sleep(0.1)
buzzer.init(freq=100, duty=512) # Transition to a lower-pitched tone
sleep(0.1)
led_red.off()
sleep(0.1)
buzzer.init(freq=500, duty=512) # Return to high-pitched tone
sleep(0.1)
led_red.on()
sleep(0.1)
buzzer.init(freq=100, duty=512) # Transition to lower-pitched tone
sleep(0.8) # Maintain lower-pitched tone for longer duration
led_red.off()
sleep(0.8)