import machine
import time
from machine import Pin
# Pin assignments (adjust based on your hardware setup)
pir_sensor1 = Pin(15, Pin.IN) # PIR sensor connected to pin 15
pir_sensor2 = Pin(2, Pin.IN) # PIR sensor connected to pin 2
speaker_pin = Pin(25) # High-frequency speaker connected to pin 25
# Sound frequency configuration
frequency = 18000 # frq that humans can hear; Set desired high frequency in Hz
# frequency = 30000 # frq that monkeys can hear
# Function to generate sound wave
def play_sound():
pwm = machine.PWM(speaker_pin)
pwm.freq(frequency) # Set the frequency
pwm.duty(512) # Set duty cycle to 50% for maximum volume
time.sleep(5) # Play sound for 5 seconds
pwm.deinit() # Stop sound
# Main loop
while True:
if pir_sensor1.value() or pir_sensor2.value(): # Detect motion
play_sound() # Play sound to repel monkeys
time.sleep(0.1) # Check for motion every 0.1 seconds