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

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,852
  • 5
  • 76
  • 108