Hi, trying to set the speed of a stepper motor using a 50K slide potentiometer. I want it to be stopped completely when slide is in one end, end full speed at the other end.
Hardware:
Arduino UNO
Arduino motor shield
Slider 50K (pins 1, 2, 2, 3)
Bipolar stepper 3V, 0,9°, 0,41Nm
Pot is connected:
Pin 1 to 5V
Pin 2 to analog in A0
Pin 3 ground
Motor is connected to the +/- +/-
External power 3V and the Vin connect is cut
First tried with this code to get the motor running, works fine:
Then I wanted to add the speed control so I changed some of the code using this tutorial:
My code now looks like this and is not working, the motor runs only when slider is in one direction otherwise it just skips around:
// Include the Stepper library:
#include <Stepper.h>
// Define number of steps per revolution:
const int stepsPerRevolution = 400;
// Give the motor control pins names:
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);
void setup() {
// Set the PWM and brake pins so that the direction pins can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
}
void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}
Please read the post at the start of any forum , entitled "How to use this Forum".
OR https://forum.arduino.cc/index.php?topic=712198.0
Then look down to "code problems" about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Good to see you have got the stepper working with some code.
Maybe so? I din't think I set the actual speed somewhere in the code, just that it should vary between max and min. But now when I think about It the Arduino doesn't know the maximum speed of the motor. It's not clear when I look in the spec.sheet of the motor either. It's a "Wantmotor" product: 42BYGHM809
As it is now, it spins a little at one speed when slider is on one side. But if I slide it to the other side it's just going "ghrmmm" in one place vibrating the same no matter where the slider is.
Hi,
Can you try this edit of your code please.
When you have uploaded it, open the IDE "monitor", set the baud to 115200, see what it shows as you adjust the slider.
Do you have a DMM?
// Include the Stepper library:
#include <Stepper.h>
// Define number of steps per revolution:
const int stepsPerRevolution = 400;
// Give the motor control pins names:
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);
void setup() {
Serial.begin(115200);
// Set the PWM and brake pins so that the direction pins can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
}
void loop() {
delay(100);
// read the sensor value:
int sensorReading = analogRead(A0);
Serial.print("Pot Reading = ");
Serial.print(sensorReading);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
Serial.print(" MotorSpeed = ");
Serial.println(motorSpeed);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}
Hi Tom, now the motor at least turns around all the time. When the slider is closest to 5V it moves a little faster, and for all the rest it moves at a constant speed. Just like the IDE monitor shows. It seems that the signal from pot doesn't change when sliding, until I hit the end.
Looks like this in the IDE:
Pot Reading = 72 MotorSpeed = 7
Pot Reading = 76 MotorSpeed = 7
Pot Reading = 80 MotorSpeed = 7
Pot Reading = 81 MotorSpeed = 7
Pot Reading = 75 MotorSpeed = 7
Pot Reading = 76 MotorSpeed = 7
Pot Reading = 82 MotorSpeed = 8
Pot Reading = 76 MotorSpeed = 7
Pot Reading = 74 MotorSpeed = 7
Pot Reading = 79 MotorSpeed = 7
Pot Reading = 73 MotorSpeed = 7
Pot Reading = 705 MotorSpeed = 68
Pot Reading = 986 MotorSpeed = 96
Pot Reading = 988 MotorSpeed = 96
Pot Reading = 990 MotorSpeed = 96
Pot Reading = 991 MotorSpeed = 96
Pot Reading = 992 MotorSpeed = 96
Pot Reading = 992 MotorSpeed = 96
Pot Reading = 992 MotorSpeed = 96
Pot Reading = 993 MotorSpeed = 97
Pot Reading = 993 MotorSpeed = 97
Pot Reading = 993 MotorSpeed = 97
Can you post a picture of your pot please.
Your DMM in DC Volts mode, should be able to measure the volts on the input pin connected to the pot with respect to gnd.
Where do each of the coloured wires go?
You only need three connections.
I think
RED should go to 5v of your controller.
WHITE should go to gnd of your controller.
GREEN to your analog input of your controller.
The grey is not needed.
Can you post and image showing your pot, controller and wire connections?
What I did:
Changed the connections as you suggested, changed input to A2 and also in the code.
Now I just need a faster stepper for my application I noticed Only way to speed this up is by increasing the voltage I guess
Thanks both @TomGeorge and @gcjr for helping me out here, very nice first impression of this forum!
Edit, final code:
/* Example sketch to control a stepper motor with Arduino Motor Shield Rev3, Arduino UNO and Stepper.h library. More info: https://www.makerguides.com */
// Include the Stepper library:
#include <Stepper.h>
// Define number of steps per revolution:
const int stepsPerRevolution = 400;
// Give the motor control pins names:
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);
void setup() {
// Set the PWM and brake pins so that the direction pins can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
}
void loop() {
// read the sensor value:
int sensorReading = analogRead(A2);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}
@lastchancename No not really for this application, but I had one laying and was interested in learning about steppers for another project. So yes, I was thinking about a DC motor would be better and also for speed.