'''write a micropython script to interface 12v DC motor and to rotate
the motor in clockwise direction with full speed for 5sec, stop the motor for 3sec,
and to rotate the motor in anticlockwise direction half speed for 1 sec,
after that stop the motor.'''
from machine import Pin, PWM
import utime
# Motor pins
IN1_PIN = Pin(0, Pin.OUT)
IN2_PIN = Pin(1, Pin.OUT)
PWM_PIN = Pin(2)
# PWM frequency
PWM_FREQ = 1000 # Hz
# Motor speed
FULL_SPEED = 1023
HALF_SPEED = 512
# Rotate motor in clockwise direction at full speed for 5 seconds
IN1_PIN.value(1)
IN2_PIN.value(0)
pwm = PWM(PWM_PIN, frequency=PWM_FREQ)
pwm.duty(FULL_SPEED)
utime.sleep(5)
# Stop the motor for 3 seconds
pwm.duty(0)
utime.sleep(3)
# Rotate motor in anticlockwise direction at half speed for 1 second
IN1_PIN.value(0)
IN2_PIN.value(1)
pwm.duty(HALF_SPEED)
utime.sleep(1)
# Stop the motor
pwm.duty(0)