Adafruit Motor hat error

I had both the motor and servo hats working well on a pi 3B+ so that I had keyboard control for a little FPV rover.

Now I can get the servo hat to work but I can’t get the motor hat to work on a couple of pis that I have. I have the latest Buster distros installed, updated, installed the adafruit blinka and hat libraries.

Powering the pi and the hats separately.

1 Like

kindly share full code, where is start giving error

1 Like

I’m using the code of the adafruit tutorial, you can see the last line that gives the error in the pic

1 Like

Traceback errors have to be read from bottom up. So the actual error is on the first line, which pertains to Python 3.7.

Please check your python version. Default Python version in Buster is 2.x. The program calls for version 3.x.
Install Python 3.7 and try
To install python
Try python 3.7 instead of general version 3
The latest is 3.9. I do not know the compatibility with Adafruit program

1 Like

No it is running.3.7

1 Like

Just curious: Was I2C enabled on the Pi’s where the motorHAT is not working?

This is the code from (lines 40 to 62 included) Lines 40 and 58 as in error code are in bold.

class DCMotor:
    **"""DC motor driver. ``positive_pwm`` and ``negative_pwm`` can be swapped if the motor runs in**
**    the opposite direction from what was expected for "forwards".**

    Motor controller recirculation current decay mode is selectable and defaults to
    ``motor.FAST_DECAY`` (coasting). ``motor.SLOW_DECAY`` is recommended to improve spin
    threshold, speed-to-throttle linearity, and PWM frequency sensitivity.

    Decay mode settings only effect the operational performance of controller chips such
    as the DRV8833, DRV8871, and TB6612. Either decay mode setting is compatible
    with discrete h-bridge controller circuitry such as the L9110H and L293D; operational
    performance is not altered.

    :param ~pwmio.PWMOut positive_pwm: The motor input that causes the motor to spin forwards
      when high and the other is low.
    :param ~pwmio.PWMOut negative_pwm: The motor input that causes the motor to spin backwards
      when high and the other is low."""

    **def __init__(self, positive_pwm: PWMOut, negative_pwm: PWMOut) -> None:**
        self._positive = positive_pwm
        self._negative = negative_pwm
        self._throttle = None
        self._decay_mode = FAST_DECAY

The second traceback error is to import the

def _motor(self, motor_name, channels, stepper_name):
        **from adafruit_motor import motor  # pylint: disable=import-outside-toplevel**

**        motor_name = "_motor" + str(motor_name)**
**        stepper_name = "_stepper" + str(stepper_name)**
        if not getattr(self, motor_name):
            if getattr(self, stepper_name):
                raise RuntimeError(
                    "Cannot use {} at the same time as {}.".format(
                        motor_name[1:], stepper_name[1:]

The topmost traceback is for Motor 1

    def motor1(self):
        """**:py:class:``~adafruit_motor.motor.DCMotor`` controls for motor 1.**
**        The following image shows the location of the M1** terminal on the DC/Stepper FeatherWing.
        The label on the FeatherWing is found on the bottom of the board.
        The terminal is labeled on the top of the Shield and Pi Hat.
        .. image :: ../docs/_static/motor_featherwing/m1.jpg
          :alt: Motor 1 location
        This example moves the motor forwards for one fifth of a second at full speed.
        .. code-block:: python
            import time
            from adafruit_motorkit import motorkit
            kit = MotorKit()
            kit.motor1.throttle = 1.0
            kit.motor1.throttle = 0
        **return self._motor(1, (8, 9, 10), 1)**

Basically, when the script looks for motor 1, and calls in for the parameters, it could not find the definition for the PWM. Read somewhere the I2C, if not enabled, can cause these issues. Adafruit MotorHat requires I2C to be enabled. If it works on some of the Pi’s and not in others, is it possible that it is not enabled inadverently?

If this does not help, someone with more experience with Python and this specific product’s troubleshooting can help

1 Like

I2c is enabled.

It worked last year, I’m trying on 2 pis now and I’ve done everything needed. The servo hat works, the motor hat is returning this error.

I’m following the bouncing ball to test the motors from the tutorial.

1 Like

More complete error, happens on both the 3B+ that it used to work on last year and the new 02W I got to shrink down the build and power use by the pi

Hi Bridget,

Just to confirm running the command, in the CLI

sudo apt-get install python-smbus
sudo apt-get install i2c-tools
sudo i2cdetect -y 0

With just the Motor HAT it should give us an address of 0x96

Reading through the docs here: Adafruit_CircuitPython_Motor/ at main · adafruit/Adafruit_CircuitPython_Motor · GitHub
In the initalisation sequence it doesnt look like pwmio was imported.

Reinstalling CircuitPython using this guide from Adafruit should get you out of trouble

Let us know how you go!

I did all that as part of my pi setup, I have copied it all in to one reference file on my PC that I go through when I get a pi ready to play with. It passed the blinkatest above.

I redid the adafruit circuitpython thing from the guide everything was already the latest version, missed getting a screenshot, and I am getting the same errors.

1 Like

This was the solution from the adafruit forums. Circuit python and blinka was installed the other day, but somehow this was still needed.

pip3 install --upgrade adafruit-blinka

1 Like

Hi Bridget,

Very interesting! Just double-checking, were you able to get the motor HAT up and running again?

I’m keen to see what you make!

Yep I had installed Blinka a day or two prior, but somehow the update fixed something and now the motor hat works.

Drove a little rover in to my kitchen and back from my PC was using a preview camera window and curses to drive and skid steer. Got a chassis with 4 motors and 3d printed a better top for it to put components in.

I have a motor and a servo hat, so ideally I’d have a front facing camera for driving and another on pan/tilt for looking around. Maybe some sensors.

At the moment my Python is pretty basic, I can’t make a GUI yet and I want to make the controls better, and work towards some automation and mapping but I am struggling.


Hi Bridget,

That sounds siiick! I’d be keen to see some photos of your setup :smiley:

1 Like

Hi Bridget,

Thanks for confirming that’s great to hear! I too would love to see some photos if you have a moment!

Sounds awesome!

Just a mess of components and shitty code at the moment.

Got a lipo and a powerboost 1000c powering the pi 02W, and 5 x rechargeable AAs powering the motors.

I really need to get in to the coding, probably only invested a few hours tops over the last 12 months hence not getting very far.

1 Like