Vehicle Reverse sensor Alarm system with Ultrasonic sound sensor and Raspberry pi pico


 

# In this Project We will create a Vehicle Reverse Alarm system with Ultrasonic sound sensor(HCSR04)

from machine import Pin# first we import machine module.
import utime# import utime 
import time#import time
echo=Pin(2,Pin.IN)#echo is Pin 2 and assigned as IN Put.
trigger= Pin(3,Pin.OUT)# trigger is Pin 3 and assigned as Out Put
buzzer= Pin(4,Pin.OUT)#buzzer is Pin 4 and assigned as Out put
led_Red = Pin(5,Pin.OUT)#led_Red is connected to Pin 5 and assigned as an Out Put
led_Yellow =Pin(6,Pin.OUT)#led_Yellow is connected to Pin 6 and assigned as an Out Put
led_Blue = Pin(7,Pin.OUT)#led_Blue is connected to Pin 7 and assigned as an Out Put
led_Green = Pin(8,Pin.OUT)#led_Green is connected to Pin 8 and assigned as an Out Put

while True:# infinite loop 
    trigger.value(0)# first keep the Trigger input Low
    utime.sleep_us(2)#providing a 2 microsecond delay
    trigger.value(1)# sending a High value to trigger
    utime.sleep_us(5)keeping the delay of 5 micro second
    while echo.value()==0:
        signaloff = utime.ticks_us()
    while echo.value()==1:
        signalon = utime.ticks_us()
    timepassed = signalon - signaloff
    distance =(timepassed *0.0343)/2# calculating the time to a constant value for conversion
    print("The distance",distance,"cm")# printing the value of distance variable in shell
    utime.sleep(2)# time delay 2 micro second
    
    if distance <=30:#condition checking if distance is equal or less than 30 CM
        led_Red.on()#turning On the Red led
        led_Yellow.on()#turning On the Yellow led
        led_Blue.on()#turning On the Blue led
        led_Green.on()#turning On the Green led
        
        for j in range(50):# Buzzer Beep function
         buzzer.toggle()
         time.sleep_ms(10)
    else:
        buzzer.off()#turning Off the buzzer
        led_Red.off()#turning On the Red led
        
        
    if distance <=40::#condition checking if distance is equal or less than 40 CM
        led_Yellow.on()
        led_Blue.on()
        led_Green.on()
    else:
        led_Yellow.off()
        led_Blue.off()
        led_Green.off()
        
    if distance <= 50::#condition checking if distance is equal or less than 50 CM
        led_Blue.on()
        led_Green.on()
    else:
        led_Blue.off()
        led_Green.off()
        
    if distance <= 60::#condition checking if distance is equal or less than 60 CM
        led_Green.on()
    else:
        led_Green.off()
        
        
    
    
    


Comments

Popular posts from this blog

4 Stage Timer With DS3231RTC Module.

Interfacing KY038 sound sensor with Raspberry pi Pico

Interfacing MQ2 Gas & Smoke Sensor With Raspberry Pi Pico