Is there an example sketch somewhere that allows control of a stepper motor using a potentiometer? Essentially I would like to use the Arduino>examples>stepper>MotorKnob sketch with the Adafruit Motor Shield V2. Once I got that I think I will be able to advance and continue experimenting but for now I am stuck.
Thanks
Adafruit Motor Shield V2 stepper knob
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- Franklin97355
- Posts: 23911
- Joined: Mon Apr 21, 2008 2:33 pm
Re: Adafruit Motor Shield V2 stepper knob
What is it you want the pot to do in controlling the stepper? Have you looked into a rotary encoder as a control device?
-
- Posts: 7
- Joined: Sun Mar 17, 2013 2:32 am
Re: Adafruit Motor Shield V2 stepper knob
well... when a pot is connected to an analog pin and say 4 wires of a bipolar stepper are wired to an H-bridge and digital pins 8,9,10,11 the example sketch allows the user to control the steppers rotation/position in relation to that of the potentiometer... but since the adafruit motor shield library is different from the <stepper.h> I am not able to achieve the same result.What is it you want the pot to do in controlling the stepper? Have you looked into a rotary encoder as a control device?
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Adafruit Motor Shield V2 stepper knob
If you post the code you have so far, we can help you adapt it to the Adafruit library.
-
- Posts: 7
- Joined: Sun Mar 17, 2013 2:32 am
Re: Adafruit Motor Shield V2 stepper knob
So this is what I want to happen... but I haven't been able to get it to work using the V2 motor shield and it's libraries:
Adafuit_MotorShield ...
Adafuit_MotorShield ...
Code: Select all
/*
* MotorKnob
*
* A stepper motor follows the turns of a potentiometer
* (or other sensor) on analog input 0.
*
* http://www.arduino.cc/en/Reference/Stepper
* This example code is in the public domain.
*/
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 100
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);
// the previous reading from the analog input
int previous = 0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);
}
void loop()
{
// get the sensor value
int val = analogRead(0);
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);
// remember the previous value of the sensor
previous = val;
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Adafruit Motor Shield V2 stepper knob
Something like this...
Code: Select all
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(10); // 10 rpm
}
int previous;
void loop()
{
// get the sensor value
int val = analogRead(0);
// move a number of steps equal to the change in the
// sensor reading
int steps = val - previous;
if (steps > 0)
{
myMotor->step(-steps, BACKWARD, DOUBLE);
}
else if (steps < 0)
{
myMotor->step(steps, FORWARD, DOUBLE);
}
// remember the previous value of the sensor
previous = val;
}
-
- Posts: 7
- Joined: Sun Mar 17, 2013 2:32 am
Re: Adafruit Motor Shield V2 stepper knob
So I tried something like that also but the problem is that the code gets stuck inside of each if/case... For example if my Pot reads 0 as soon as I upload the sketch the stepper stays stationary. As soon as I turn the pot the stepper enters the "(steps > 0)" and only loops in the "BACKWARDS" direction meaning the pot is now moot. when the sketch is uploaded and the pot is at any position from 0 to 1023, a positive number, the stepper again sticks in the "BACKWARDS" direction.Something like this...
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Adafruit Motor Shield V2 stepper knob
That, or something like it? If your code is different, post it.So I tried something like that also
-
- Posts: 7
- Joined: Sun Mar 17, 2013 2:32 am
Re: Adafruit Motor Shield V2 stepper knob
my apologies, that was unclear of me
I did try the exact same code.
I did try the exact same code.
-
- Posts: 7
- Joined: Sun Mar 17, 2013 2:32 am
Re: Adafruit Motor Shield V2 stepper knob
Ok so in this video it looks like the setup has the effect I am looking for...
https://www.youtube.com/watch?v=fwm6FqZ-6cY
hopefully this gives a better idea of what i am trying to achieve.
also in the meantime I might just try to use 2 button switches to move my stepper forward and backwards.
https://www.youtube.com/watch?v=fwm6FqZ-6cY
hopefully this gives a better idea of what i am trying to achieve.
also in the meantime I might just try to use 2 button switches to move my stepper forward and backwards.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Adafruit Motor Shield V2 stepper knob
OK. I had a typo there and reversed the sign on steps. Try this one.
Code: Select all
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
int previous;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(10); // 10 rpm
previous= analogRead(0);
}
void loop()
{
// get the sensor value
int val = analogRead(0);
// move a number of steps equal to the change in the
// sensor reading
int steps = val - previous;
if (steps > 0)
{
myMotor->step(steps, FORWARD, DOUBLE);
}
else if (steps < 0)
{
myMotor->step(-steps, BACKWARD, DOUBLE);
}
// remember the previous value of the sensor
previous = val;
}
-
- Posts: 7
- Joined: Sun Mar 17, 2013 2:32 am
Re: Adafruit Motor Shield V2 stepper knob
HEY! this looks pretty good! its a little jittery at times when I want it to stop/hold its position but otherwise this is great! I will keep playing around with it and try microstepping.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Adafruit Motor Shield V2 stepper knob
Jitter is likely due to noise on the analog pin. This is pretty typical. Microstepping will make it less noticeable. You can also minimize it by taking multiple readings and using the average.
- adaman
- Posts: 3
- Joined: Tue Nov 22, 2016 11:43 pm
Re: Adafruit Motor Shield V2 stepper knob
Code: Select all
[quote="trickstersss"]HEY! this looks pretty good! its a little jittery at times when I want it to stop/hold its position but otherwise this is great! I will keep playing around with it and try microstepping.[/q[quote="trickstersss"]HEY! this looks pretty good! its a little jittery at times when I want it to stop/hold its position but otherwise this is great! I will keep playing around with it and try microstepping.[/quote]
Trying your code[code]
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Trying to use this code, but compiler error message "myMotor not declared in this scope" I have specified steps (400) and port(2). What am I missing?
Code: Select all
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
int previous;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(10); // 10 rpm
previous= analogRead(0);
}
void loop()
{
// get the sensor value
int val = analogRead(0);
// move a number of steps equal to the change in the
// sensor reading
int steps = val - previous;
if (steps > 0)
{
myMotor->step(steps, FORWARD, DOUBLE);
}
else if (steps < 0)
{
myMotor->step(-steps, BACKWARD, DOUBLE);
}
// remember the previous value of the sensor
previous = val;
}
[/quote][quote="trickstersss"]/*
* MotorKnob
*
* A stepper motor follows the turns of a potentiometer
* (or other sensor) on analog input 0.
*
* http://www.arduino.cc/en/Reference/Stepper
* This example code is in the public domain.
*/
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 100
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);
// the previous reading from the analog input
int previous = 0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);
}
void loop()
{
// get the sensor value
int val = analogRead(0);
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);
// remember the previous value of the sensor
previous = val;
}[/quote][/code]
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Adafruit Motor Shield V2 stepper knob
@adaman - is there a question in there somewhere?
Please be positive and constructive with your questions and comments.