Part 5 – Moving two motors
Now that my little demo microPython program moving one N20 motor goes well, I am now upgrading it to move two motors. Now I have a problem of mixing up the getting complicated jumper wires, and therefore find it difficult to do troubleshooting. So I need to do some proper documentation, starting with the photo below.spv2_wiring_2021aug17061024×480 185 KB
Now the time has come to use Pico MicroPython to move two motors. The following program is tested OK.
# Program Name
# move_two_motors_v05.py - tlfong01 2021aug17hkt1607
# Reference
# Pi-Top Forum - making-a-rpi-pico-based-smart-vehicle/924
# https://forum.pi-top.com/t/making-a-rpi-pico-based-smart-vehicle/924
#
# Configuration
# Acer Intel COIRE i5 PC Chinese Windows 10 (64-bit), Thonny 3.3.3, Python 3.7.9 (32-bit), Tk 8.6.9, USB COM Port #4
#
# Thonny MicroPython Intepreter
# Micropython (Rapsberry Pi Pico)
# DC Motor
# N20 1:48 6V DC Gear Motor with quadrature encoder signals A, B
#
# DC Motor Driver
# TB6612FNG Dual DC Motor Driver
# Program Function
# Move Two DC motors forward, backward, and stop
# User Guide
# (a) Connect PWMA (purple), PWMB (purple) to High (Vcc)
# (b) Connect Standby (brown) to High (Vcc)
# (c) Pico GP 10, 11 (Blk, Bwn) to Motor Driver AIN1, AIN2 (Yel, Blu)
# (d) Pico GP 12, 13 (Red, Orn) to Motor Driver BIN1, BIN2 (Yel, Blu)
# (c) Run program to move motor forward, backward, and stop
import utime
from machine import Pin
motorDict01 = {
'1': {'IN1': 10, 'IN2': 11},
'2': {'IN1': 12, 'IN2': 13},
'3': {'IN1': 0, 'IN2': 0},
'4': {'IN1': 0, 'IN2': 0},
}
motorDictDict = {
'1': motorDict01,
'2': motorDict01,
}
def setupMotor(motorDictNum, motorNum):
motorDict = motorDictDict[str(motorDictNum)]
motorIn1 = Pin(motorDict[str(motorNum)]['IN1'], Pin.OUT)
motorIn2 = Pin(motorDict[str(motorNum)]['IN2'], Pin.OUT)
motorInList = [motorIn1, motorIn2]
return motorInList
def moveMotorForward(motorDictNum, motorNum):
motorInList = setupMotor(motorDictNum, motorNum)
motorInList[0].high()
motorInList[1].low()
return motorInList
def moveMotorBackward(motorDictNum, motorNum):
motorInList = setupMotor(motorDictNum, motorNum)
motorInList[0].low()
motorInList[1].high()
return motorInList
def moveMotorStop(motorDictNum, motorNum):
motorInList = setupMotor(motorDictNum, motorNum)
motorInList[0].low()
motorInList[1].low()
return motorInList
# *** Tests ***
moveMotorForward(motorDictNum = 1, motorNum = 1)
utime.sleep(1)
moveMotorBackward(motorDictNum = 1, motorNum = 1)
utime.sleep(1)
moveMotorStop(motorDictNum = 1, motorNum = 1)
moveMotorForward(motorDictNum = 1, motorNum = 2)
utime.sleep(2)
moveMotorBackward(motorDictNum = 1, motorNum = 2)
utime.sleep(2)
moveMotorStop(motorDictNum = 1, motorNum = 2)
# *** End of program ***
# *** Sample Output - tlfong01 2021jul01hkt1707
'''
'''
/ to continue, …
Categories: Uncategorized