Ben's Blog

electronics, I.T. ben December 15, 2020

Driving a Bipolar Stepper Motor with an L298N and a Raspberry Pi

A few things to know about the L298N

This is a rudimentary way to drive a stepper motor. You are activating the motor’s coils in sequence yourself to have it take steps. There exist cheap stepper drivers which only require 1 signal from the Pi to be instructed to execute a full step sequence.

There are 2 coils on a bipolar stepper motor, each with a + and a – side. As such, this method requires 4 pins from your Pi to drive the Motor Vs only 1 pin with a more advanced stepper driver. Equally true is that a more advanced stepper driver will require at least another pin for direction and they’ll usually have other pins for micro-stepping.

This method does not allow for micro-stepping, only full steps. This is fine enough for most applications.

Lastly, while the L298N method might appear less appealing because of the above points, I find it to be extremely robust (advanced stepper drivers can be very finicky). You can power your Pi or Arduino with it thanks to a 5V port (warning, it can only provide so many amps and a Pi running heavy processing will crash).

It can be used to drive 2 DC motors instead of 2 stepper coils of a bipolar stepper motor, and has 2 PWM pins to adjust power given to the motors. It’s a very versatile and enabling device to master. You’ll also learn a lot about how stepper motors work.

Finding your stepper motor coils

Documentation for electronics is often inaccurate, disparate, when it’s even available. It’s good to find or verify what wires you think your coils are on. To do so you can put an LED on 2 motor pins and spin it until it lights up. When it does, induction powered your LED so you know you are holding the 2 wires of a coil. The other 2 wires are obviously the other coil.

There is no good way I could find to verify which of a coil’s wire is positive or negative. In my trials, I found that it doesn’t matter for a bipolar stepper motor on an L298N. The worst that will happen is that your motor may turn in the other direction which is easy enough to fix by flipping wires.

Circuit

Fritzing

Code

[python]#!/usr/bin/python3
import RPi.GPIO as GPIO
import time

out1 = 17
out2 = 18
out3 = 27
out4 = 22

# careful lowering this, at some point you run into the mechanical limitation of how quick your motor can move
step_sleep = 0.002

step_count = 200

# setting up
GPIO.setmode( GPIO.BCM )
GPIO.setup( out1, GPIO.OUT )
GPIO.setup( out2, GPIO.OUT )
GPIO.setup( out3, GPIO.OUT )
GPIO.setup( out4, GPIO.OUT )

# initializing
GPIO.output( out1, GPIO.LOW )
GPIO.output( out2, GPIO.LOW )
GPIO.output( out3, GPIO.LOW )
GPIO.output( out4, GPIO.LOW )

def cleanup():
GPIO.output( out1, GPIO.LOW )
GPIO.output( out2, GPIO.LOW )
GPIO.output( out3, GPIO.LOW )
GPIO.output( out4, GPIO.LOW )
GPIO.cleanup()

# the meat
try:
i = 0
for i in range(step_count):
if i%4==0:
GPIO.output( out4, GPIO.HIGH )
GPIO.output( out3, GPIO.LOW )
GPIO.output( out2, GPIO.LOW )
GPIO.output( out1, GPIO.LOW )
elif i%4==1:
GPIO.output( out4, GPIO.LOW )
GPIO.output( out3, GPIO.LOW )
GPIO.output( out2, GPIO.HIGH )
GPIO.output( out1, GPIO.LOW )
elif i%4==2:
GPIO.output( out4, GPIO.LOW )
GPIO.output( out3, GPIO.HIGH )
GPIO.output( out2, GPIO.LOW )
GPIO.output( out1, GPIO.LOW )
elif i%4==3:
GPIO.output( out4, GPIO.LOW )
GPIO.output( out3, GPIO.LOW )
GPIO.output( out2, GPIO.LOW )
GPIO.output( out1, GPIO.HIGH )

time.sleep( step_sleep )

except KeyboardInterrupt:
cleanup()
exit( 1 )

cleanup()
exit( 0 )[/python]

Stuff you might need for this to run:

[bash]sudo apt-get update –fix-missing && sudo apt-get install python3-rpi.gpio[/bash]

 

This stepper motor is rated for rotating by 1.8° per step. This means that it takes 100 steps to rotate 180 degrees:

and 200 to do a full 360:

Power the Pi from the L298N?

As I alluded to earlier, the L298N comes with a 5V power output that is most convenient to power Pis and Arduinos as it allows you to eliminate one of the 2 power supplies to your system. Be advised that while it provides the right voltage, it does not provide enough amps to power a standard Raspberry Pi, and while it can power a Pi Zero, the later will not get enough power and crash if it works hard enough (having a camera and a live streaming server pushed it over the edge in my testing). It will power a Pi Zero on Wifi running Python which is enough for a lot of applications.

Here’s the circuit if you want to do this:

Fritzing

Recent Comments

  1. Nagurbee shaik says:
    April 24, 2022 at 11:47 am

    Hi,

    I have connected a stepper motor, which needs only 3.1v rated voltage and 2.5 amps rated current. in this case, can i proceed with 5v power supply? if i use 12v power supply, is it ok to stepper motor ?
    setup:
    42BYGHW811 wantai mini stepper motor
    L298N driver
    Raspberry pi 3 b+
    power supply L298N – 5v
    power supply to raspberry – 5v

    kindly resolve the issue.
    Thank you in advance

    Reply
    • ben says:
      April 24, 2022 at 1:39 pm

      I definitely wouldn’t be using 12v supply but you might be able to get away with 5v. Overall this might be kind of a mismatch and you might want to try and find a better suited stepper driver.

      Reply
      • Nagurbee shaik says:
        April 24, 2022 at 1:43 pm

        Hi,

        May i know which stepper motor you used in above article?

        and can you suggest me any stepper driver for 42BYGHW811 wantai mini stepper motor.

        Thank you.

        Reply
        • ben says:
          April 24, 2022 at 1:52 pm

          The motor on this post is a nema 17. I don’t know of a driver for your motor off the top of my head, it’s your homework to figure one out that matches the specs :).

          Reply
          • Nagurbee shaik says:
            April 24, 2022 at 2:05 pm

            Thank you so much.. 🙂

          • Nagurbee shaik says:
            April 24, 2022 at 2:41 pm

            hi,

            Can you please give me the stepper motor model number ?
            because even my stepper is also a nema 17.

            thank you.. 🙂

          • ben says:
            April 25, 2022 at 1:45 am

            17HS4401

          • Nagurbee shaik says:
            April 25, 2022 at 7:03 pm

            Thankyou so much, its really helpfull to me.. 🙂

  2. Andrew Pullinger says:
    August 18, 2022 at 9:35 am

    I’m very much a beginner and have just bought a RP to play with . I managed to connect this all up and make it work first time. Thanks Ben.
    Next stage – Is to amalgamate a drive system all into a BBQ rotisserie and a Star tracker for my camera.

    Reply
    • ben says:
      August 18, 2022 at 2:30 pm

      Nice 🙂

      Reply
  3. Michael says:
    November 3, 2023 at 8:33 pm

    Hi! I’m new to Raspberry. I followed all the steps in this guide, unfortunately the motor didn’t move in the end 🙁 Do you have any tips?

    Reply
    • ben says:
      November 4, 2023 at 2:59 am

      Depends on the symptoms, do you see/hear the motor try to do anything?

      Reply
      • Michael says:
        November 5, 2023 at 5:30 pm

        No, the motor didn’t move 🙁 But I set up a circuit with a DC motor and it worked wonderfully.

        Reply
  4. Speed says:
    May 22, 2024 at 7:49 pm

    How to I reverse the way the stepper driver spins?

    Reply
    • ben says:
      May 22, 2024 at 9:40 pm

      Turn your laptop upside down :).

      I kid, but take a look at the code and think about actuating the pins in the reverse order.

      Reply

Leave a Comment

Cancel reply

This blog is solar powered

Interactive

Handwriting Capture
Mandalagaba
IPv6 link-local to MAC converter
IPv6 MAC to link-local converter
Markov Text Generation
Markov Word Generation
Markov Music Generation
Duplogrifier
Flood Fill Algorithms
Homestead Metrics
RGB Playground
Web Games

Categories

  • aesthetics111
    • plots54
    • specular holography6
  • Books3
  • I.T.202
    • 3D modeling / printing21
    • AI6
    • all out geekery36
    • electronics27
    • homestead automation6
    • maniacal paranoia25
    • plotters49
    • unix / linux29
    • video games4
    • web development29
    • web games3
  • Lego / Duplo67
  • life in the U.S.42
  • miscellaneous202
  • nature encounters114
  • old vinyls3
  • organs2
  • self sustainability560
    • agriculture105
    • apiculture38
    • apple20
    • building131
    • canning3
    • crochet6
    • foraging6
    • hunting10
    • maple syrup47
    • poultry39
    • preserving2
    • solar power28
    • water23
    • wood84
  • trip to a new life6
Theme by Bloompixel. Proudly Powered by WordPress