Real Gold
Posts: 8
Joined: Thu Apr 09, 2015 4:15 am

Stepper motor

Thu Apr 09, 2015 4:19 am

hi
i want to know hot to use stepper motor with raspberry pi ??
i see some people use a small driver but i mean i want to use
big size of stepper motor and digital driver like this driver
my stepper motor 4.2 amp
Image

waiting

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Stepper motor

Thu Apr 09, 2015 1:18 pm

Real Gold wrote:hi
i want to know hot to use stepper motor with raspberry pi ??
i see some people use a small driver but i mean i want to use
big size of stepper motor and digital driver like this driver
my stepper motor 4.2 amp
Image

waiting
best to include links, saves us from having to look them up.

http://www.stepmotor.biz/smd.htm#smd42

Manual

That sort of stepper motor driver seems fairly simple to connect to a Pi, just needs 3 GPIO and 3 transistors, one for Enable. one for direction, one for stepping.

It looks like you'll need to use 5Vdc as the signal to the stepper driver, configure it as image 2 of the manual (configure it as either example 1 or example 2, depending on your transistors).
Doug.
Building Management Systems Engineer.

Real Gold
Posts: 8
Joined: Thu Apr 09, 2015 4:15 am

Re: Stepper motor

Thu Apr 09, 2015 2:14 pm

Thank you
may you tell me python stepper motor control and
may you give me more information to connect
stepper motor with driver you said "3 GPIO and 3 transistors" any explanation ??
how to connect the 3 transistors and 3 GPIO ??
waiting for your reply

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Stepper motor

Thu Apr 09, 2015 2:28 pm

stepper.gif
stepper.gif (24.24 KiB) Viewed 3607 times
Pick 3 GPIO pins, the 5V pin and GND pins, and attach as either of the examples.

your python program shouldn't be too hard to figure out, just enable the Enable pin, pick the correct setting for the direction pin and trigger the step pin each time you wish the stepper to make a step.
Doug.
Building Management Systems Engineer.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Stepper motor

Thu Apr 09, 2015 3:05 pm

If you could give more details of your actual requirements (how you plan to use the stepper motor) then I'm sure someone will be able to help more with the python programming.
Doug.
Building Management Systems Engineer.

Real Gold
Posts: 8
Joined: Thu Apr 09, 2015 4:15 am

Re: Stepper motor

Thu Apr 09, 2015 4:27 pm

Thank you
may you give me any python example to control stepper motor ??

Real Gold
Posts: 8
Joined: Thu Apr 09, 2015 4:15 am

Re: Stepper motor

Thu Apr 09, 2015 4:36 pm

i will use the stepper motor with a ball screw to get linear motion on my device
i decide to build a smallest desktop injection molding machine
see
https://www.youtube.com/watch?v=4xwwWB2q2zI

Image

i am waiting for your reply my brother :D

may you tell me about a best hydraulic control unit for my project
and suitable hydraulic cylinder

don't forget you set to me "give more details of your actual requirements (how you plan to use the stepper motor)"

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Stepper motor

Thu Apr 09, 2015 7:41 pm

what's your input method? (are we talking about a keyboard and display or a couple of buttons attached to GPIOs)

I'd be tempted to use scratch gpio if you have a monitor.

Assuming python control via a keyboard:

I'm not the best at python but here something I've thrown together, stolen from another project.

Code: Select all

import RPi.GPIO as io
io.setmode(io.BCM)
import sys, tty, termios, time

# This blocks of code defines the three GPIO
# pins used for the stepper motor
motor_enable_pin = 4
motor_direction_pin = 17
motor_step_pin = 24
io.setup(motor_enable_pin, io.OUT)
io.setup(motor_direction_pin, io.OUT)
io.setup(motor_step_pin, io.OUT)


# The getch method can determine which key has been pressed
# by the user on the keyboard by accessing the system files
# It will then return the pressed key as a variable
def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

# This section of code defines the methods used to determine
# whether the stepper motor needs to spin forward or backwards. 
# different directions are acheived by setting the
# direction GPIO pin to true or to false. 
# both pins match, the motor will not turn.


def stepper_enable():
    io.output(motor_enable_pin, True)

def stepper_disable():
    io.output(motor_enable_pin, False)

def step_once():
    io.output(motor_step_pin, True)
    time.sleep(.1)
    io.output(motor_step_pin, False)
    time.sleep(.1)

def step_forward():
    io.output(motor_step_pin, True)
    step_once()

def step_reverse():
    io.output(motor_step_pin, False)
    step_once()




# Setting the stepper pins to false so the motors will not move
# until the user presses the first key
io.output(motor_enable_pin, False)
io.output(motor_step_pin, False)




# Instructions for when the user has an interface
print("w/s: acceleration")
print("a/d: steering")
print("l: lights")
print("x: exit")

# Infinite loop that will not end until the user presses the
# exit key
while True:
    # Keyboard character retrieval method is called and saved
    # into variable
    char = getch()

    # The stepper will be enabled when the "e" key is pressed
    if(char == "e"):
        stepper_enable()

    # The stepper will be disabled when the "d" key is pressed
    if(char == "d"):
        stepper_disable()

    # The "f" key will step the motor forward
    if(char == "f"):
        step_forward()

    # The "r" key will step the motor in reverse
    if(char == "r"):
        step_reverse()

    # The "g" key will step the motor 10 steps forwards
    if(char == "g"):
        for x in range(0, 10):
            step_forwards()

    # The "t" key will step the motor 10 steps in reverse
    if(char == "t"):
        for x in range(0, 10):
            step_reverse()


    # The "p" key will break the loop and exit the program
    if(char == "p"):
        print("Program Ended")
        break



    # The keyboard character variable will be set to blank, ready
    # to save the next key that is pressed
    char = ""

# Program will cease all GPIO activity before terminating
io.cleanup()
Doug.
Building Management Systems Engineer.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Stepper motor

Thu Apr 09, 2015 7:47 pm

may you tell me about a best hydraulic control unit for my project
and suitable hydraulic cylinder
I'm no expert on hydraulics, but you might find one here, I'd recommend specifics, what size of hydraulic cylinder do you expect to need? I'd guess that would depend on the volume of liquid you are injecting?

Good luck.
Doug.
Building Management Systems Engineer.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Stepper motor

Thu Apr 09, 2015 8:00 pm

P.S.

I would recommend testing your new python program with LEDs before connecting the stepper motor, connect LEDs to the GPIO pins (follow a GPIO LED connection tutorial) and increase the time delay for the stepper so that you can see the light.

(This makes it much easier to see where you are going wrong).

Please be aware that I'm not an expert on stepper motors, I haven't yet connected one up myself (I've got one in one of my parts boxes but haven't got around to connecting it up yet).
Doug.
Building Management Systems Engineer.

Real Gold
Posts: 8
Joined: Thu Apr 09, 2015 4:15 am

Re: Stepper motor

Fri Apr 10, 2015 8:54 pm

may you tell me any hydraulic switch or sensor ((control unit open and close ))
to use in my project ?? and tell me any raspberry pi simulation software
and about my news in fact i see scratch language is suitable to my project and i think it is better than python to make strong Gipo software have a (Graphic user interface ) Thank you my friend :D

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Stepper motor

Fri Apr 10, 2015 9:36 pm

BMS Doug wrote: I'm no expert on hydraulics, but you might find one here, I'd recommend specifics, what size of hydraulic cylinder do you expect to need? I'd guess that would depend on the volume of liquid you are injecting?
Sorry, I can't help you any further, I don't know much about hydraulics.

Good luck with your project.
Doug.
Building Management Systems Engineer.

User avatar
Tsjakka
Posts: 19
Joined: Fri Dec 21, 2012 7:21 pm

Re: Stepper motor

Sat Aug 25, 2018 12:17 pm

I recently bought a stepper motor, driver and power supply for a project. I was able to get them running using the information and software from this post, so I thought I should share a few bits.

First, lets take a look at my setup:

Image

In this image you can see the required connections:

Image

As you know, the GPIO ports on the Raspberry Pi are 3.3V, which isn't enough for the inputs of the driver. Therefor, I have put a few electronic devices on a breadboard, notably a transistor and a resistor. Together, the allow switching the inputs on the driver (DM542) with 5V signals.
We see the 5V power connection from the Raspberry Pi to the breadboard; the red and yellow wires (on pin 2 and 6 of the RPi, respectively).
The 3 GPIO ports are connected to pins 11, 13 and 15. On the driver, I have connected the collector of the transistors to the PUL-, DIR- and ENA- inputs. So, I am following "Signal source example 1" in the image provided by BMS Doug (the one with a common anode).
Here a close-up of the breadboard:

Image

The transistor is a BC547B by Fairchild. But I was told that many NPN transistors would fit the bill. The resistor is about 1000 Ohm, it is used to limit the current through the GPIO ports.

I adjusted the software posted by BMS Doug and fixed a few minor issues. You can get it here:

Code: Select all

import RPi.GPIO as io
io.setmode(io.BCM)
import sys, tty, termios, time

# This blocks of code defines the three GPIO
# pins used for the stepper motor
motor_enable_pin = 17
motor_direction_pin = 27
motor_step_pin = 22
delay = 3E-004              # By playing with this delay you can influence the rotational speed.
pulses_per_rev = 400        # This can be configured on the driver using the DIP-switches
io.setup(motor_enable_pin, io.OUT)
io.setup(motor_direction_pin, io.OUT)
io.setup(motor_step_pin, io.OUT)


# The getch method can determine which key has been pressed
# by the user on the keyboard by accessing the system files
# It will then return the pressed key as a variable
def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

	
# This section of code defines the methods used to determine
# whether the stepper motor needs to spin forward or backwards. 
# Different directions are acheived by setting the
# direction GPIO pin to true or to false. 
# My driver required:
#   DIR must be ahead of PUL effective edge by 5 micro-s to ensure correct direction;
#   Pulse width not less than 2.5 micro-s;
#   Pulse low-level width not less than 2.5 micro-s.

def stepper_enable():
    io.output(motor_enable_pin, False)

def stepper_disable():
    io.output(motor_enable_pin, True)

def step_once():
    io.output(motor_step_pin, True)
    time.sleep(delay)
    io.output(motor_step_pin, False)
    time.sleep(delay)

def step_forward():
    io.output(motor_direction_pin, True)
    time.sleep(delay)
    step_once()

def step_reverse():
    io.output(motor_direction_pin, False)
    time.sleep(delay)
    step_once()

# Setting the stepper pins to false so the motors will not move
# until the user presses the first key
io.output(motor_enable_pin, False)
io.output(motor_step_pin, False)


# Print instructions for when the user has an interface
print("e/d: enable/disable")
print("f/r: step forward / reverse")
print("g/t: rotate forward / reverse")
print("x: exit")

# Infinite loop that will not end until the user presses the
# exit key
while True:
    # Keyboard character retrieval method is called and saved
    # into variable
    char = getch()
    print char	

    # The stepper will be enabled when the "e" key is pressed
    if (char == "e"):
        stepper_enable()

    # The stepper will be disabled when the "d" key is pressed
    if (char == "d"):
        stepper_disable()

    # The "f" key will step the motor forward
    if (char == "f"):
        step_forward()

    # The "r" key will step the motor in reverse
    if (char == "r"):
        step_reverse()

    # The "g" key will step the motor 1 rotation forwards
    if (char == "g"):
        for x in range(0, pulses_per_rev):
            step_forward()

    # The "t" key will step the motor 1 rotation in reverse
    if (char == "t"):
        for x in range(0, pulses_per_rev):
            step_reverse()

    # The "x" key will break the loop and exit the program
    if (char == "x"):
        print("Program Ended")
        break

    # The keyboard character variable will be set to blank, ready
    # to save the next key that is pressed
    char = ""

# Program will cease all GPIO activity before terminating
io.cleanup()

Milliways
Posts: 497
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: Stepper motor

Tue Oct 01, 2019 1:20 am

BMS Doug wrote:
Thu Apr 09, 2015 2:28 pm
stepper.gif

Pick 3 GPIO pins, the 5V pin and GND pins, and attach as either of the examples.

your python program shouldn't be too hard to figure out, just enable the Enable pin, pick the correct setting for the direction pin and trigger the step pin each time you wish the stepper to make a step.
DO NOT do this! The suggested circuit connects the GPIO directly to a transistor base, effectively shorting the GPIO. The alternative (a PNP transistor) is even worse - it connects the 5V to a GPIO and would short the 5V supply!

pcmanbob
Posts: 7712
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Stepper motor

Tue Oct 01, 2019 8:13 am

The common anode method as show in the diagram img.2 - connection scheme is the correct way to do it , with the addition of a resistor between the gpio pin and the transistor base., the 3.3v from the gpio pin switches the base of the NPN transistor via a resistor, the transistor is switching the 5v output from the motor controller to ground.

This is exactly what Tsjakka's pictures show and is the correct way to do it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “General discussion”