2-phase stepper motor on pyboard

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Leddalhs
Posts: 5
Joined: Wed Jan 13, 2016 10:29 pm

2-phase stepper motor on pyboard

Post by Leddalhs » Wed Jan 13, 2016 11:03 pm

Hi.

A few days ago, I use example about 4-phase stepper motor

and see that motor is running.

Next, I wanted to run the 2-phase stepper motor at same way.

So, I modify some code of steps and connect pyboard and 2-phase stepper motor driver.

Code: Select all

import pyb

class Stepper:

    steps = [
        [1, 2],
        [2, 3],
        [3, 4],
        [1, 4],
        [1, 2],
        [2, 3],
        [3, 4],
        [1, 4]
	]
    step_delay_ms = 1

    def __init__(self, pins):
        self.pins = [pyb.Pin(pin, pyb.Pin.OUT_PP) for pin in pins]
        self.current_step = 0

    def do_step(self):
        self._low_on_all()
        self._high_on_step_pins()
        self._record_step()
        pyb.delay(self.step_delay_ms)

    def _low_on_all(self):
        for pin in self.pins:
            pin.low()

    def _high_on_step_pins(self):
        high_pins = self.steps[self.current_step]
        for pin_number in high_pins:
            self.pins[pin_number - 1].high()

    def _record_step(self):
        self.current_step += 1
        if self.current_step == len(self.steps):
            self.current_step = 0
            
And, I input the code using putty.

Code: Select all

import pyb

from Stepper_2phase import *

motor = Stepper(['X1', 'X2', 'X3', 'X4'])
for i in range(4000):
    motor.do_step()
But it doesn't work without any response(vibration, noise etc).

I think that connecting between pyboard and motor driver is correct(I referred motor driver manual).

I don't understand this situation. I just modified steps code.

Please give me some tips about this problem.

Thanks.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: 2-phase stepper motor on pyboard

Post by dhylands » Wed Jan 13, 2016 11:31 pm

Do you have a link to the stepper motor and stepper driver that you're using?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: 2-phase stepper motor on pyboard

Post by pythoncoder » Thu Jan 14, 2016 10:19 am

Are you sure steps is right? Try

Code: Select all

    steps = [
        [1, 2],
        [2, 3],
        [3, 4],
        [4, 1]
   ]
I'd also temporarily set the step delay to something much longer (say 1 second) so that you've more of a chance of seeing what's going on.
Peter Hinch
Index to my micropython libraries.

Leddalhs
Posts: 5
Joined: Wed Jan 13, 2016 10:29 pm

Re: 2-phase stepper motor on pyboard

Post by Leddalhs » Sun Jan 17, 2016 8:47 pm

pythoncoder wrote:Are you sure steps is right? Try

Code: Select all

    steps = [
        [1, 2],
        [2, 3],
        [3, 4],
        [4, 1]
   ]
I'd also temporarily set the step delay to something much longer (say 1 second) so that you've more of a chance of seeing what's going on.
Thanks for your reply

I already do that.

But, it doesn't run without any response.

Thanks.

Leddalhs
Posts: 5
Joined: Wed Jan 13, 2016 10:29 pm

Re: 2-phase stepper motor on pyboard

Post by Leddalhs » Sun Jan 17, 2016 9:08 pm


User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: 2-phase stepper motor on pyboard

Post by dhylands » Sun Jan 17, 2016 9:22 pm

I think pythoncoder was trying to point out that the last entry in your table is 1, 4 and it should probably be 4, 1

I also noticed that the SLA7026 is a unipolar driver and you're using bipolar steppers. If the 2 center taps (white and yellow wires) are connected together, then it would be wired in a unipolar fashion.

How is the SLA7026 wired to micropython and how is the stepper wired to the SLA7026?

Leddalhs
Posts: 5
Joined: Wed Jan 13, 2016 10:29 pm

Re: 2-phase stepper motor on pyboard

Post by Leddalhs » Tue Jan 19, 2016 9:38 am

Oh sorry

step motor I linked(nk266-01at) is wrong. Step motor I used(nk266-02at) is unipolar stepmotor(I think this motor has been discontinued).

strange thing is that this system(nk266-02at + sla7026m) with arduino UNO is run very well.

Code: Select all

   /*
 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.


 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}
   
I just changed the MCU.

wires is connected same way(pin 8, 9, 10, 11 ==> X2, X3, X4, X5).

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: 2-phase stepper motor on pyboard

Post by dhylands » Tue Jan 19, 2016 7:30 pm

Looking at the arduino code, the correct sequence should be:

Code: Select all

steps = [
    [1, 3],
    [2, 3],
    [2, 4],
    [1, 4],
]
which is different from the sequence in your code. And since python uses zero based arrays, I'd use:

Code: Select all

steps = [
    [0, 2],
    [1, 2],
    [1, 3],
    [0, 3],
]
and then you don't need to subtract 1 when using them, and this will more closely match the sequence shown in the Arduino code comments, which is:

Code: Select all

  Step C0 C1 C2 C3
     1  1  0  1  0
     2  0  1  1  0
     3  0  1  0  1
     4  1  0  0  1
I'd also recommend that you NOT do "set all the pins low" and then "set the pins high that should be high".

It would be much better to just set the pins that should be low low, and set the pins high that should be high.

Finally, you can simplify your record_step function to be:

Code: Select all

 def _record_step(self):
     self.current_step = (self.current_step + 1) % len(steps)

Leddalhs
Posts: 5
Joined: Wed Jan 13, 2016 10:29 pm

Re: 2-phase stepper motor on pyboard

Post by Leddalhs » Thu Jan 28, 2016 9:21 pm

Ah..... I can't run motor.

I modified code and wiring but it doesn't run.

Is there anyone who can operate a 2-phase(unipolar) stepper motor here?

Please share code and video.

Thanks.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: 2-phase stepper motor on pyboard

Post by pythoncoder » Fri Jan 29, 2016 8:31 am

That motor driver is a fairly complex part requiring significant external circuitry. Are you sure it's working?

My first step to commissioning it would be to disconnect the Pyboard and test the electronics separately (apologies if you've already done this). Connect the four inputs to the chip to 5V via temporary 1K resistors and link pairs of pins to 0V in accordance with the datasheet's 2-phase excitation table. As you apply each pair of links the motor should rotate to the correct position - or, at the very least, lock into position if you manually rotate the shaft. Once this is working, getting the Pyboard to control it should be easy.
Peter Hinch
Index to my micropython libraries.

Post Reply