Motor Spin Speed using Adafruit Stepper Motor Hat

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
DeclanBaker
 
Posts: 5
Joined: Thu Jun 01, 2023 11:15 am

Motor Spin Speed using Adafruit Stepper Motor Hat

Post by DeclanBaker »

I am using an Adafruit stepper motor and Dc motor HAT, NEMA 17 bipolar stepper motor attached to the M3 and M4 ports, and 12V power supply. There is little to no load on the motor. From a code perspective I approached this problem in two ways (more below). I am trying to get the motor to spin faster, but I seem to reach a maximum spin speed, which is a different speed for each of the different codes. I looked up the maximum spin rate of a NEMA 17 stepper motor and this motor is well under performing, thus I believe it is either an electrical issue or coding error. I switched around all the wires to the M3 and M4 port to see if that would change anything and the speed was the same regardless. Any tips that would help would be fantastic. Thank you in advance.

CODE:
1)

Code: Select all

#!/usr/bin/python
#import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_Stepper
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor

import time
import atexit

# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT()

# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
    mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
    mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)

atexit.register(turnOffMotors)

myStepper = mh.getStepper(200, 2)  # 200 steps/rev, motor port #2
#this was changed from (200, 1) cuz I am using m3 and m4
myStepper.setSpeed(3000)             
# 30 RPM usually but changed; 300RPM vs 3000RPM doesn't have much of a difference

while (True):
    print("Double coil steps")
    myStepper.step(1204, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.DOUBLE)
    time.sleep(1)

#No matter how much I increase the myStepper.setSpeed() it reaches a maximum of roughly 50 RPM
2)

Code: Select all

import time
import board
import busio
from adafruit_motor import stepper
from adafruit_motorkit import MotorKit as MK

kit = MK(i2c = busio.I2C(board.SCL, board.SDA, 100000))
for i in range (1204):
        kit.stepper2.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
        time.sleep(0.00001)

kit.stepper2.release()
Again no matter how small I make the time.sleep() it still reaches an asymptote of roughly 60 RPM.
Last edited by adafruit_support_bill on Thu Jun 01, 2023 11:29 am, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor Spin Speed using Adafruit Stepper Motor Hat

Post by adafruit_support_bill »

Please post the electrical specs for the motor. NEMA17 is just a frame-size spec. It only tells us where the mounting holes on the faceplate are.

Also please post specs for your power supply. For best performance, it will need to supply sufficient current to power two phases of the motor at a time.

The maximum step rate for the motor can be limited by physics, code or communications overhead. Assuming that your motor + power supply + are electromechanically a good match for the Hat, it is usually code or communications overhead.

The i2c bus puts an upper bound on the rate at which step commands can be set to the Hat. It is possible to increase the i2c bus clock speed: https://learn.adafruit.com/todbot-circu ... us-3114606

CircuitPython is an interpreted language, so there is some overhead in executing the code itself. You can move to a faster processor, or code time-critical sections in C.

User avatar
DeclanBaker
 
Posts: 5
Joined: Thu Jun 01, 2023 11:15 am

Re: Motor Spin Speed using Adafruit Stepper Motor Hat

Post by DeclanBaker »

This is the specs of the stepper motor:
https://cdn-shop.adafruit.com/product-f ... asheet.jpg

This is the specs of the power supply:
12v 7a LED Desktop Power Adapter
Input:Universal 100-240Vac;50/60Hz
Output: 12v 7.0a,84W
Model number:YHY-12007000
AC Inlet: IEC 320-C6/C8/C14
DC connector: 5.5*2.1/5.5*2.5/others
MTBF: 50000hrs

Thank you for the advice on the i2c bus clock speed I will look into that. If there are any other questions please let me know I will happily answer them

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor Spin Speed using Adafruit Stepper Motor Hat

Post by adafruit_support_bill »

That motor is compatible with the Hat. And the power supply is more than adequate. The motor will need about 700mA max (350mA/phase).

Not sure what speeds you need to achieve. But the practical electromechanical limit with this combination of motor/driver/power supply will be about 830 steps/second or 250 RPM. (Any faster and it will start missing steps).

Those speeds were achieved with the i2c bus running at 400KHz, and using optimized C++ code. At the default rate using the standard c++ Arduino library, the system will max out at about 50 RPM. Python tends to be slower and more dependent on processor speed.

User avatar
DeclanBaker
 
Posts: 5
Joined: Thu Jun 01, 2023 11:15 am

Re: Motor Spin Speed using Adafruit Stepper Motor Hat

Post by DeclanBaker »

import time
import board
import busio
from adafruit_motor import stepper
from adafruit_motorkit import MotorKit as MK

kit = MK(i2c = busio.I2C(board.SCL, board.SDA, 400000))
for i in range (1204):
kit.stepper2.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
time.sleep(0.00001)

kit.stepper2.release()

When I change the line:
kit = MK(i2c = busio.I2C(board.SCL, board.SDA, 100000))
kit = MK(i2c = busio.I2C(board.SCL, board.SDA, 400000))
in order to increase the run speed of the i2c the motor starts off spinning faster for roughly 0.5 seconds then returns to the i2c run speed of 100kHz
I tried changing the line to:
kit = MK(i2c = busio.I2C(board.SCL, board.SDA, 400000), pwm_frequency=400000) # or
kit = MK(i2c = busio.I2C(board.SCL, board.SDA, 200000), pwm_frequency=200000) # or even
kit = MK(i2c = busio.I2C(board.SCL, board.SDA, 100000), pwm_frequency=100000)
For all of these I get "ValueError: PCA9685 cannot output at the given frequency", but when I looked up maximum frequency for the PCA9685 it is at 1.6kHz so I am confused on where to go from here

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor Spin Speed using Adafruit Stepper Motor Hat

Post by adafruit_support_bill »

the motor starts off spinning faster for roughly 0.5 seconds then returns to the i2c run speed of 100kHz
Try removing the sleep statement.
PCA9685 cannot output at the given frequency
PWM frequency and i2c clock frequency are entirely different things. The PWM frequency of the PCA9685 defaults to 1.6KHz and there is no benefit to changing that for your application. When driving stepper motors, PWM is only used in MICROSTEP mode.

User avatar
DeclanBaker
 
Posts: 5
Joined: Thu Jun 01, 2023 11:15 am

Re: Motor Spin Speed using Adafruit Stepper Motor Hat

Post by DeclanBaker »

I fixed the problem with:
`sudo nano /boot/config.txt`
then added the line:
`dtparam=i2c_arm_baudrate=400000`
under the line:
`dtparam=i2c_arm=on`

I am concerned that this could be bad if I attach any other elements to this RPI. Do you see any possible problems?

I read somewhere that if I put the sleep.time(0.0), or remove it completely, then that could possibly damage the motor or have it over step, this definitely could be wrong.

Lastly, how did you know that the motor I provided would work just from the electrical specs.

Thank you so much for your help! I really appreciate the speed and quality of your responses.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor Spin Speed using Adafruit Stepper Motor Hat

Post by adafruit_support_bill »

I am concerned that this could be bad if I attach any other elements to this RPI. Do you see any possible problems?
If you are adding other i2c devices to the bus, make sure that they can handle the higher clock speed. This should be specified in the data sheet for the device.

Also don't try to communicate with other i2c devices using a different thread. i2c is a *serial* bus and can only manage one transaction at a time.

Also note that running other threads and/or processes can lead to irregular speeds since Linux is a time-sharing operating system, so the thread that is running your motor may be suspended from time-to-time.
I read somewhere that if I put the sleep.time(0.0), or remove it completely, then that could possibly damage the motor or have it over step, this definitely could be wrong.
Stepping too fast can cause skipped steps. But I think you are limited by software overhead at this point. There is no risk of damage to the motor.
Lastly, how did you know that the motor I provided would work just from the electrical specs.
The TB6612 driver chips on the Hat are simple H-bridges. They do not have active current limiting, so it is important to make sure that the motors do not pull more current than the chip is rated for (1.2A/channel).

The maximum current each channel will experience is governed by Ohm's Law (I = V/R). So 12v / 35 Ohms = 0.343 Amps. Well within the 1.2A rating. For more on the subject see: https://learn.adafruit.com/all-about-st ... he-stepper

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

Return to “Adafruit CircuitPython”