Step motor L298N,etc

Viewed 46 times

I have a stepper that has 4 wires:

PIN 1 --- A  --- BLK --- CONNECTED TO GPIO 4
PIN 2 --- A\ --- GRN --- CONNECTED TO GPIO 23
PIN 3 --- B  --- RED --- CONNECTED TO GPIO 17
PIN 4 --- B\ --- BLU --- CONNECTED TO GPIO 24

I have tried looking for tutorials on how to operate the stepper motor they all seem to be for stepper motors that have 5 wires and connect to another HAT. (I have a 5 wire stepper motor and HAT for that stepper motor).

I was using this script:

import RPi.GPIO as GPIO
import time


#enable_pin = 18
coil_A_1_pin = 4
coil_A_2_pin = 17
coil_B_1_pin = 23
coil_B_2_pin = 24

#GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)

#GPIO.output(enable_pin, 1)

def forward(delay, steps):  
  for i in range(0, steps):
    setStep(1, 1, 0, 0)
    setStep(0, 1, 1, 0)
    setStep(0, 0, 1, 1)
    setStep(1, 0, 0, 1)

def backwards(delay, steps):  
  for i in range(0, steps):
    setStep(1, 0, 0, 1)
    setStep(0, 0, 1, 1)
    setStep(0, 1, 1, 0)
    setStep(1, 1, 0, 0)

def setStep(w1, w2, w3, w4):
  GPIO.output(coil_A_1_pin, w1)
  GPIO.output(coil_A_2_pin, w2)
  GPIO.output(coil_B_1_pin, w3)
  GPIO.output(coil_B_2_pin, w4)

while True:
  delay = raw_input("Delay between steps (milliseconds)?")
  steps = raw_input("How many steps forward? ")
  forward(int(delay) / 1000.0, int(steps))
  steps = raw_input("How many steps backwards? ")
  backwards(int(delay) / 1000.0, int(steps))

And this follows the step sequence that came with the motor: Data Sheet

I’m using a raspberry pi 3. The stepper motor does not turn at all. I want it to turn from the inputs in the script. Any help is appreciated.

  • Help for what? You have not told us what happens and what you want to happen. – joan 2 days ago
  • 1
    @joan apologies, updated question now – JackU 2 days ago
  • 1
    Are you trying to drive the motor coils directly from the GPIO pins? They require 2A per phase so you’ll need to use some current amplification in order to drive them: don’t think the GPIO alone will cut it. Use the schematic tool to add a drawing of your connections to the question. – Roger Jones 2 days ago
  • 1
    Also, there’s a missmatch between your list of connections and the pins in the code: is coil “A” between GPIO 4 and 17 or 4 and 23? – Roger Jones 2 days ago
  • (1) You need to use a step motor driver, such as L293D. (2) You can also use a multi-meter and batteries to do experiments to make sure which coil is which coil, which leads are A, B, C, D, etc. (3) Adafruit’s step motor tutorial is good for newbies, (4) You might like to read my answer below for more details: raspberrypi.stackexchange.com/questions/97975/…. – tlfong01 2 days ago   
  • It’s best not to drip feed information. Lots of people only look at a question once so you may only get one stab at getting an answer. What HAT, what stepper, photo of connections, etc. etc. – joan 2 days ago
  • 2
    @JackU, I forgot to mention my answer above includes a chat record about how I struggled for hours to use a mult-meter and batteries to learn why there are unipolar and bipolar motors, why some are 4 wires, some 5 wires. I even DIYed manual switches (actually 2 pole, 5 throw) rotary switches to manually activate one coil at a time, to move step by step. What I wish to share my painful learning experience is that step motor is complicated, many many times harder than blinking a led. But after hard work, JOY of seeing motor move only one tiny 0.6 degree step by hand is also many times bigger. – tlfong01 2 days ago   
  • @tlfong01 I bought two types of step motors, one with 4 wires and one with 5. I can’t seem to find much info about the 4 wire one and how to set it up. I’ve found plenty of info about 5 wires – JackU 2 days ago
  • Well give me the links of your steppers. I have more than 10 types of steppers in my junk box. Or show me a picture of the wiring connector. I can try to match your connector with mine and guess which colour lead is which for you to confirm at you side. aliexpress.com/w/wholesale-bipolar-motor.html – tlfong01 yesterday   
  • I always recommend newbies to start with SIX WIRE models, because you can config it to unipolar so it is easy to move the motor by a battery. After preliminary testing unipolar ok, then you can switch to bipolar. Some ads show the color code so you can compare with yours. See this ad’s colour code below. fr.aliexpress.com/item/… – tlfong01 yesterday   
  • Or stick to a particular group for consistent wiring colour and location, so not to go wrong so easily, like “Nema17” referred above: “Nema17” – bipolar stepper motor 34mm 0.9 degrees 2 phases Nema 17 Hybird 42 4 wire stepper motor for 3D printer CNC engraving machine US$15 – tlfong01 yesterday    
  • Suggesting testing by hand: I would suggest to refer to the following tutorial and then test the motor by hand in HALF STEP mode, using two 3V battery packs. You just follow the half step sequence table, by hand, keep/reverse alternate battery polarity, (using crocodile clips) (1) Step Motor Tutorial vshamu.wordpress.com/tag/bipolar-stepper-motor – tlfong01 yesterday    
  • I forgot to mention that beside L293D, you can also try L298N. L293D mode is good because they have two drivers for two step motors and also have optical isolators to isolate noise, back EMF, EMI spikes etc. On the other hand not to powerful L298N is very good for newbies, because you can more easily start playing with DC motor, PWM mode to control DC motor speed, drive servo, and finally stepping motor. You can google L298N in this forum on how newbies start with cheapy yellow TT toy motor. You can also google for TowerPro MG995/996 servos which “official” rpi tutorial use as example. – tlfong01 just now   Edit   

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: