1

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);
});
Owow
  • 123
  • 4
  • 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 Nov 30 '19 at 14:55
  • References: (1) http://homepage.divms.uiowa.edu/~jones/step/ (2) https://raspberrypi.stackexchange.com/questions/97975/controlling-28byj48-bipolar-convert-with-l293d-ic/97976#97976. 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 Nov 30 '19 at 15:03
  • By the way, MicroChip and AdaFruit's stepper motor tutorials are also very good. – tlfong01 Nov 30 '19 at 15:06
  • 1
    Thanks for all the answers, @joan did find a solution to my problem – Owow Nov 30 '19 at 15:17
  • You are welcome. Happy motor stepping and nodeJs programming. Cheers. – tlfong01 Nov 30 '19 at 23:56

1 Answers1

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()
joan
  • 71,024
  • 5
  • 73
  • 106
  • 1
    Perfect it was 5,13,19,6 / 13,5,6,19 Thank you very much ! it still don't work with pigpio-l298n package but i will try to port to nodejs what you did in python – Owow Nov 30 '19 at 15:16
  • i'm searching for the RPM of the stepping motor but i think i'm missing the input frequency in the data sheet since i need to do RPM = step angle (1.8) /360 * input frequency(??) * 60. The motor rotate but not smoothly and go back a few step at random intervals so i guess i didn't find the right RPM. EDIT: looks like if i put 100 steps at 60% speed, it rotate almost perfectly but if i change the speed it breaks – Owow Dec 01 '19 at 20:57