Python Forum
stepper motor with raspberry pi
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stepper motor with raspberry pi
#1
I’ve had my 1st stepper hat delivered and all is working fine and have it turning a bipolar stepper motor, but need help getting more from the demo script.

I’ve used the instructions from the link on the Pimoroni shop to the motor HAT Adafruit instructions

I’ve copied it all and taken out what I think I don’t need and changed it to get a continuous turn but I’m trying to get it to do a full 360 rotate then stop. I’ve increased the steps to get (about) a full 360 but the demo script just keeps looping through??

My idea is to have a button press then 360 rotate and stop, until the next button press. I’m still trying to learn python but can’t even figure out a loop :-/

This is what I have so far:


#!/usr/bin/python
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(addr = 0x60)

# 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, 1)       # 200 steps/rev, motor port #1
myStepper.setSpeed(800)                  # 800 RPM

while (True):
	print("Interleaved coil steps")
	myStepper.step(400, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.INTERLEAVE)
any help is appreciated :-)
Reply
#2
while True is an infinite loop.  If you never break from it, then myStepper.step(400 #etc will be called, over and over, forever.

I'm not sure what step(400) means (400 steps?  400 milliseconds of movement?), but without a break in there, it looks like that's probably not what you're expecting.
Reply
#3
(Aug-14-2017, 07:39 PM)nilamo Wrote: while True is an infinite loop. If you never break from it, then myStepper.step(400 #etc will be called, over and over, forever.

I'm not sure what step(400) means (400 steps? 400 milliseconds of movement?), but without a break in there, it looks like that's probably not what you're expecting.

400 is the number of steps the motor (i think). from what I gather there are 4 magnets and each allows steps of certain speeds or size, triggering, in turn, to make the unit rotate. The code was taken from the example on the page I got the motor hat from, so their example must be the infinite loop. What I don't know how to do is for it to loop so I can get a full 360-degree rotation. i.e. if it takes steps to rotate then I want it to stop when it gets to 600 ready for the next button press. I don't know how many steps will make it turn the full 360, I'll figure that out afterward once I get past getting the loop to stop when it hits the right number of steps.

I've seen various loops when researching but non make any sense to me, esp as they are coded using other boards so I don't know how I can change it to fit mine :-s

or, if I just remove the line that says
Quote:while true
would that mean that it just does the 400 steps then stops?

it's not the normal loop I'm used too. I've just commented out the while true line (and removed the indent after I got an indent error) and it runs and stops on its own! winner!

now I just need to add code for a button press now :-)

thanks for steering me in the right direction
Reply
#4
(Aug-14-2017, 09:30 PM)bowen73 Wrote: thanks for steering me in the right direction
No problem :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  micropython-stepper 1.0.3 trix 2 554 Nov-20-2023, 06:00 AM
Last Post: trix
  Stepper motor/Easy Driver/HELP Harney 1 1,853 Jul-31-2021, 08:05 AM
Last Post: Harney
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,938 Mar-21-2021, 05:17 PM
Last Post: yome
  stepper motor and servo key control cel 4 2,552 Jul-27-2020, 06:26 AM
Last Post: DPaul
  Stepper Motor key control cel 2 1,863 Jul-25-2020, 12:19 AM
Last Post: cel
  EOF error using stepper motor cel 1 1,684 Jul-24-2020, 01:27 PM
Last Post: Larz60+
  Control 2 stepper motor simultaneously jihene 2 3,962 May-08-2019, 05:27 PM
Last Post: DeaD_EyE
  How to use servo motor with TFMini Distance Sensor in python script? programerguy 1 3,209 Dec-04-2018, 04:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020