# Controlling 2 motors with TB6612FNG and Rasp Pi

**URL:** <https://forum.core-electronics.com.au/t/controlling-2-motors-with-tb6612fng-and-rasp-pi/14014>\
**Category:** Uncategorized\
**Created:** [9 May 2022 06:02 UTC](https://forum.core-electronics.com.au/t/controlling-2-motors-with-tb6612fng-and-rasp-pi/14014 "2022-05-09T06:02:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pia201195](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pia201195](https://forum.core-electronics.com.au/u/Pia201195)\
**Post date:** [9 May 2022 06:02 UTC](https://forum.core-electronics.com.au/t/controlling-2-motors-with-tb6612fng-and-rasp-pi/14014/1 "2022-05-09T06:02:23Z")

</div>

Hi there,

I have purchased a TB6612FNG (sparkfun) motordriver. I have been using the link below as a guide

> **[How To Control A Motor with the Raspberry Pi - Tutorial Australia](https://core-electronics.com.au/guides/how-to-control-a-motor-with-the-raspberry-pi/)**
>
> In this tutorial, we are going to connect a Motor to the Raspberry Pi and create a hypnotic self-spinner. We are first going to look at how to wire the Raspberry Pi to the motor controller and the motor. We will then look at using the raspberry pi to...

It only shows how to control one motor. How would I change the script to allow it to run on two motors?

---

<div class="post-metadata">

**Author:** ![Liam](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam/32/3562_2.png) [@Liam](https://forum.core-electronics.com.au/u/Liam)\
**Post date:** [10 May 2022 07:00 UTC](https://forum.core-electronics.com.au/t/controlling-2-motors-with-tb6612fng-and-rasp-pi/14014/2 "2022-05-10T07:00:15Z")

</div>

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:

```auto
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](https://pinout.xyz/) is an amazing resource for your Pi projects!  
Liam
