stepper twitching

Hi all
This is my first attempt at arduino programming and motor control.
I have a problem with “twitching and buzzing” of the stepper.
I have a NEMA17 motor rated 1.5A
I have 2 power sources
1 at 4.5v
and one at 12v 1.5A

when i uploaded the code to the arduino without any power other than the 5v from the usb suprisinly the motor ran as i hoped but, as it was twitching between movement i figured it was the low power.
when i attatxhed the power to the Adafruit motor driver the motor lost its original task and moved erratically.

the code:

#include <AFMotor.h>
#include <AccelStepper.h>

AF_Stepper motor1(200, 1);
int analogPin = 0; // potentiometer wiper (middle terminal)
//connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
int prepos = 0;
int pos = 100; //0 to 200 .
void setup()
{
motor1.setSpeed(125); // 30 rpm
}

void loop() {

prepos = pos;
val = analogRead(analogPin); // read the input pin

pos = map (val, 0, 1030, 0, 2000);

if (pos != prepos){
int diststep = pos - prepos;
if (diststep < 0) {
motor1.step(-diststep, BACKWARD, SINGLE);
}
if (diststep > 0) {
motor1.step(diststep, FORWARD, SINGLE);
}

}

}

what do i do?
thanks

some things i should have added to help with my question im using a Vishay 10 turn pot model 534

im wondering if the twitching even at 12v is due to the pot being a 10turn one.

i dont have a normal one to try.

I would expect any potentiometer to give slightly varying readings no matter what type it is.

You'll need to add code to smooth out the readings from the potentiometer. Typically this would entail taking the average of ~10 successive readings, or perhaps you might want to ignore any reading that is less than ~4 different than your current reading. Or both.

Do you have the ground side odf the Arduino and your power supply wired (tied) together?

hi guys kf2qd no i dont have the grounds tied together.

chagrin how would i code this?

thanks si

Connect a wire between the grounds of both power supplies. This must always be done.

To smooth a reading:

  pos = 0;
  for (int ctr = 0; ctr < 10; ctr++) {
    val = analogRead(analogPin);    // read the input pin
    pos = pos + map (val, 0, 1030, 0, 2000);
  }
  pos = pos / 10;

Basically you’re taking the reading 10 times, summing them up, then dividing the final result by 10 to get the average. That’s not very efficient code but it should be straightforward I hope.

Hi

Thanks chragrin.

Im still getting twitch on some areas of the pot position, i wrote in the code below a serial print so i could see the pots performance and its okay but there are areas where the number jumps back and forth by 1, this of course is not divisiable, i wonder if there is a way to tell the arduino to stop powering the stepper once the pot has stopped moving, i.e
if the arduino reads the pots position being either 550 and 551 (or fluctuating by 1) then stop the program untill the pot reads ±2 that way the motor isnt getting constant power.

#include <AFMotor.h>
#include <AccelStepper.h>

AF_Stepper motor1(200, 1);
int analogPin = 0; // potentiometer wiper (middle terminal)
//connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
int prepos = 0;
int pos = 1023; //0 to 200 .
void setup()
{
motor1.setSpeed(100); // 30 rpm
Serial.begin(9600);
}

void loop() {

prepos = pos;
val = analogRead(analogPin); // read the input pin

pos = 0;
for (int ctr = 0; ctr < 100; ctr++) {
val = analogRead(analogPin); // read the input pin
pos = pos + map (val, 0, 1023, 0, 2000);
}
pos = pos / 100;

pos = map (val, 0, 1023, 0, 2000);

if (pos != prepos){
int diststep = pos - prepos;
if (diststep < 0) {
motor1.step(-diststep, BACKWARD, INTERLEAVE);
}
if (diststep > 0) {
motor1.step(diststep, FORWARD, INTERLEAVE);
}
int sensorvalue = analogRead(A0);
Serial.println(sensorvalue);
delay(10);

}
Serial.flush();

}