Motors for raspberry pi robot not working

Hi,

I am working on building a robot, and I am trying to make it so that the motors can be controlled by using keyboard controls, the arrows on the keyboard. However, I have not managed to get it working. I am unsure it this would be because of a problem with my code or a problem with my circuit.
I am using a TB6612FNG Dual H-Bridge motor driver
The part for the camera works, as I am able to get the livestream. However, when I try to use the keys to make the motors drive, in the terminal it responds with As and Bs in brackets, specifically A when I press the top arrow and B when I press the bottom arrow.
I have also included a picture of my circuitry. I have 4 AA batteries in the battery pack, which are each 1.5 V. The pictures aren’t very clear, so here is the connections:
PMWA - Pin 12
AI1 - Pin 16
AI2 - Pin 18
STBY - Pin 22
BI1 - Pin 15
BI2 - Pin 13
PMWB - Pin 11

Here is a transcript of the code that is applicable to the motors:
import curses
from gpiozero import Robot
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server

robot = Robot(left = (18, 16), right = (15, 13))

actions = {
curses.KEY_UP: robot.forward,
curses.KEY_DOWN: robot.backward,
curses.KEY_LEFT: robot.left,
curses.KEY_RIGHT: robot.right,
}

def main(window):
next_key = None
while True:
curses.halfdelay(1)
if next_key is None:
key = window.getch()
else:
key = next_key
next_key = None
if key != -1:
# KEY DOWN
curses.halfdelay(3)
action = actions.get(key)
if action is not None:
action()
next_key = key
while next_key == key:
next_key = window.getch()
# KEY UP
robot.stop()
curses.wrapper(main)


If anyone can let me know what I have done wrong here, that would be great. Thanks.

Hi Willow
Is “cursers” spelt correctly. Shouldn’t it be “cursors” (note “or” instead of “er”). That would be pretty confusing as I believe the syntax would be very sensitive to spelling.

I suggest this as when I enter “RPi cursers” into a search my computer corrects the spelling to “RPi cursors”.
Cheers Bob

Hi Bob,

So that would be in the first line of my code, as well as everywhere else it says cursers? I changed it to cursors, but now none of the code (including the camera) won’t run. It gave me this error message:
Traceback (most recent call last):
File “/home/pi/robotcontrol.py”, line 2, in
import cursors
ModuleNotFoundError: No module named ‘cursors’

To prevent the key character from displaying you need to add a call to noecho(). See: curses — Terminal handling for character-cell displays — Python 3.11.5 documentation.

However, the fact that you see the characters does indicate that the key is being detected. You can match the characters you are seeing to the movement constants (KEY_UP, etc) to confirm that the keys are doing what you think they are doing. You can add simple debug output statements to console to find out how much further the code is getting - at least until you confirm that you are executing the code up to the ‘action()’ line.

(Note: I come from a different language, where that line should be action.value(), not action(). Python might be different, but it’s worth checking.)

You can debug your code by replacing the calls to the robot functions with calls to your own functions that display a message confirming that the action process is being called. If that works OK then you know the problem is in the various robot.whatever functions.

The step after that would then be to find a robot example script that includes a minimum of other code. If that fails then you likely have a wiring problem. For instance: 2. Basic Recipes — GPIO Zero 1.6.2 Documentation

What are the pin connections you are using from the Pi to the driver? It is difficult to identify pin connections from the image, so a list of how you have it wired would be useful. Be sure to include the power and ground connections.

edit/ get() returns the value part of the key-value pair, not the tuple, so you can ignore the comment about a possible problem with the action() line /edit

1 Like

Hi Willow
I could be wrong here. That is just something I picked up on. Could be American spelling but I don’t think so. I flagged it as something to check. Maybe RPi or Python has its own brand of English, wouldn’t surprise me.
Cheers Bob

The people who create these libraries are free to choose whatever strange spelling they feel like, although they will often create variations like this to distinguish their library from the many others that provide similar functionality. There is no obligation to follow any particular language or spelling.

Hi Jeff
Fair enough. I did not look too closely at it. I thought it was part of Python syntax in which case correct spelling would have been essential I think.
Wild goose chase it turns out.
Cheers Bob

Hi Willow,

How did you go with this? Jeff’s post is excellent and should be all you need to get on your feet:

Keen to hear how you are travelling :slight_smile:

Hi James,

Sorry for that late reply. I had a look through those websites, and they were very helpful in aiding me to understand what my code was doing better. All of it looks right, and I tried a few other codes just to try and get the motors moving, but none of them worked so that makes me think I may have wired it incorrectly. The connections on my breadboard are as follows, with the first part being the pin on the motor driver:
VM - positive side of power rail
VCC - positive side of power rail
GND - negative side of power rail
AO1 - motor 1 +
AO2 - motor 1 -
BO2 - motor 2 -
BO1 - motor 2 +
GND - negative side of power rail
PWMA - raspberry pi pin 12
AI2 - pin 18
AI1 - pin 16
STBY - pin 22
BI1 - pin 15
BI2 - pin 13
PWMB - pin 11
red/positive wire from battery pack - positive terminal of power rail
black/negative wire from battery pack - negative terminal of power rail
Raspberry pi pin 1 - positive power rail
Raspberry pi GND - negative power rail

Sorry if my terminology is incorrect, I’m still learning all of this.

Hi Willow, James
Here is where an oscilloscope is useful.
Just a thought, does anyone know the frequency of the PWM signal. If perchance it is too fast the motors will not turn.

I think you need to check firstly that the PWM signal is getting to the H bridge board. Easy with an oscilloscope but without you could use a multimeter. For a PWM signal of 50% duty cycle you should read a DC voltage of half the supply, that is half the RPi supply voltage. If the RPi is 3.3V this will be 1.65V. This voltage will vary from 0 to full supply (3.3V???) depending on duty cycle.
Cheers Bob

I can’t quite reconcile the list above to your image. You could lay out the list by component to make comparison with the image easier. It is also important to be quite explicit whether the MCU connection is to a pin number or a GPIO number: you have indicated pin number, but have you confirmed the Robot library is using pin numbers and not GPIO numbers?

For instance (as far as I can make out):

TB6612FNG:
Pin    Colour    Connection
VM     Green     Battery+            
VCC    Green     Battery+
GND    Red       Battery-
A01              Motor1+
A02              Motor1-
B02              Motor2+
B01              Motor2-
GND    nc
PWMA   Green     RPiGPIO? RPiPin??
AIN2   Black     RPiGPIO? RPiPin??
AIN1   Blue      RPiGPIO?  RPiPin??
STNDBY White     RPiGPIO?  RPiPin??
BIN1   Brown     RPiGPIO?  RPiPin??
BIN2   Purple    RPiGPIO?  RPiPin??
PWMB   Orange    RPiGPIO?  RPiPin??
GND    nc

And then the same thing for the RPi.

Do you have a multimeter? There is sample code available for the TB6612FNG that you can use to drive the motors so you can see that the voltages on AO1/2 and BO1/2 change as expected. You should be able to confirm changes occurring with AIN1/2 and BIN1/2, as well as a DC voltage on PWMA/B that will vary with motor speed. Also to check that VM, VCC, GND and STBY really are at the level they should be.

It actually creates a lot of headaches. It’s better to do it according to the standard.