from machine import Pin, PWM
import time
# Initialize IR sensors (pull-up likely needed for active-low sensors)
ir_sensor_in = Pin(26, Pin.IN, Pin.PULL_UP) # Assuming active-low (triggered = LOW)
ir_sensor_out = Pin(27, Pin.IN, Pin.PULL_UP) # Same as above
# Motor control pins (L298N driver)
IN1 = Pin(18, Pin.OUT, value=0) # Motor direction control
IN2 = Pin(19, Pin.OUT, value=0) # Motor direction control
ENA = Pin(4, Pin.OUT) # Enable pin (PWM for speed control)
# Configure PWM for speed control (frequency=1000Hz)
speed = PWM(ENA, freq=1000) # Corrected: Use PWM, not PAM
speed.duty_u16(65000) # Speed range: 0 (off) to 65535 (max)
def turn():
IN1.on()
IN2.off()
def stop():
IN1.off()
IN2.off()
while True:
reading_in = not ir_sensor_in.value() # Inverted if sensor is active-low
reading_out = not ir_sensor_out.value()
if reading_in and not reading_out:
turn()
print("Turn")
elif reading_out:
stop()
print("Stop")
else:
stop() # Default state if no sensor is triggered