print("This program will test PIR sensor")
print('Created by: Ikhwanfahmi')
print('Date: 1/4/2024')
# Import libraries
from utime import sleep
from machine import Pin, PWM, SoftI2C
import library_oled
# Pin declaration
PIR_sensor = Pin(26, Pin.IN)
Red_led = Pin(4, Pin.OUT)
Green_led = Pin(2, Pin.OUT)
Buzzer = PWM(Pin(18, Pin.OUT))
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21))
# Create an object name for component with library
display = library_oled.SSD1306_I2C(width=128, height=64, i2c=oled_pin, external_vcc=False)
# Main program
while True:
Green_led.off() # Initialize green LED
Red_led.off() # Initialize red LED
Buzzer.init(freq=1, duty=0) # Initialize the buzzer
motion_status = PIR_sensor.value() # Read the digital value of the sensor (0 or 1)
print("The value of PIR sensor is", motion_status)
if motion_status:
Green_led.off()
for m in range(5):
Red_led.on()
Buzzer.init(freq=1033, duty=512) # Adjust frequency and duty cycle
sleep(0.4)
Red_led.off()
Buzzer.init(freq=1, duty=0) # Turn off the buzzer
sleep(0.4)
display.fill(0) # Clear display (0 is usually black)
display.text("pencuri", 13, 30, 1) # Show text (1 is white)
display.show()
else:
Green_led.on()
Red_led.off()
Buzzer.init(freq=1, duty=0) # Ensure buzzer is off when no motion is detected
# Display update
display.fill(0) # Clear display (0 is usually black)
display.text("Hey wsup guys", 13, 30, 1) # Show text (1 is white)
display.show()
sleep(2) # System delay
Loading
esp32-devkit-v1
esp32-devkit-v1