print("This program integrates OLED screen with ESP32")
# Credits to https://RandomNerdTutorials.com & program edited accordingly
#1. Import all necessary modules and libraries
from machine import Pin, SoftI2C #SoftI2C kena ada if sda & scl wujud
from utime import sleep
import oled_library #This is oled library
#2. Declare pin on OLED Display
i2c_oled = SoftI2C(scl=Pin(22), sda=Pin(21))
#Initialize the motion parameter
motion = False #Initially there is no movement
#3. Define the OLED basic parameter
oled_width = 128 #based on spec
oled_height = 64 #based on spec
#Create function interrupt handling
#Remarks : This function will be called everytime a motion is detected
def handle_interrupt(pin): #def is for function
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
#Declare input and output pin
#Remark: LED as output and PiR sensor as input
led_red = Pin(12, Pin.OUT) #OUT = Output
pir = Pin(14, Pin.IN) #IN = Input
#5. Set interrupt pin on PIR sensor by using irq() method
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
#6. Time to test! Now we can compare the output of LED when there is motion or no motion
while True: #forever loop
if motion == True: #A motion has been detected
print('WELCOME', interrupt_pin,)
for i in range (10):
led_red.on()
sleep(0.5)
led_red.off()
sleep(0.5)
print('THANK YOU ^__^') # No motion
motion = False
#4. Create an object called "oled"
#Remark: object format "library name.class name"
oled = oled_library.SSD1306_I2C(oled_width, oled_height, i2c_oled)
#5. Lets's test the OLED
#Remark : The font colour is default set to 1 (white)
#Create text to be displayed on OLED screen
oled.text('WELCOME', 30, 30,)
#oled.contrast(255)
sleep(0.5)
oled.text
oled.show() #To display the text
sleep(3)