Uncategorized

Stepping motor notes

Asked 
Active today
Viewed 8 times
0

I have a bipolar stepper motor with the following datasheet


enter image description here


And a L298N driver wired like this:


enter image description here

I tried the 3 NPM packages available for the L298N driver, but none of them work with my set up, the motor just make a noise but don’t turn. I tried switching the IN pins and even tried some other python scripts but nothing works, the motor never rotate

Here is a sample code with the pigpio-l298n package (note: i use 5,6,13,19 GPIO pins instead of the one on the L298N driver image above. Also i put GPIO 26 & 27 for en enable pins but i didn’t connected them to the driver since there are already jumpers pins on it)

const readline = require('readline');
const L298N = require('pigpio-l298n');
//bcm code
let l298n = new L298N(26,5,6,27,13,19);
l298n.setSpeed(l298n.NO1,80);
l298n.forward(l298n.NO1);

const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout
});
rl.on('line', function (input) {
    if (input === 'quit()') {
        rl.close();
    } else if (input === 'f') {
            l298n.forward(l298n.NO1);
    } else if (input === 'b') {
            l298n.backward(l298n.NO1)
    } else if (input === 't') {
            l298n.stop(l298n.NO1);
    } else {
            l298n.setSpeed(l298n.NO1,parseInt(input));
    }
});

process.on("SIGINT", function(){
    l298n.stop(l298n.NO1);
    console.log('shutdown!');
    process.exit(0);
});
 New contributor
  • Hi @Owow, Welcome and nice to meet you. Ah, let me see. I usually test python bipolar stepper motor controller in 3 steps: (1) By hand (yes, no python, no hardware controller), apply DC voltage to Winding 1, then Winding 2 (of course in the correct polarity), then 1, 2, 1, 2 etc (2) Also by hand, but applying DC voltage to controller (L293, 298 etc), (3) By python, in debug mode, step by step, … When I was a newbie, I read the Prof Jones’s tutorial (Ref 1) very carefully three times, understanding more or less what is going on. Seconds reference is useful for unipolar and microstepping. – tlfong01 10 mins ago
  • References: (1) homepage.divms.uiowa.edu/~jones/step (2) raspberrypi.stackexchange.com/questions/97975/…. To fully understand stepping motors, I actually started with unipolar stepper, before moving on to bipolar, then microstepping. After upgrading to ninja, like other ninjas, I also modified the cheapie 28BYJ48 unipolar to bipolar then test it. Of course you can ignore any theory, just blindly try one package after another, and wiring pattern one after another until you find the combination that works, … 🙂 – tlfong01 2 mins ago   Edit
  • By the way, MicroChip and AdaFruit’s stepper motor tutorials are also very good. – tlfong01 just now   Edit
0

Try the following code. It permutes through all the possibilities so make a note of the values used when the stepper turns.

I have changed the code to use GPIO 5, 6, 13, and 19 (pins 29, 31, 33, 35). It needs the pigpio daemon to be running (sudo pigpiod).

#!/usr/bin/env python

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

import time
import itertools

import pigpio # http://abyz.me.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 = [5, 6, 13, 19] # 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()
.END

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

%d bloggers like this: