Hi there,
I have purchased a TB6612FNG (sparkfun) motordriver. I have been using the link below as a guide
It only shows how to control one motor. How would I change the script to allow it to run on two motors?
Hi there,
I have purchased a TB6612FNG (sparkfun) motordriver. I have been using the link below as a guide
It only shows how to control one motor. How would I change the script to allow it to run on two motors?
Hi Pia,
Welcome to the forum!!
After connecting everything together you’ll have to use 3 other pins to control the B-channel motor.
Something like this should work:
import gpiozero
import time
#Setup pins for motorA
BackwardA = gpiozero.OutputDevice(18) # On/Off output
ForwardA = gpiozero.OutputDevice(23) #On/Off output
SpeedPWMA = gpiozero.PWMOutputDevice(24) # set up PWM pin
#Setup pins for motorB
BackwardB = gpiozero.OutputDevice(<SPARE DIGITAL PIN HERE>) # On/Off output
ForwardB = gpiozero.OutputDevice(<SPARE DIGITAL PIN HERE>) #On/Off output
SpeedPWMB = gpiozero.PWMOutputDevice(<SPARE PWM PIN HERE>) # set up PWM pin
while True:
directionFlagA = input("set motorA direction: ")
if directionFlagA == "back": # if user types "back" change direction of motor
BackwardA.on() # Sets BackwardA Direction pin on
ForwardA.off() # Sets BackwardA Direction pin on
else:
BackwardA.off() # Sets BackwardA Direction off
ForwardA.on() # Sets BackwardA Direction pin on
directionFlagB = input("set motorB direction: ")
if directionFlagB == "back": # if user types "back" change direction of motor
BackwardB.on() # Sets BackwardB Direction pin on
ForwardB.off() # Sets BackwardB Direction pin on
else:
BackwardB.off() # Sets BackwardB Direction off
ForwardB.on() # Sets BackwardB Direction pin on
speedFlagA = float(input("set speedA (between 0-1000): ")) # Gets a number from the from the user
speedFlagB = float(input("set speedB (between 0-1000): "))
SpeedPWMB.value = speedFlag/1000 # Sets the duty cycle of the PWM between 0-1
SpeedPWMA.value = speedFlag/1000 # Sets the duty cycle of the PWM between 0-1
Note that I haven’t included the pins numbers in case you have another project using those pins, pinout.xyz is an amazing resource for your Pi projects!
Liam