# Stepper motor problem with mega 2560

**URL:** <https://forum.arduino.cc/t/stepper-motor-problem-with-mega-2560/1037409>\
**Category:** Programming\
**Created:** [October 1, 2022, 3:22pm UTC](https://forum.arduino.cc/t/stepper-motor-problem-with-mega-2560/1037409 "2022-10-01T15:22:13Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [October 1, 2022, 4:17pm UTC](https://forum.arduino.cc/t/stepper-motor-problem-with-mega-2560/1037409/7 "2022-10-01T16:17:42Z")

</div>

> [@scorpio21](#):
>
> ```arduino
> for(int x = 0; x < stepsPerRevolution; x++)
> {
> analogWrite(stepPin, highValue);
> //You have to use delay because it won´t work without it
> delay(10);
> analogWrite(stepPin, lowValue);
> //delay(1);
> }
> 
> ```

That is not code for a stepper motor. The analogWrite function can be used to control the speed of a DC motor, not a stepper. To see how to write code to control a stepper see [Robin2's simple stepper code tutorial.](https://forum.arduino.cc/t/simple-stepper-program/268292)

> [@Simple Stepper Program](https://forum.arduino.cc/t/simple-stepper-program/268292):
>
> I wrote a very simple program to demonstrate stepper control using a Polulu A4988 stepper driver. It should also work with any stepper driver that just needs step and direction signals from an Arduino. I have included it in response to several queries and it seemed it would be easier to create this Thread and then just add a link to it. Edit 10 Dec 2014 - readers may also be interested in this note about [Stepper Motor Basics](http://forum.arduino.cc/index.php?topic=284828) If all you want to do is demonstrate that a motor can work this code…

Or use a library like[the AccelStepper library](https://www.airspayce.com/mikem/arduino/AccelStepper/) or [the MobaTools stepper library](https://github.com/MicroBahner/MobaTools). Both libraries have documentation and example code to help to learn to use them. Personally, I think the MobaTools library easier to learn. Both libraries are available via the IDE library manager.

---

_[View the full topic](https://forum.arduino.cc/t/stepper-motor-problem-with-mega-2560/1037409)._
