from machine import Pin, I2C, PWM
from ssd1306 import SSD1306_I2C
import time
# PIR sensor setup
pir_pin = Pin(4, Pin.IN) # Assuming PIR sensor connected to D2
# I2C LCD SSD1306 setup
i2c = I2C(0, scl=Pin(5), sda=Pin(4)) # Assuming I2C Pins are D1 (SDA) and D0 (SCL)
lcd = SSD1306_I2C(128, 64, i2c)
# Servo motor setup
servo_pin = Pin(17) # Assuming servo connected to D5
servo = PWM(servo_pin, freq=50)
# Variables
count = 0
max_count = 4
# Function to rotate servo by specified angle
def rotate_servo(angle):
duty = angle / 18 + 2
servo.duty(duty)
try:
while True:
# Check PIR sensor
if pir_pin.value() == 1:
count += 1
lcd.fill(0)
lcd.text("Count: {}".format(count), 0, 0)
lcd.show()
time.sleep(0.5)
# If count reaches 4, stop detecting further movements and rotate servo
if count >= max_count:
rotate_servo(90)
break
except KeyboardInterrupt:
pass
finally:
# Turn off servo and display final count on LCD
servo.duty(0)
lcd.fill(0)
lcd.text("Final Count: {}".format(count), 0, 0)
lcd.show()