Simple Stepper Motor Programming

I am trying to use a Stepper Motor, A4988 and Potentiometer Speed Control who was setup
by someone else. The setup was working fine until it was unplugged and left for a few weeks and now since trying to turn it back on, it's not doing anything. I have no copy of the original code that was used.

I've found some basic code from howtomechatronics but I'm not having any luck.

// 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
}

The motor is powered from a 24V external power supply and when everything is turned on, the motor does not react.

Any support or advice would be great, thank you!

@gevo01
Looks like a custom designed motor shield.
Do you have the schematic for it?

@jim-p
I don't believe it's custom, I was told everything was off the shelf (I may be incorrect).

This was what I believe he was trying to replicate:

Custom or not, do you have the schematic?

@jim-p
I was left with that, but I don't think that's the correct schematic.

It's not the schematic.
I did a search for 22185-stepper driver, came up with nothing.
What you need to do is disassemble everything and use an ohmmeter to see which points on the shield are connected to which Arduino pins.

Your code uses pin 2 as the stepPin, as does the diagram in post #4.

However looking at the photos of your 22185- Stepper Driver PCB, I can see that pin 2 doesn't seem to be used.


I can see connections from Arduino digital pins 4, 5, and 7 going towards the daughter board.
You need to check which pins they connect to on the plug in driver board, and then adjust the code accordingly.

@JohnLincoln Thanks for the feedback, neither of the pins is making a difference with the code.

A4988 connections

STEP 4   DIR 5   ENABLE 7

Looking at the A4988 connection diagram that I found at https://www.pololu.com/product/2128 , together with your photos, it looks to me as though Arduino digital pin 4 is connected to the step pin.

(Arduino digital pin 5 looks as though it is connected to the dir pin, which is what the code expects.)

Check whether that is the case.

If it is then you need to change

#define stepPin 2

to

`#define stepPin 4`

EDIT:
Annotated photo added.

Now that I have seen the new photo that you included in post #9, I can see that pin 7 goes to the !ENABLE pin on the A4988 driver board.

Pin 7 needs to be taken low to enable the stepper motor driver.

1 Like

Try this modified code, which takes pin 7 low to enable the A4988 driver, and uses pin 4 for the stepPin.

// defines pins
#define stepPin 4
#define dirPin 5
#define enablePin 7

int customDelay, customDelayMapped;

void setup() {
  // Sets the three pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enablePin, OUTPUT);

  // take the enablePin LOW, to enable the A4988 driver
  digitalWrite(enablePin, LOW);
}
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
}

Perfect, thanks that's solved it and is now working!!

The potentiometer is controlling the speed, however, it's only rotating a few degrees and not doing a full rotation.

Is there a lot of work involved with modifying the code to make it rotate to a certain position then stop?

That's good to hear.

That seems odd, the code should make it run continuously.

I would've thought so too, because I don't see anything that would limit its rotation. Here's a video of what it's doing and a picture of the wiring. I've since crimped all the cables but I highlight doubt that would've changed anything?

Watch Stepper Motor Twitching | Streamable

I don't see any hardware there to tell the stepper where it is. One solution in such cases is a limit switch and you have to use it to index the motor on startup.

Sorry, I misworded what I was suggesting. What I was trying to get at was that I want the motor to fully rotate as opposed to only rotating a few degrees then stopping.

Well, you would need something (a switch?) to trigger the rotation, but the code would be pretty simple.

Obviously, you need to get what you have working first though.

From what I can see in the video it is not rotating at all, only twitching.
Did you check the wiring motor<->A4988 to have the motor coils correctly connected to the driver? Do you know which wires belong to one coil respectively?

Remove the A4988 from the shield and directly connect devices (and protection components) to it.

These are the wires to the coils.
image

A and B define the pairs, so orange and blue should both be B say. As you have it they're connected to different coils.

1 Like