Arduino with Adafruit V1 Motor Shield and Potentiometer

Hello, first post as I am VERY new to this whole field. I am trying to control the speed of a stepper motor (two eventually) using a 10K potentiometer. I have the following components:

  • Arduino R3
  • Adafruit V1 Motor Shield L293D
  • Two Vexta Model C5919-9012 Bi-Polar stepper motors with 200 steps and 0.14A

I am powering the motors with an external 12VDC power source. I have also tried a 5V power source through the Arduino as well. The code I found passes the checks but there is no response from the motor.

Here is the wiring setup:

#include <AFMotor.h>

AF_Stepper stepper1(100, 1);

int analogPin = A0; // Pin analog

int val = 10; // variable to store the value read
int prepos = 0;
int pos = 0; //0 to 224

void setup()
{
   stepper1.setSpeed(10); 
   Serial.begin(9600);
}

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

   //pos = map (analogRead(analogPin), 0, 1023, 0, 200);
   
   // read pot and map to stepper speed
   int spd = map (analogRead(analogPin), 0, 1023, 0, 200);
   stepper1.setSpeed(spd);
   
   if (pos != prepos)
   {
      int diststep = pos;
      if (diststep > 0)
      {
         stepper1.step(diststep, FORWARD, DOUBLE);
      }
   }
}

Any help is greatly appreciated.

Welcome to the forum

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum

Verify input from the potentiometer (should range 0 to 1023).

Try an example from the AFmotor library...

Both are zero.

This might be part of your problem, not sure. Proper syntax is to define your pins. There are far more experienced people than I who can explain this, but it's like a variable declaration except that you don't define a type. The compiler just puts whatever you defined where you put the definition name. So in your case, wherever you type "analogPin", it would put A0 in its place without assigning A0 a type.

#define analogPin A0

Note that you do not put an equal sign between the name and the value, and you do not terminate it with a semicolon.

Another thing to note is that you never assign pos or prepos in your loop, so your if statement will never be true. You also never use

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

I'm not sure if that was leftover from one of the examples you used, though.

I believe that you mean that it is optional whether you #define or declare variables for your pins. If they are declared then it is good practice to make the variable a byte and declare it as const

Ah, yes, after reading some documentation, that is correct. I have only ever seen #define being used, but you are correct in that you could use

byte const analogPin = A0;

Thank you for the help. It worked with the following code:

/* Include the library */
#include <AccelStepper.h>

// Initialize motor pins (1 = setup with stepper driver)
AccelStepper stepper(1, 9, 8);  // step pin = 9, dir pin = 8

/* Set the analogue pin the potentiometer will be connected to. */
#define POT_PIN A0

/* Set a dead area at the centre of the pot where it crosses from forward to reverse */
#define DEADZONE 20

/* The analogue pin will return values between 0 and 1024 so divide this up between
   forward and reverse */
#define POT_REV_MIN 0
#define POT_REV_MAX (512 - DEADZONE)
#define POT_FWD_MIN (512 + DEADZONE)
#define POT_FWD_MAX 1024




void setup() {
  /* Set the number of steps to continuous so the the motor is always turning whilst
     not int the dead zone*/
  stepper.setMaxSpeed(1000);
  // Original set to 4000
  stepper.setSpeed(0);
}


void loop() {
  int Speed, Pot;

  /* Read the analogue pin to determine the position of the pot. */
  Pot = analogRead(POT_PIN);

  /* Is the pot in the reverse position ? */
  if (Pot >= POT_REV_MIN && Pot <= POT_REV_MAX) {
    Speed = map(Pot, POT_REV_MIN, POT_REV_MAX, 100, 10);
    // Original set to "Speed = map(Pot, POT_REV_MIN, POT_REV_MAX, 3000, 10)"
    stepper.setSpeed(-Speed);
    stepper.runSpeed();

    /* Is the pot in the forward position ? */
  } else if (Pot >= POT_FWD_MIN && Pot <= POT_FWD_MAX) {
    Speed = map(Pot, POT_FWD_MIN, POT_FWD_MAX, 10, 100);
    // Original set to "Speed = map(Pot, POT_FWD_MIN, POT_FWD_MAX, 10, 3000)"
    stepper.setSpeed(Speed);
    stepper.runSpeed();

    /* Is the pot in the dead zone ? */
  } else {
    Speed = 0;
  }}