Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Stepper motor with Pi

Tue Apr 18, 2017 6:22 pm

Hello,

Firstly, excuse my bad english but I'm not a native english.

I post here because i encounter some issue when i try to make a stepper motor spin.

I've try to supply the motor with the PI (stepper motor 28BYJ-48 5V DC) and after with a 9V battery.

The motor only vibrate but never spin. The most weird thing is that I've still try to make it works and during a session test, the motor have spin !!! I've make a break and when i come back, the motor don't spin anymore :(

So i need your help to understand what is wrong here !

Here is a link for my wires connection : http://hpics.li/ba7fc8a

Thank you !

PS : when i launch the script, led are well blinking

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Stepper motor with Pi

Tue Apr 18, 2017 6:50 pm

If it only vibrates it means the GPIO are connected in the wrong order. However if it worked once that would appear not to be the problem unless you changed the connections.

Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Re: Stepper motor with Pi

Tue Apr 18, 2017 7:01 pm

Hum really ? But yeah i didn't change anything (i also try during my test session to test all combinaisons for my 4 GPIO pins [17,22,23,24], do you see something wrong in my wires connection ?

ps : Tuto and script that's i use : http://www.raspberrypi-spy.co.uk/2012/0 ... in-python/

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Stepper motor with Pi

Tue Apr 18, 2017 7:11 pm

A link to a tutorial tells me nothing about your setup. If you show the code you are using and a clear photo of your Pi plus stepper plus connections it may be possible to help.

Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Re: Stepper motor with Pi

Tue Apr 18, 2017 7:19 pm

Thank you very much for being here, I'm doing this right now, i will send you all the informations in few minutes. =)

Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Re: Stepper motor with Pi

Tue Apr 18, 2017 7:33 pm

So informations :
Script is exactly the same as the tutoriel :

Code: Select all

#!/usr/bin/python
# Import required libraries
import sys
import time
import RPi.GPIO as GPIO
 
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
 
# Define GPIO signals to use
# Physical pins 11,15,16,18
# GPIO17,GPIO22,GPIO23,GPIO24
StepPins = [17,22,23,24]
 
# Set all pins as output
for pin in StepPins:
  print "Setup pins"
  GPIO.setup(pin,GPIO.OUT)
  GPIO.output(pin, False)
 
# Define advanced sequence
# as shown in manufacturers datasheet
Seq = [[1,0,0,1],
       [1,0,0,0],
       [1,1,0,0],
       [0,1,0,0],
       [0,1,1,0],
       [0,0,1,0],
       [0,0,1,1],
       [0,0,0,1]]
        
StepCount = len(Seq)
StepDir = 1 # Set to 1 or 2 for clockwise
            # Set to -1 or -2 for anti-clockwise
 
# Read wait time from command line
if len(sys.argv)>1:
  WaitTime = int(sys.argv[1])/float(1000)
else:
  WaitTime = 10/float(1000)
 
# Initialise variables
StepCounter = 0
 
# Start main loop
while True:
 
  print StepCounter,
  print Seq[StepCounter]
 
  for pin in range(0,4):
    xpin=StepPins[pin]# Get GPIO
    if Seq[StepCounter][pin]!=0:
      print " Enable GPIO %i" %(xpin)
      GPIO.output(xpin, True)
    else:
      GPIO.output(xpin, False)
 
  StepCounter += StepDir
 
  # If we reach the end of the sequence
  # start again
  if (StepCounter>=StepCount):
    StepCounter = 0
  if (StepCounter<0):
    StepCounter = StepCount+StepDir
 
  # Wait before moving on
  time.sleep(WaitTime)
Connections and motors :

Motor : http://hpics.li/83ae3c4

ULN2003 - Breadboard : http://hpics.li/7ab7b2b

Raspberry PI - Breadboard : http://hpics.li/8b17c8a

General : http://hpics.li/3c2130d

There is one picture where ground battery is not connected but i just forgot to set it, he is here in my tests
Thank you !

User avatar
Gavinmc42
Posts: 4526
Joined: Wed Aug 28, 2013 3:31 am

Re: Stepper motor with Pi

Tue Apr 18, 2017 10:16 pm

Have you slowed it down enough to check the sequence is correct on the LEDS?
Then speed it up until it stops and back off a little to find the fastest speed.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Stepper motor with Pi

Wed Apr 19, 2017 8:11 am

There are too many levels of wire redirection to be clear which wires are ending up at the stepper in which order (at least for me).

It's probably best to rule out wrong wiring by running the following script. It will permute the possibilities. If it turns smoothly at some stage this suggest there is a sequence problem.

Code: Select all

#!/usr/bin/env python

# permute_stepper.py
# 2014-10-06
# Public Domain

import time
import itertools

import pigpio # http://abyz.co.uk/rpi/pigpio/python.html

class stepper:
   """
   A class to pulse a stepper.
   """

   def __init__(self, pi, g1, g2, g3, g4):
      """
      """
      self.pi = pi
      self.g1 = g1
      self.g2 = g2
      self.g3 = g3
      self.g4 = g4

      self.all = (1<<g1 | 1<<g2 | 1<<g3 | 1<<g4)

      self.pos = 0

      pi.set_mode(g1, pigpio.OUTPUT)
      pi.set_mode(g2, pigpio.OUTPUT)
      pi.set_mode(g3, pigpio.OUTPUT)
      pi.set_mode(g4, pigpio.OUTPUT)

   def move(self):
      pos = self.pos 
      if pos < 0:
         pos = 7
      elif pos > 7:
         pos = 0
      self.pos = pos

      if   pos == 0: on = (1<<self.g4)
      elif pos == 1: on = (1<<self.g3 | 1<<self.g4)
      elif pos == 2: on = (1<<self.g3)
      elif pos == 3: on = (1<<self.g2 | 1<<self.g3)
      elif pos == 4: on = (1<<self.g2)
      elif pos == 5: on = (1<<self.g1 | 1<<self.g2)
      elif pos == 6: on = (1<<self.g1)
      else:          on = (1<<self.g1 | 1<<self.g4)

      off = on ^ self.all

      self.pi.clear_bank_1(off)
      self.pi.set_bank_1(on)

   def forward(self):
      self.pos += 1
      self.move()

   def backward(self):
      self.pos -= 1
      self.move()

   def stop(self):
      self.pi.clear_bank_1(self.all)

# Permutes the gpio assignments to find ones which successfully
# drive a stepper.  The stepper should move clockwise then
# anti-clockwise for DELAY seconds.

DELAY=3

gpios = [17, 22, 23, 24] # Set the gpios being used here.

pi=pigpio.pi()

if not pi.connected:
   exit(0)

try:
   for x in itertools.permutations(gpios):

      s = stepper(pi, x[0], x[1], x[2], x[3])

      print("Trying {}".format(x))

      stop = time.time() + DELAY
      while time.time() < stop:
         s.forward()
         time.sleep(0.0001)

      stop = time.time() + DELAY
      while time.time() < stop:
         s.backward()
         time.sleep(0.0001)

except KeyboardInterrupt:
   pass

s.stop()

pi.stop()

Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Re: Stepper motor with Pi

Wed Apr 19, 2017 9:52 am

Hi !

Thanks again for your help but still with the script that you provide, nothing happen :/ motor still vibrate but do not spin !

I've try with multiple wait Time :/

Any suggestion ?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Stepper motor with Pi

Wed Apr 19, 2017 10:10 am

It should work. I'd power then from the Pi's 5V line. Perhaps your battery is flat.

See viewtopic.php?p=709334#p709334 for a couple of photos showing the connections I made.

Here is a chart showing the correlation between delay between steps and stepper speed. 17 RPM seems to be the maximum.
28BYJ-48.png
28BYJ-48.png (21.18 KiB) Viewed 2213 times

User avatar
Gavinmc42
Posts: 4526
Joined: Wed Aug 28, 2013 3:31 am

Re: Stepper motor with Pi

Wed Apr 19, 2017 11:19 am

Mine worked fine powered from the Pi's 5V.
Stepper just plugs into the PCB, so cannot mess that up.

Must be the code. Try simpler code.
I have even tested them with shell script in half step mode.
viewtopic.php?f=91&t=179504&p=1142954&h ... r#p1142954
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Re: Stepper motor with Pi

Wed Apr 19, 2017 11:53 am

Thank you very much for all the answers !

It actually spin but i change nothing ! :o i really don't get it ! I'll wait 1/2 Hours and give you a feedback if it is still spinning

User avatar
Gavinmc42
Posts: 4526
Joined: Wed Aug 28, 2013 3:31 am

Re: Stepper motor with Pi

Wed Apr 19, 2017 11:57 am

You might want to check the connector/wiring, sounds like one wire might need a rework.

Can you change speed ok?
Try wiggling one wire at a time to see if it stops.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Re: Stepper motor with Pi

Wed Apr 19, 2017 12:32 pm

Yeah i'll check thank you !
Just a question, why my motor won't spin at the same speed ?

I mean i ask it to move 1000 time forward then 1000 time backward but the motor didn't came back to his original position !

Is it a way to control this ?

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Stepper motor with Pi

Wed Apr 19, 2017 12:48 pm

Hi,
exactly what type of battery are you using- a 'standard' PP3 ? This may struggle to supply enough current, and probably not for very long if it does.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Re: Stepper motor with Pi

Wed Apr 19, 2017 12:53 pm

I'm actually using my PI as supplyer with PIN2 (5V) so i guess he always provide the same power no ?

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Stepper motor with Pi

Wed Apr 19, 2017 12:54 pm

Seregone wrote:I'm actually using my PI as supplyer with PIN2 (5V) so i guess he always provide the same power no ?
OK
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
Gavinmc42
Posts: 4526
Joined: Wed Aug 28, 2013 3:31 am

Re: Stepper motor with Pi

Thu Apr 20, 2017 3:30 am

Power supply was from 5V on header
I tested on Pi B+ but used Pi3 supply.

Could be driving too fast if missing steps?
These are not the fastest steppers on the planet :lol:
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Stepper motor with Pi

Thu Apr 20, 2017 7:12 am

Gavinmc42 wrote:Power supply was from 5V on header
I tested on Pi B+ but used Pi3 supply.

Could be driving too fast if missing steps?
These are not the fastest steppers on the planet :lol:
No, they are not, not great torque either.

I think the OP has now said twice that it works sometimes, once might be a mistake and he forgot he changed connections between trials, I'll take the second to rather point to a poor connection.

Seregone
Posts: 30
Joined: Tue Apr 04, 2017 2:54 pm

Re: Stepper motor with Pi

Thu Apr 20, 2017 10:02 am

Hi !

Thank again ! I start my PI this morning and try the motor, he still spin so i'm happy but i still have this problem about forward and backward, someone can explain me why this motor is faster in forward than in backward ?

This is the script that i use :

Code: Select all

class StepperControler:
    def __init__(self, pin1, pin2, pin3, pin4):
        self.stepPins = [pin1,pin2,pin3,pin4];
        for pin in self.stepPins:
            GPIO.setup(pin, GPIO.OUT);
            GPIO.output(pin, False);
        self.sequences = [[1,0,0,1],
                          [1,0,0,0],
                          [1,1,0,0],
                          [0,1,0,0],
                          [0,1,1,0],
                          [0,0,1,0],
                          [0,0,1,1],
                          [0,0,0,1]];

        
        self.stepCount = len(self.sequences);
        self.stepDir = 1;
        self.waitTime = 0.002;
        self.stepCounter = 0;

    def move(self, stepDir):
        self.stepDir = stepDir;

        it = 0;
        while it != 1000:
            it = it + 1;

            for pin in range(0,4):
                xpin=self.stepPins[pin]# Get GPIO
                if self.sequences[self.stepCounter][pin]!=0:
                    GPIO.output(xpin, True)
                else:
                    GPIO.output(xpin, False)
         
            self.stepCounter += self.stepDir
         
            # If we reach the end of the sequence
            # start again
            if (self.stepCounter>=self.stepCount):
                self.stepCounter = 0
            if (self.stepCounter<0):
                self.stepCounter = self.stepCount+self.stepDir
         
            # Wait before moving on
            time.sleep(self.waitTime)

        # Stop all led
        for pin in self.stepPins:
            GPIO.output(pin, False);

    def __stop__(self):
        GPIO.cleanup();

Return to “General discussion”