DC Motor control using TB6612FNG driver and Raspberry PI gpiozero lib.

In this article, we look at using TB6612FNG motor controller, and the gpiozero python library for  controlling DC motor. TB6612FNG motor driver has widely used for applications where better efficiency and performance is required. Below schematic shows the Raspberry PI zero GPIO connections to the motor controller. TB6612 motor controller can driver two motors simultaneously, but we are using a single motor in this experiment. We are using GPIO BCM6 (pin 31) for PWM to control the speed of the motor. TB6612 takes different voltages for VCC and VM (Motor Voltage), in this example we are using single 5V for both VCC and VM.

We are using BCM13 and BCM19 for controlling motor direction inputs for CW (Clockwise) and CCW rotation. The speed of the motor can be varied by varying the PWM value.

We gpiozero python library is used for accessing Raspberry PI GPIO. There are few other frameworks like RPi.GPIO and wiringpi.

software PWM gpiozero gives more accuracy and efficiency.

PWMOutputdevice class is used for generating PWM on GPIO6 for controlling motor speed, and OutputDevice class is used for changing the state of motor and rotation direction.

TB6612FNG has STBY (standby) input is used to keep motors enable or disable. This we are keeping active high my connecting to VCC.

#    C2Plabs.com 
#
#

from gpiozero import PWMOutputDevice
from gpiozero import DigitalOutputDevice
from time import sleep

# GPIO 6 is used for Generating Software PWM
# GPIO 13 & GPIO 19 are used for Motor control pins as per schematic 


PWM_PIN_MOT1 = 6
IN1_PIN_MOT1 = 13
IN2_PIN_MOT1 = 19

# PWMOutputDevice takes  BCM_PIN number
# Active High 
# intial value
# PWM Frequency
# and Pin_factory which can be ignored

pwm_pin_mot1 = PWMOutputDevice (PWM_PIN_MOT1,True, 0, 1200)

# DigitalOutputDevice take 
# Pin Nuumber
# Active High
#Initial Value

cw_pin_mot1 = DigitalOutputDevice (IN1_PIN_MOT1, True, 0)

ccw_pin_mot1 = DigitalOutputDevice (IN2_PIN_MOT1, True, 0)


def RotateMotorCW():
        print ('Enter Motor speed 0 to 100 percent')
        pwm_percnt = raw_input ()
        pwm_percnt = int (pwm_percnt)
        pwm_percnt = pwm_percnt/100.0
        pwm_pin_mot1.value = pwm_percnt
        cw_pin_mot1.value = 1
        ccw_pin_mot1.value = 0


def RotateMotorCCW():
        print ('Enter Motor speed 0 to 100 percent')
        pwm_percnt = raw_input ()
        pwm_percnt = int (pwm_percnt)
        pwm_percnt = pwm_percnt/100.0
        pwm_pin_mot1.value = pwm_percnt
        cw_pin_mot1.value = 0
        ccw_pin_mot1.value = 1


def StopMotor():
        cw_pin_mot1.value = 0
        ccw_pin_mot1.value = 0
        pwm_pin_mot1.value = 0

def main():

        try:
                while True:
                        RotateMotorCW()
                        sleep(4)
                        RotateMotorCCW()
        except KeyboardInterrupt:
                print ('Interrupted')
        StopMotor()

if __name__ == "__main__":
        main()
                  

Code snippet listed above shows how to use Raspberry Pi gpiozero library for generating software PWM, and to control GPIO pins. There are two functions defined RotateMotorCW and RotateMotorCCW. In each of the functions we accept PWM value of speed using “pwm_percnt = raw_input ()“.

Leave a Reply

Your email address will not be published. Required fields are marked *