# simple motor driver
# L293D
# connections
# GP14 --> 1A
# GP15 --> 2A
# GP16 --> 1,2EN
import machine
import utime
ch_1A = machine.Pin(14, mode = machine.Pin.OUT)
ch_2A = machine.Pin(15, mode = machine.Pin.OUT)
# set PWM freqency to 1kHz.
# duty to 0
pwm = machine.PWM(machine.Pin(16))
pwm.freq(1000)
pwm.duty_u16(0)
def motor_driver(duty, direction):
global ch_1A, ch_2A, pwm
duty = int(duty * 65535/100)
pwm.duty_u16(duty)
if duty != 0:
if direction == 0:
ch_1A.value(1)
ch_2A.value(0)
else:
ch_1A.value(0)
ch_2A.value(1)
else:
ch_1A.value(0)
ch_2A.value(0)
# main program
# turn motor in CW direction
while True:
motor_driver(100, 0)
utime.sleep(2)
motor_driver(50, 1)
utime.sleep(2)