Help with Python Raspberry Pi DC Motor Control with TB6612

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
onerainywish
 
Posts: 15
Joined: Thu Oct 07, 2021 10:25 pm

Help with Python Raspberry Pi DC Motor Control with TB6612

Post by onerainywish »

Help please! Working on a tight deadline and I would be forever grateful for some help.

Trying to just run a simple micro metal gearmotor dc motor using my Raspberry Pi with Adafruit TB6612. Wanting to use the adafruit libraries, but having a hard time deciphering the documentaiton. Can someone point me in the direction of proper use of the adafruit_motor library to control a dc motor from a raspberry pi? The more concrete your examples the better. Thank you!

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by adafruit_support_carter »

The first step is to install Blinka on the Pi:
https://learn.adafruit.com/circuitpytho ... spberry-pi
This is needed to allow using *any* CircuitPython library. Make sure you are passing the post install test:
https://learn.adafruit.com/circuitpytho ... 3030038-18

Then, the TB6612 guide shows how to install and use the CircuitPython Motor library:
https://learn.adafruit.com/adafruit-tb6 ... cuitpython
It shows how to wire to the Pi and provides a simple example program.

User avatar
onerainywish
 
Posts: 15
Joined: Thu Oct 07, 2021 10:25 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by onerainywish »

Thank you! I did find this, but the provided example shows how to control a stepper motor, but not a dc motor. I am trying to use the TB6612 to control this:

https://www.robotshop.com/uk/6v-1001-15 ... coder.html

Struggling to figure out how to apply to library to that from the examples provided and how to match power supply to the gearmotor. Anything you can do to provide some guidance?

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by adafruit_support_carter »

Here's a DC motor example:
https://github.com/adafruit/Adafruit_Ci ... c_motor.py

Do you already have this hardware (the TB6612)? Or are still trying to determine options?

User avatar
onerainywish
 
Posts: 15
Joined: Thu Oct 07, 2021 10:25 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by onerainywish »

Already went with TB6612 -- wanted to do DRVF8871 but couldn't find them anywhere

Okay so attempting to use this library, but i'm a bit confused about PWM A and B. Can somehow help me understand what these mean WRT the TB6612. Since the 6612 has two PWM inputs -- one for each of the two DC motors...I'm a bit confused by what to do with that. How do I assign these?

Code: Select all

PWM_PIN_A = board.GP28  # pick any pwm pins on their own channels
PWM_PIN_B = board.GP27

# DC motor setup
# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
# cases. A capacitor can be used to help prevent this.
pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50)
pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50)
motor1 = motor.DCMotor(pwm_a, pwm_b)
Here is my full code...but just not sure how to assign PWM a and b?

Code: Select all

import time
import board
import pwmio
from adafruit_motor import motor

PWM_PIN_A = board.D7  # Currently Connected to PWM A on the board
PWM_PIN_B = board.D12 # this is currently connected to AIN1

# DC motor setup
# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
# cases. A capacitor can be used to help prevent this.
pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50)
motor1 = motor.DCMotor(pwm_a)

print("***DC motor test***")

print("\nForwards slow")
motor1.throttle = 0.5
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nForwards")
motor1.throttle = 1.0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("throttle:", motor1.throttle)
time.sleep(1)

print("\nBackwards")
motor1.throttle = -1.0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("throttle:", motor1.throttle)
time.sleep(1)

print("\nBackwards slow")
motor1.throttle = -0.5
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nSpin freely")
motor1.throttle = None
print("  throttle:", motor1.throttle)

print("\n***Motor test is complete***")

User avatar
onerainywish
 
Posts: 15
Joined: Thu Oct 07, 2021 10:25 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by onerainywish »

Found this in the docs here: https://circuitpython.readthedocs.io/pr ... t/api.html
Note

The TB6612 boards feature three inputs XIN1, XIN2 and PWMX. Since we PWM the INs directly its expected that the PWM pin is consistently high.
I have no idea what that means though? How do I choose PWMA and PWMB?

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by adafruit_support_carter »

The TB6612 can drive two motors. If you are using only one, then you can ignore half the pins. For example, just use the MOTORA output. Then you only need to connect to PWMA, AIN1, and AIN2.

Wire PWMA to any available digital pin and set the pin to output and HIGH.

Wire AIN1 and AIN2 to any available PWM pins and specify those here:

Code: Select all

PWM_PIN_A = board.GP28  # pick any pwm pins on their own channels
PWM_PIN_B = board.GP27

User avatar
onerainywish
 
Posts: 15
Joined: Thu Oct 07, 2021 10:25 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by onerainywish »

Super helpful. That makes sense! Last question...I hope...

When defining pins on a Raspberry Pi W or Raspberry Pi 4 using blinka's board library --- how do I do it? Keeps telling me there isn't a GP11 for instance...

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by adafruit_support_carter »

This is a good general reference:
https://pinout.xyz/#

All modern Pi's have the same pinout on the 2x20 header.

In Blinka, use Dx notation. For example:

Code: Select all

board.D23
would be GPIO 23.

User avatar
onerainywish
 
Posts: 15
Joined: Thu Oct 07, 2021 10:25 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by onerainywish »

Yes! But so I define them as board.DXX or board.GPXX or…

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by adafruit_support_carter »

board.DXX

replace XX with the number

ex:

Code: Select all

board.D23

User avatar
onerainywish
 
Posts: 15
Joined: Thu Oct 07, 2021 10:25 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by onerainywish »

Amazing. Extremely grateful for all this support. For some reason I was really stuck on this. Okay. Last question.

How do I use blinka to simply set a GPIO to output and high? Happy to be pointed to the docs on this as well.

User avatar
onerainywish
 
Posts: 15
Joined: Thu Oct 07, 2021 10:25 pm

Re: Help with Python Raspberry Pi DC Motor Control with TB66

Post by onerainywish »

Okay. I figured it out. Thank you for all the help!

Unfortunately there seems to be some discrepancy between the Adafruit Board Library and the Rpi GPIO board reference. But I was able to work around it. Still not totally sure how to accurately define GPIO across the whole Rpi using circuit python, but it's probably my own fault that I'm struggling to understanding.

Anyway -- I was able to get this up and running successfully so I thought I would share:

Code: Select all

import board
import digitalio
import pwmio
from adafruit_motor import motor
import RPi.GPIO as GPIO

# PINOUT
# GRD = 34
# 
PWM_PIN_A = board.D13 # this is currently connected to AIN2 this is PWM1 on the RPi4 AKA GPIO33
PWM_PIN_B = board.D18 # this is currently connected to AIN1 18 this is PWM0 on the RPi4 AKA GPIO12
# Set the motor speed
GPIO.output(23, GPIO.HIGH) # This is connected to PWM on the TB6612 


# DC motor setup
# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
# cases. A capacitor can be used to help prevent this.
pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50)
pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50)
motor1 = motor.DCMotor(pwm_a, pwm_b)


print("***DC motor test***")

print("\nForwards slow")
motor1.throttle = 0.05
print("  throttle:", motor1.throttle)
time.sleep(20)

print("\nStop")
motor1.throttle = 0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nForwards")
motor1.throttle = 1.0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("throttle:", motor1.throttle)
time.sleep(1)

print("\nBackwards")
motor1.throttle = -1.0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("throttle:", motor1.throttle)
time.sleep(1)

print("\nBackwards slow")
motor1.throttle = -0.1
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nSpin freely")
motor1.throttle = None
print("  throttle:", motor1.throttle)

print("\n***Motor test is complete***")

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”