# Simple Stepper Motor Programming

**URL:** <https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [July 23, 2024, 9:24am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379 "2024-07-23T09:24:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gevo01](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gevo01](https://forum.arduino.cc/u/gevo01)\
**Post date:** [July 23, 2024, 9:24am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/1 "2024-07-23T09:24:06Z")

</div>

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.

```arduino
// 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!

 ![Image (4)](https://europe1.discourse-cdn.com/arduino/original/4X/5/b/0/5b05db51d6e72ffe9fd4d7d61d3bf0e0e4f01a63.jpeg)  
 ![Image (5)](https://europe1.discourse-cdn.com/arduino/original/4X/5/2/5/5254558f92d0799d0499394f957ed0d6d1f51954.jpeg)  
 ![Image (6)](https://europe1.discourse-cdn.com/arduino/original/4X/c/4/f/c4fce27ba8d6889573601d50cb914a24366ca22c.jpeg)

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [July 23, 2024, 9:33am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/3 "2024-07-23T09:33:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gevo01](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gevo01](https://forum.arduino.cc/u/gevo01)\
**Post date:** [July 23, 2024, 9:42am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/4 "2024-07-23T09:42:43Z")

</div>

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

 ![Image (7)](https://europe1.discourse-cdn.com/arduino/original/4X/4/b/e/4be5d4eec9d3a7ae8d2ef696cb84be105d038871.jpeg)

This was what I believe he was trying to replicate:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/3/6/9/36988abe3d32646314137597a0b1982b061f51bb.jpeg)

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [July 23, 2024, 9:45am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/5 "2024-07-23T09:45:53Z")

</div>

Custom or not, do you have the schematic?

---

<div class="post-metadata">

**Author:** ![gevo01](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gevo01](https://forum.arduino.cc/u/gevo01)\
**Post date:** [July 23, 2024, 9:46am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/6 "2024-07-23T09:46:55Z")

</div>

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

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/6/1/561357e0b4b9178595ba3387414422f57e095372.jpeg)

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [July 23, 2024, 9:53am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/7 "2024-07-23T09:53:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [July 23, 2024, 10:13am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/8 "2024-07-23T10:13:24Z")

</div>

> [@gevo01](#):
>
> ```arduino
> // defines pins
> #define stepPin 2
> #define dirPin 5
> 
> ```

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.

 ![22185 Stepper Driver](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/4/074737ed31409410bb654c17672b837f658d3fea.jpeg)  
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.

---

<div class="post-metadata">

**Author:** ![gevo01](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gevo01](https://forum.arduino.cc/u/gevo01)\
**Post date:** [July 23, 2024, 10:35am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/9 "2024-07-23T10:35:08Z")

</div>

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

 ![eba48ccc-e8e1-4b01-a614-1838c5aa826a](https://europe1.discourse-cdn.com/arduino/original/4X/7/2/4/724501792623286f1da044b56d6b04e50290a929.jpeg)  
 ![f36a1fa6-6ee8-4064-90ce-339560d3e7d1](https://europe1.discourse-cdn.com/arduino/original/4X/e/3/4/e34ec36cb070f879fbd928bdd9a2a85df8bf9baa.jpeg)

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [July 23, 2024, 10:46am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/10 "2024-07-23T10:46:04Z")

</div>

![A4988 connections](https://europe1.discourse-cdn.com/arduino/original/4X/6/3/f/63f97c58961f9acb0fd36f2e59165f652719a067.png)

![STEP 4 DIR 5 ENABLE 7](https://europe1.discourse-cdn.com/arduino/original/4X/c/9/5/c95dcd4a6f6a7312f122b3c1a262007f78ca54c2.jpeg)

Looking at the A4988 connection diagram that I found at [https://www.pololu.com/product/2128](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

```arduino
#define stepPin 2

```

to

```arduino
`#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.

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [July 23, 2024, 11:27am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/11 "2024-07-23T11:27:00Z")

</div>

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

> ****
>
> ```arduino
> // 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
> }
> ```

---

<div class="post-metadata">

**Author:** ![gevo01](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gevo01](https://forum.arduino.cc/u/gevo01)\
**Post date:** [July 23, 2024, 11:45am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/12 "2024-07-23T11:45:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [July 23, 2024, 12:06pm UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/13 "2024-07-23T12:06:29Z")

</div>

> [@gevo01](#):
>
> Perfect, thanks that's solved it and is now working!!

That's good to hear.

> [@gevo01](#):
>
> The potentiometer is controlling the speed, however, it's only rotating a few degrees and not doing a full rotation.

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

---

<div class="post-metadata">

**Author:** ![gevo01](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gevo01](https://forum.arduino.cc/u/gevo01)\
**Post date:** [July 23, 2024, 12:16pm UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/14 "2024-07-23T12:16:03Z")

</div>

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](https://streamable.com/gh1u0x)

 ![c2dabb57-c3b7-4238-ab49-6787046c2b06](https://europe1.discourse-cdn.com/arduino/original/4X/1/4/f/14f45916830ab3e9841d8237bd867120f5ce3593.jpeg)

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [July 23, 2024, 12:23pm UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/15 "2024-07-23T12:23:43Z")

</div>

> [@gevo01](#):
>
> Is there a lot of work involved with modifying the code to make it rotate to a certain position then stop?

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.

---

<div class="post-metadata">

**Author:** ![gevo01](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gevo01](https://forum.arduino.cc/u/gevo01)\
**Post date:** [July 23, 2024, 12:37pm UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/16 "2024-07-23T12:37:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [July 23, 2024, 12:44pm UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/17 "2024-07-23T12:44:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [July 23, 2024, 3:08pm UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/18 "2024-07-23T15:08:21Z")

</div>

> [@gevo01](#):
>
> The potentiometer is controlling the speed, however, it's only rotating a few degrees and not doing a full rotation.

> [@gevo01](#):
>
> Here's a video of what it's doing

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?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [July 24, 2024, 3:24am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/19 "2024-07-24T03:24:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gevo01](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gevo01](https://forum.arduino.cc/u/gevo01)\
**Post date:** [July 24, 2024, 6:45am UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/20 "2024-07-24T06:45:48Z")

</div>

These are the wires to the coils.  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/e/d/1/ed16c7a531bee48046cd30dc487efe739fe817bd.png)

 ![Image (7)](https://europe1.discourse-cdn.com/arduino/original/4X/4/b/e/4be5d4eec9d3a7ae8d2ef696cb84be105d038871.jpeg)

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [July 24, 2024, 12:48pm UTC](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379/21 "2024-07-24T12:48:22Z")

</div>

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.

[Next page](https://forum.arduino.cc/t/simple-stepper-motor-programming/1284379.md?page=2)
