gabone
Posts: 19
Joined: Fri Dec 28, 2012 12:23 pm

stepper hat

Tue Dec 29, 2015 3:10 am

Hello,

I recently bought a DC and Stepper Motor HAT for Raspberry Pi from Adafruit. I wanted to use it in a project with a stepper motor. During testing I found out that I need more RPM for the stepper motor. I understood that the python application supplied with the HAT is slow because python is an interpreted language. So I started translating the python code to C using the WiringPi library. It turns out that the stepper motor is spinning at the same RPM. I tried to increase the baud rate of the i2c port from /boot/config.txt, but above 100000 it's missing steps. So, I have a couple of questions:

1. What is the maximum baud rate that RPi can handle? Above 100000 it's missing steps. I found out that I2C standard mode is 100000, fast mode is 400000, and even 3.4 Mbps for High speed mode.

2. I noticed that the HAT is using the PCA9685. The data sheet says that it can handle High speed mode if you set the slave address to 00001XX. Please correct me if I am wrong. I noticed that the PCA9685 is hardwired as 1100000 on the A5 pin. Is it possible to configure the chip to use High speed mode, or maybe Fast mode or Fast mode+? If yes, could the RPi handle that speed?

Somehow it seems that the stepper RPM is limited by the I2C baud rate and the RPi is too slow for higher speeds.

Thank you,
Gabriel

User avatar
Douglas6
Posts: 4778
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: stepper hat

Tue Dec 29, 2015 3:35 am

The i2c speed will not affect the speed of the motors. At most, it will affect the rate at which the motor speed can be changed. The motor speed is affected by the duty rate of the PWM produced by the board (which can vary the full range of 0% to 100%), and the capabilities of the motor itself. Similarly, the language used does not affect motor speed, but only the rate of change.

gabone
Posts: 19
Joined: Fri Dec 28, 2012 12:23 pm

Re: stepper hat

Tue Dec 29, 2015 4:20 am

Hi Douglas,

As far as I recall PWM is not used for single step and double step. The duty rate is always 100%. The PWM is only used for microstepping in the original Adafruit python code.
By the rate of change you mean the change between coils?

Thank you for replying.
Gabriel

User avatar
mikronauts
Posts: 2729
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: stepper hat

Tue Dec 29, 2015 4:27 am

Trying to use a PWM LED controller chip to drive a stepper motor is pretty much the same thing as using a screw driver to hammer in a nail.

Wrong tool for the job.

If you must use an I2C interfaced chip, then get a dedicated stepper motor controller such as

http://www.nxp.com/documents/data_sheet/PCA9629A.pdf

or similar.
gabone wrote:Hello,

I recently bought a DC and Stepper Motor HAT for Raspberry Pi from Adafruit. I wanted to use it in a project with a stepper motor. During testing I found out that I need more RPM for the stepper motor. I understood that the python application supplied with the HAT is slow because python is an interpreted language. So I started translating the python code to C using the WiringPi library. It turns out that the stepper motor is spinning at the same RPM. I tried to increase the baud rate of the i2c port from /boot/config.txt, but above 100000 it's missing steps. So, I have a couple of questions:

1. What is the maximum baud rate that RPi can handle? Above 100000 it's missing steps. I found out that I2C standard mode is 100000, fast mode is 400000, and even 3.4 Mbps for High speed mode.

2. I noticed that the HAT is using the PCA9685. The data sheet says that it can handle High speed mode if you set the slave address to 00001XX. Please correct me if I am wrong. I noticed that the PCA9685 is hardwired as 1100000 on the A5 pin. Is it possible to configure the chip to use High speed mode, or maybe Fast mode or Fast mode+? If yes, could the RPi handle that speed?

Somehow it seems that the stepper RPM is limited by the I2C baud rate and the RPi is too slow for higher speeds.

Thank you,
Gabriel
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

gabone
Posts: 19
Joined: Fri Dec 28, 2012 12:23 pm

Re: stepper hat

Tue Dec 29, 2015 4:43 am

Hi Mikronauts,

You made me laugh :). So, in other words, Adafruit's idea to use this i2c chip was not so good.
I spent so much time translating the python code to C and understanding how the i2c chip works that I just had enough of it. I just want to get rid of it and use some ULNs controlled by GPIO. The problem is I don't which one. My steppers are rated 8.8V and 0.8A. I understand that most of them are 0.5A. So your suggestion is very welcome.

Thank you,
Gabriel

User avatar
mikronauts
Posts: 2729
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: stepper hat

Tue Dec 29, 2015 3:14 pm

Hi Gabriel,

Glad to help :-)

First, you need to find out if your stepper motor is unipolar or bipolar.

If it is unipolar, then you could use a UNL-style driver chip, but make sure it is rated for your voltage/current.

If it is bipolar, you would need a dual h-bridge per motor.

Regarding the timing pulses, simplest would be using joan's pigpio to output "waveforms" over the Pi's GPIO pins.

Or get dedicated I2C / SPI based stepper motor controller chips.
gabone wrote:Hi Mikronauts,

You made me laugh :). So, in other words, Adafruit's idea to use this i2c chip was not so good.
I spent so much time translating the python code to C and understanding how the i2c chip works that I just had enough of it. I just want to get rid of it and use some ULNs controlled by GPIO. The problem is I don't which one. My steppers are rated 8.8V and 0.8A. I understand that most of them are 0.5A. So your suggestion is very welcome.

Thank you,
Gabriel
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
mikronauts
Posts: 2729
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: stepper hat

Tue Dec 29, 2015 4:13 pm

https://www.adafruit.com/products/2348

Is that the HAT you have?

I can see why Adafruit used the PWM LED controller on it, as it can be used to drive PWM for gear motors quite nicely.

Unfortunately this limits the update rate for stepper motors - as you have noticed - as stepper motors need (fast at high speed) changing patterns signals to drive their coils, which do not be to PWM'd.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

gabone
Posts: 19
Joined: Fri Dec 28, 2012 12:23 pm

Re: stepper hat

Wed Dec 30, 2015 12:41 pm

Yes, that is the hat. I bought yesterday an ULN2803 and hooked it up to the raspi and the stepper motor. I managed to get maximum around 85 RPM before the motor started to stutter.

pootle
Posts: 340
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: stepper hat

Fri Jun 03, 2016 9:06 am

Just for anyone else bumping into this thread, the Adafruit code that drives the steppers is pretty rudimentary, and for simple stepper use can easily be optimised to increase the maximum step rate to over 250 steps per second for single step driving. Still not wonderful but a whole lot more useful than 80 odd.

The step speed is entirely limited by the rate data can be stuffed down the i2c bus, and the standard code writes 16 registers every step, of which 14 are identical (in single step mode), so a simple mod to the code reduces the i2c traffic dramatically. This also means that rewrites in C for performance will make no difference at all - well cpu utilisation will improve, but even at 280 steps per second my thread is running at < 13% utilisation (on a pi 3).

However it does appear that if you do this wrong it could short the supply - so approach with caution. This only works if only steppers are being used.

Also note that the Adafruit code is probably not thread safe, so all motors should be driven from 1 process / thread

in Adafruit_I2C.py in the constructor add:

Code: Select all

    self.regvals = [None] * 256
and change the write8 method to:

Code: Select all

  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:
        try:
          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()
(editied to change "per minute" to "per second"... brain fade)

njspix
Posts: 5
Joined: Fri Dec 02, 2016 5:51 pm

Re: stepper hat

Thu Apr 06, 2017 12:42 am

I've recently run into the same problem as the OP, and I wanted to try the workaround posted by pootle. However, I'm at a loss when it comes to this:
in Adafruit_I2C.py in the constructor add:
As far as I can tell this file is wrapped up in something called an .egg. What is this? How do I edit it? After I edit it, do I need to do something to install the module again?

Thanks so much!

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: stepper hat

Thu Apr 06, 2017 4:24 pm

Gertbot can do stepping up to 5000 steps/sec with jitter less then 1% :D

njspix
Posts: 5
Joined: Fri Dec 02, 2016 5:51 pm

Re: stepper hat

Fri Apr 07, 2017 2:32 am

Wow, that would be nice! Should have looked a little harder...
I'd like to try to make what I have work before I go out and spend another $85... any thoughts? Is this getting off-topic?

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: stepper hat

Fri Apr 07, 2017 3:01 pm

Is this getting off-topic?
Maybe. I was not even trying to sell something. I was trying to point out the difference a dedicated ARM core (in this case a Cortex-M3)
makes to do real-time tasks. My stepper function is a timer driven interrupt routine running at up to 5KHz.
In the routine I not only have to do the I/O control for stepping but also check the end-stop switches and if the
number of steps has been taken. What is still missing is software for ramp-up and down.
(It has that for the DC motors by the way).

I have given up on 8-bit processors. They are often more even expensive then a 32-bit, especially the old ones.
(Ooops now we are really off topic. I'll stop here)

njspix
Posts: 5
Joined: Fri Dec 02, 2016 5:51 pm

Re: stepper hat

Sat Apr 08, 2017 5:05 pm

My apologies Gert, I did not mean to suggest that your suggestion was off-topic. I was just not sure if I should be asking a question about Python .eggs in this thread.

domoticom
Posts: 1
Joined: Thu Jan 11, 2018 3:22 pm

Re: stepper hat

Thu Jan 11, 2018 3:38 pm

I struggled whit the same speed problem. I was reading about the gertbot and then I blew my adafruit HAT while playing with current....So I ordered the Gertbot from Farnell. there were a few small problems (UART settings and qt4 library missing) in getting the software running, but that was due to changes in Pi3 and stretch. Gertbot support guided me through and it works fine now.
Because of the autonomous setup, a dedicated cpu on the board can step much faster and more precise.
With Adafruit HAT the max speed (frequency) was 150 or so. Now with the right current, and ramping up I can get until 4000!
Ramping up can be done in the Pi code, but I believe Gertbot will have ramping function in the upcoming s/w release

I'd strongly recommend it to everyone needing a professional stepper driver for RPi

Return to “HATs and other add-ons”