I want to control 2 motors using my raspberry pi and a motor controller. I strictly followed the instructions from this video: How to connect the TB6612FNG Dual H-Bridge motor dirver to an Arduino and Raspberry Pi - YouTube
but I can’t get it to work.
Here are my components:
-Raspberry Pi 3b+
-Pololu TB6612FNG Dual Motor Driver Carrier
-9V battery (tested and successfully powers both motors)
-2 motors
-Breadbord
Photos of my setup:
Diagram of the controller:
The wires are connected as follows:
VMOT - Battery (+)
GND - Battery (-) / Pin 6
VCC - Pin 1
AO1/2 - Motor 1
BO1/2 - Motor 2
PWMA - Pin 12
AIN1 - Pin 16
AIN2 - Pin 18
STBY - Pin 22
BIN1 - Pin 15
BIN2 - Pin 13
PWMB - Pin 11
My code:
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO.setwarnings(False);
# PWM Frequency
pwmFreq = 100
# Setup pins
GPIO.setup(12, GPIO.OUT) #PWMA
GPIO.setup(18, GPIO.OUT) #AIN2
GPIO.setup(16, GPIO.OUT) #AIN1
GPIO.setup(22, GPIO.OUT) #STBY
GPIO.setup(15, GPIO.OUT) #BIN1
GPIO.setup(13, GPIO.OUT) #BIN2
GPIO.setup(11, GPIO.OUT) #PWMB
pwma = GPIO.PWM(12, pwmFreq) # pin 18 to PWM
pwmb = GPIO.PWM(11, pwmFreq) # pin 13 to PWM
pwma.start(100)
pwmb.start(100)
# Functions
def forward(spd):
runMotor(0, spd, 0)
runMotor(1, spd, 0)
def reverse(spd):
runMotor(0, spd, 1)
runMotor(1, spd, 1)
def left(spd):
runMotor(0, spd, 0)
runMotor(1, spd, 1)
def right(spd):
runMotor(0, spd, 1)
runMotor(1, spd, 0)
def runMotor(motor, spd, direction):
GPIO.output(22, GPIO.HIGH);
in1 = GPIO.HIGH
in2 = GPIO.LOW
if(direction == 1):
in1 = GPIO.LOW
in2 = GPIO.HIGH
if(motor == 0):
GPIO.output(16, in1)
GPIO.output(18, in2)
pwma.ChangeDutyCycle(spd)
elif(motor == 1):
GPIO.output(15, in1)
GPIO.output(13, in2)
pwma.ChangeDutyCycle(spd)
def motorStop():
GPIO.output(22, GPIO.LOW)
## Main
def main(args=None):
print("test")
left(50)
sleep(2)
motorStop()
print("test")
if __name__ == "__main__":
main()
Output:
No motor movement whatsoever