Speed control for bipolar stepper

628 views
Skip to first unread message

StepNFetchIT

unread,
Jan 28, 2017, 1:15:18 PM1/28/17
to accelstepper
Hello,

I'm kinda new to the arduino world and have what I think should be a fairly simple project at hand. I would like to make a speed control for a bipolar stepper motor that can spin the motor at a rate of roughly 30RPM to 200RPM. First let me run down the hardware:

Arduino Uno (chinese knock-off)

L298N dual-h bridge driver board  (not just the L298N chip), the most common one you find on Ebay (red board), the complete board with +5v regulator which is designed to drive two DC motors or one stepper

Bipolar stepper motor (12V, 200 steps per revolution)

10K Potentiometer

I've tried some code using the default stepper library and did get the motor to spin but found it to moved roughly in large steps at slow speeds. I decided microstepping might be the way to go for smooth slow speed operation and that is what led me to the eccelstepper library.

Currently for wiring I have the L298N inputs connected to digital pins 8,9,10,11 on the arduino, and the pot wiper is connected to analog input 0 with +5v across the pot. 

As stated above, my required range of operation is about 30-2000RPM, but ideally when the pot is at 0 I would like the stepper motor to hold position.  (this is optional if difficult to achieve) If it is possible to lock the motor then a speed range of 0-200RPM would be fine.

I am wondering if there are any code examples available that will do what I am looking for? If so, could someone please point me to them, or give me some guidance on how to write this code myself....

Thanks in advance to anyone that takes the time to read or reply to my post here.




Mike McCauley

unread,
Jan 30, 2017, 2:01:34 AM1/30/17
to accels...@googlegroups.com
Hello,

have a look at the ProportionalControl example
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474

StepNFetchIT

unread,
Jan 30, 2017, 8:28:38 AM1/30/17
to accelstepper
Thanks, I took a quick glance at the proportional example this AM... This example only moves the motor to the position determined by the potentiometer then stops, I need constant rotation at the speed set by the potentiometer.  Even though this isn't exactly what I need I may be able to take the input sampling code and splice it together with the constant rotation code to get what I need. Not sure if I am up to that or not, but it can't hurt to try. Thanks for the nudge in the right direction.

Mike McCauley

unread,
Jan 30, 2017, 8:59:10 PM1/30/17
to accels...@googlegroups.com, StepNFetchIT
This might work for you:

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3,
4, 5

// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0

#define MAX_SPEED 150

void setup()
{
stepper.setMaxSpeed(1000);
}

void loop()
{
// Read new speed
int analog_in = analogRead(ANALOG_IN);
int speed = map(analog_in, 0, 1024, 0, MAX_SPEED);
stepper.setSpeed(speed);
stepper.runSpeed();

StepNFetchIT

unread,
Jan 30, 2017, 9:31:40 PM1/30/17
to accelstepper, rotod...@hotmail.com
Thanks a bunch for taking the time to post that. I don't have the hardware in front of me right at the moment, but I should be able to test it tomorrow. 

StepNFetchIT

unread,
Jan 31, 2017, 7:53:20 PM1/31/17
to accelstepper, rotod...@hotmail.com
I got a chance to test the code tonight, after a few changes I got it working. I found if I set the low speed any less than around 750 the motor would just move in short pulses? Running at 750 suits my application, but I am wondering why slow speeds seem to be such a challenge? I also found the stepper and the motor driver ran a bit hot with this code.


This is what I ended up with:

#include <AccelStepper.h>

// Define a stepper and the pins it will use

AccelStepper stepper(AccelStepper::HALF4WIRE, 8, 9, 10, 11); //half step, four wire bipolar stepper on digital pins 8,9,10,11

// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0

#define MAX_SPEED 1200

void setup()
{  
  stepper.setMaxSpeed(1200);
}

void loop()
{
  // Read new speed
  int analog_in = analogRead(ANALOG_IN);
  int speed = map(analog_in, 0, 1024, 775, MAX_SPEED);
  stepper.setSpeed(speed);
  stepper.runSpeed();
}

I also have another code I found that seems to more of a hand written micro stepper code, it seems to run a bit cooler and smoother but so far I just have it running at a fixed speed. 

Thanks again for helping me out with this, if you (or anyone) has suggestions for further refinement of the code I am open to suggestions.
Reply all
Reply to author
Forward
0 new messages