print ("Reverse parking sensor")
#import modules/ library
import hcsr04 #ultrasonic sensor
import ssd1306 #import oled
from machine import SoftI2C,Pin,PWM #PWM=pulse width modulation
from time import sleep
#ultrasonic sensor
ultrasonic sensor= hcsr04.HCSR04(trigger_pin=13,echo_pin=27,echo_timeout_us=500*2*30)
#OLED set up
i2c_oleddisplay=SoftI2C(scl=Pin(22),sda=Pin(21))
oled_width=128
oled_height=64
#use OOP to create the project
oled=ssd1306.SSD1306_I2C(oled_width,oled_height,i2c_oleddisplay)
#buzzer, declare/create the connection
buzzer=PWM(Pin(23,Pin.OUT))
#test ultrasonic sensor,buzzer and OLED
while True:
distance_in_cm=ultrasonic_sensor.distance_cm
distance_in_mm=ultrasonic_sensor.distance_in_mm
print('An object is detected within: ',distance_in_mm)
#to display the distance on OLED
oled.fill(1)
oled.text('Object"',3,0,0) #the 1st 0 is X axis, 2nd 0 is Y axis
oled.text(str( distance_in_cm),5,20,0)
oled, text ('cm', 75,20,0)
oled.text(str( distance_in_mm), 5,40,0)
oled. text ('cm', 75,40,0)
oled.show()
# to determine the distance 50cm or 150cm
if distance_in_cm<50:
for 1 in range(10):
buzzer.init (freq=1703, duty=400)
sleep(0.5)
buzzer, init(freq=1, duty=400)
sleep(0.5)
elif 50 <= distance in cm <150 :
for i in range(10):
buzzer, init (freq-1703, duty=400)I
sleep(0.5)
buzzer.init(freq=1, duty=400)
sleep(1)
else:
buzzer.init(freq=1, duty=0)
sleep(5)
Loading
ssd1306
ssd1306