I need a stepper motor with a potentiometer for variable speed control. I have a motor and set up very similar to this one. Using BIG Stepper Motors with Arduino | DroneBot Workshop
I copied his code and it works kind of, but the motor doesn't shut off completely, and runs very rough until speed is about 1/3 the way up. I would like the motor to shut off completely with no holding tuque when the pot is turned close to all the way down. If anyone knows how to adjust the code to shut off completely and maybe start up a little smoother I'd appreciate it.
[code]
/*
Stepper Motor Test
stepper-test01.ino
Uses MA860H or similar Stepper Driver Unit
Has speed control & reverse switch
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Defin pins
int reverseSwitch = 2; // Push button for reverse
int driverPUL = 9; // PUL- pin
int driverDIR = 8; // DIR- pin
int spd = A1; // Potentiometer
// Variables
int pd = 500; // Pulse Delay period
boolean setdir = LOW; // Set Direction
// Interrupt Handler
void revmotor (){
setdir = !setdir;
}
void setup() {
pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
}
void loop() {
pd = map((analogRead(spd)),0,1023,2000,50);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
[/code]
Could I use this code with a bigger driver like a TB6600? If I connect the enable pin then how could I wright a command to shut off the motor when the potentiometer is at min. or just before so there is a little dead zone?
[code]
/*
* Basic example code for controlling a stepper with the AccelStepper library
*
* by Dejan, https://howtomechatronics.com
*/
#include <AccelStepper.h>
// Define the stepper motor and the pins that is connected to
AccelStepper stepper1(1, 2, 5); // (Type of driver: with 2 pins, STEP, DIR)
void setup() {
// Set maximum speed value for the stepper
stepper1.setMaxSpeed(1000);
}
void loop() {
stepper1.setSpeed((analogRead(A0));
// Step the motor with a constant speed previously set by setSpeed();
stepper1.runSpeed();
}
[/code]
#include <AccelStepper.h>
// Define the stepper motor and the pins that is connected to
AccelStepper stepper1(1, 8, 9); // (Type of driver: with 2 pins, STEP, DIR)
void setup() {
// Set maximum speed value for the stepper
stepper1.setMaxSpeed(1000);
}
void loop() {
int potValue = analogRead(A0);
if (potValue) { // if not zero // run code stepper1.setSpeed((analogRead(A1));
// Step the motor with a constant speed previously set by setSpeed();
stepper1.runSpeed();
} else {
// disable code
}
}
Still nothing, there's holding torque but no movement when I turn the pot. the opposite of what I need.
this code works but is very rough at low speed and doesn't shut off completely. I have tried other codes with this setup (one that goes then stops and reverses) it they all work fine so it's not the set up.
Basic example code for controlling a stepper without library
by Dejan, https://howtomechatronics.com
*/
// defines pins
#define stepPin 2
#define dirPin 5
int customDelay, customDelayMapped;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
speedControl();
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Custom function for reading the potentiometer and mapping its value from 300 to 3000, suitable for the custom delay value in microseconds
void speedControl() {
customDelay = analogRead(A0); // Read the potentiometer value
customDelayMapped = map(customDelay, 0, 1023, 300, 3000); // Convert the analog input from 0 to 1024, to 300 to 3000
}
type or paste code here
Which motor do you have?
Which motor driver do you have and how is it set up?
Which power supply do you have, volts and amps?
Which Arduino or other "compatible" MCU board do you have?
This is the code I use for the motors connected to a joystick. Same driver and pin set up. I just need another motor that only goes in one direction instead of forward and reverse. If I could modify this code so that the motor is off when the pot is at min and full speed max. Do you know how that could be done?
[code]
#include <AccelStepper.h> // accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)
//Pins
const byte Analog_X_pin = A1; // x-axis readings
const byte enablePin = 7;
//Variables
int Analog_X = 0; //x-axis value
int Analog_X_AVG = 0; //x-axis value average
//----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
//----------------------------------------------------------------------------
//PINS
pinMode(Analog_X_pin, INPUT);
pinMode(enablePin, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
//----------------------------------------------------------------------------
InitialValues(); // averaging the values of analog pin 0 (value from potmeter)
//----------------------------------------------------------------------------
// Stepper parameters
// setting up some default values for maximum speed and maximum acceleration
stepper.setMaxSpeed(2000); //SPEED = Steps / second
stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
stepper.setSpeed(1000);
delay(1000);
}
void loop()
{
ReadAnalog();
ReadLimits();
stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
}
void ReadLimits()
{
if( digitalRead(2)==LOW && stepper.speed()>0 )//read input, assuming they have pull-up resistors.
{
stepper.setSpeed(0);
}
if( digitalRead(3)==LOW && stepper.speed()<0 )//read input, assuming they have pull-up resistors.
{
stepper.setSpeed(0);
}
}
void ReadAnalog() {
if (abs(Analog_X-Analog_X_AVG) > 50) {
digitalWrite(enablePin, HIGH); // enable the driver
stepper.setSpeed(5*(Analog_X-Analog_X_AVG));
} else {
digitalWrite(enablePin, LOW); // disable the driver
stepper.setSpeed(0);
}
// Reading the potentiometer in the joystick:
Analog_X = analogRead(Analog_X_pin);
// if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
// This is a sort of a filter for the inaccuracy of the reading
if (abs(Analog_X-Analog_X_AVG) > 50) {
stepper.setSpeed(5 * (Analog_X-Analog_X_AVG));
} else {
stepper.setSpeed(0);
}
}
void InitialValues() {
//Set the values to zero before averaging
float tempX = 0;
//----------------------------------------------------------------------------
// read the analog 50x, then calculate an average.
// they will be the reference values
for (int i = 0; i<50; i++) {
tempX += analogRead(Analog_X_pin);
delay(10); //allowing a little time between two readings
}
//----------------------------------------------------------------------------
Analog_X_AVG = tempX/50;
//----------------------------------------------------------------------------
Serial.print("AVG_X: ");
Serial.println(Analog_X_AVG);
Serial.println("Calibration finished");
}
[/code]