print('This program is to detect motion from PIR Sensor\n')
print('Date : 21/11/2023')
print('By : AKMAL FAIZ')
#Import all necessary libraries
from machine import Pin
from utime import sleep
from machine import Pin,SoftI2C
import ssd1306 #This is OLED Library
#Pin Declaration (nama parameter mesti kena ada underscore)
led_red = Pin(2, Pin.OUT)
PIR_Sensor = Pin(13, Pin.IN)
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
#Parameter declaration
counter = 1
#Create the object name for sensor with library --> LIBRARYNAME.CLASSNAME
#bagi nama = Import Library Name.Class Name (trigger_pin, echo_pin,)
screen = ssd1306.SSD1306_I2C(width=oled_width, height=oled_height, i2c=i2c_oled) #library name.class name()
#Main Program
screen.fill(1)
screen.text("PIR Motion Sensor",0,10,0) #Text, Column, Colour code
screen.show()
sleep(4)
while True:
status_of_pir = PIR_Sensor.value() #The value will be 0 or 1
if status_of_pir == 1:
print('Hey! Motion detected!')
screen.fill(1)
screen.text("Motion Detected",0,10,0) #Text, Column, Colour code
screen.show()
for u in range (10):
led_red.on()
sleep(0.5)
led_red.off()
sleep(0.2)
print("Blink", counter)
counter = counter+1
else:
print('No motion detected')
screen.fill(1)
screen.text("No Motion Detected",0,10,0) #Text, Column, Colour code
screen.show()
led_red.off()
status_of_pir = 0
led_red.off()
sleep(2)