Wednesday, 1 June 2016

getting more out of an Adafruit DC & Stepper Motor HAT

As described in an earlier post, this HAT is really only doing stepper motor control as an afterthought, so limited the step rate to just under 80 per second.

However after rummaging around in the code while developing a driver for my telescope mount, I noticed that the Adafruit code is pretty basic and makes no attempt at optimisation.

With some simple changes to the Adafruit code I have managed to improve the step rate to around 280 per second peak. This also means that there is less jitter in the pulse timing as well.

Here are a couple of graphs of the outputs as seen from inside the python code (so the physical signals may vary from this).

This graph is an overview run with one stepper running at about 20 steps per second. I then start the other stepper fairly slow (4 per second) and double the speed a few times. The level of jitter is not good, but probably not enough to cause mis-steps as long as the step rate is well below the motor's maximum.

When the steps to the 2 motors coincide it impacts one or both motors.

The step rates of the varying speed motor here are twice as fast as the previous version, even when the second motor is stepping twice as fast as before the jitter is far lower for both motors. The version can also step significantly faster, but the jitter does start to build up at really fast step rates.

The code changes are all in the module. These changes are incompatible with anything other than stepper motors, and could short out the power supply through the HAT if anything goes wrong - which would probably trash the HAT.

add to the constructor:
self.regvals = [None] * 256
and the method write8 should be amended as follows:
def write8(self, reg, value):
    "Writes an 8-bit value to the specified register/address"
    if self.regvals[reg] is None or self.regvals[reg] != value:
          self.bus.write_byte_data(self.address, reg, value)
          self.regvals[reg] = value
          if self.debug:
            print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg))
        except IOError as err:
          return self.errMsg()


  1. Hey there, I wanted to let you know that at least two people are appreciating your changes to the original code very much. I took the freedom to contribute the code to the original repository here
    I took every chance to say that you are the original author, not me.

  2. oh! well done, I was feeling guilty about not doing that!


  3. Is it possible to get some of your testing code for driving the stepper and increasing and decreasing the speed? Is it on github or bitbucket perhaps? I am looking for example code to help out on a friend's project that requires accelerating and decelerating the stepper speed and when I use adafruit's setSpeed function it seems to sometimes lose steps and not be consistent. I have not yet tried your modified version of the adafruit code. I also saw your post about the Pololu A4988 and it looks very interesting, but I am trying to see what can be done with the hardware he already has. Thanks!

  4. Sounds like you have also run into another bug in the code - if you go to the githib adafruit repo, Hendrik changed my quick mod into a pull request - theres a couple of others in there that might be useful as well. I'll dig out the code I had and stick something on github in the next couple of days.

  5. That will be very helpful to me. Don't worry about cleaning it up or making it presentable - it's for educational purposes, not publication. I tried doing a google search for "Adafruit_MotorHAT()" but did not find hardly any code that varied much from adafruit's example.

    When I searched the raspberry pi forum for things like "stepper speed", I found some discussions about increasing the i2c bus speed in order to be able to move the steppers faster, like this: Is this something that you also experimented with when using the unmodified adafruit code?

  6. I didn't bother with the faster bus thing - there were a few reports of complications arising, and I'd already decided to try the pololu drive boards anyway. I've had a look at the code I used at the time and it is a pile of spaghetti, tangled up with all sorts of other stuff I was trying out. But in principle just use a loop with a timer and use time.sleep() to wait till the next tick is due. You can run this in a separate thread or better still a separate process. The loop needs to be

    nextick = time.time() + interval
    while running:
    call motor.step()
    nexttick += interval
    do anything else needed - adjust interval for speed ramping for example - check if you want to carry on

    as long as you do the tick straight after coming back from sleep, and calculate the sleep time immediately before sleeping the timing comes out pretty accurately

  7. Hi

    I have tried your solution in editing the write8 and adding self.regvals=[None]*256 into the _init_ of However, this changes nothing at all to the speed of the motor. The max speed for the motor would be 600RPM but I only got it running at 120RPM (this speed will be halved if two motors are running at once!). In your other post in, you state something about the motor hat not being thread safe. What do you mean by that ? I am using all Nema 17 stepper motor with 1.8 degree step angle. Help please

    1. It all comes down to the steps per second you want to achieve. Even with my code change, you can only get to just under 200 steps per second. If your motor is 1.8 degrees per step, that is 200 steps per rev. To reach 600 rpm you need 2,000 full steps per second, which is way beyond what you can do with the adafruit hat. If you use the pololu driver card instead, and drive it direct from gpio you should be able to go that fast.

      I found the steppers I am using need to use 1/2 step mode to achieve maximum speed, and need to be ramped up to that speed as well. I am issuing close to 2000 steps per second, but I do have occasional jitter problems, so I am looking into using DMA to actually waggle the gpio pins - using pigpio to do the hard work.

      The thread safe note is because with adafruit's code, all calls to the adafruit library must be from the same python thread - if you are not using python threading, then there is no problem.

      Hope this helps!

  8. This comment has been removed by the author.

  9. Hi

    I'm trying to test you mod but I can't find

    Where do I go after home/pi/Adafruit-Motor-Hat-Python-Library ??


    1. Its not in there - its in the adafruit I2C library:

      Have you downloaded the GPIO library as well as the motor hat module?

  10. Hi again.

    I found But it's in a .egg file.
    When i try sudo nano on that adress it creates a new empty file.

    I also tried cd usr/.../Adafruit_GPIO but it says that it's not a directory. Sorry I'm new to RPi.

    Thanks again