Stepper motor on digital pins

I ran the onestepatatime and onerevolution examples on pwm pins 8,9,10,11 (Arduino mega)but i need

those pins for the lcd keypad shield. is it possible to run a stepper motor on digital pins 22-53?

yes.

your code should start with :

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {

note the line that says

initialize the stepper library on pins 8 through 11.....

pick the 4 pins on the mega you want to use on the next line.

so it would read something like :

Stepper myStepper(stepsPerRevolution, ?, ?, ?, ?);

since you listed 31 pins... I was not sure which you wanted to use.

I tried that bu the motor just hummed. The shaft did not rotate. The code is working only on PWM pins. What do I do??

I tried that bu the motor just hummed.

You modified your code; you didn't post it. Hmmm...

The code is working only on PWM pins.

The code you didn't post, driving a motor you didn't provide a link to, through a motor driver that you haven't even hinted about. Hmmm....

What do I do??

Give up. Find another hobby. Or, post some links, a schematic, and some code.

You're right PaulS. I will try to be more specific this time.

First I ran this code:

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// step one revolution  in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);

// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}

I connected the driver to the PWM pins 8,9,10,11 on the arduino mega.
Worked perfectly.

I also tried analog pins A8,A9,A10,A11.

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins A8 through A11:
Stepper myStepper(stepsPerRevolution, A8, A9, A10, A11);

void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// step one revolution  in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);

// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}

Worked again.

But since I need those pins for the lcd keypad shield, I tried using digital pins 22,24,26,28 with the following code:

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 22 through 28:
Stepper myStepper(stepsPerRevolution, 22, 24, 26, 28);

void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// step one revolution  in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);

// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}

This time the motor just hummed and shook but the shaft did not rotate. How can I make the stepper motor work on the digital pins?

The motor-Nema 17PM-K401-G6V

The driver - Elesof V01 with ULN2003 chip

How can I make the stepper motor work on the digital pins?

There are some pins, A0 to An, that can act as analog pins as well as digital pins. Since you are able to get the motor to work successfully when using some digital pins that can also function as analog pins, I think that it is safe to say that there must be something wrong with the order of the wires connected to the pins when you use 22, 24, 26, and 28.

Can you provide a direct link to the motor datasheet - your existing link points to several motors.

Please provide a link to the datasheet for your stepper driver. I suspect a ULN2003 is not suitable - but I could be wrong.

Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. Then I will study it. See How to use the Forum Your code is too long to study quickly.

Have a look through Stepper Motor Basics.

...R

parthaggarwal:
I tried that bu the motor just hummed. The shaft did not rotate. The code is working only on PWM pins. What do I do??

swap two pins. if the motor hums and you did not blow out your board, then you are not alternating coils properly.

the motor has 4 coils, you have 4 transistors.

you have to connect coil A side 1
then coil B side 1
then coil A side 2
then coil B side 2

alternate coils, then sides.

just a hum typically means you are alternating the same side of a coil, then alternating coils.

best to triple check your wiring so you do not blow anything out or short things to ground.

make sure you do not apply any more voltage to the motor than the nameplate. !!!!!
this is hugely important, with that driver, you cannot apply any higher voltage to the motor !!!!

parthaggarwal:
I tried that bu the motor just hummed. The shaft did not rotate. The code is working only on PWM pins. What do I do??

move other things, leave the motor whre it was.
move only to pwn pins and make sure the driver is usable for a mega.

Hi Pratham

I have same problem. Did you solve your problem ?
I am trying to use same pins on Mega (22,24,26,28) and motor is NOT moving.

When I use PWM pings with same motor (4,5,6,7) it works fine !
Other motor connected to PWM pins (8,9,10,11) moves fine too.

The motor does not need PWM pins, the Stepper.h library takes care of the timing of the pulses.
For example, I have 7 stepper motors each using 4 pins from an Atmega1284P, only 6 of the pins are PWM capable - yet all 7 motors are moving:
https://www.youtube.com/watch?v=5-9pqTQP_SQ

The order you have the wiring is likely incorrect.
For example:
29, 27, 28, 26 works, while 29,28,27,26 does not.
The coils of the motor are driven in pairs; if your choice of pins drive CoilA+ with CoilB-, and CoilA- with CoilB+, the motor will not move.

Non-PWM pins will work. I am watching a motor spin right now from 17,15,16,14.
I moved them to 57, 55, 56, 54, which are A3 to A0, and it is also spinning.

So try changing the pins to 22,26,24,28.

yogitalekar:
Hi Pratham

I have same problem. Did you solve your problem ?
I am trying to use same pins on Mega (22,24,26,28) and motor is NOT moving.

When I use PWM pings with same motor (4,5,6,7) it works fine !
Other motor connected to PWM pins (8,9,10,11) moves fine too.

@yogitalekar, did you try that last thing @CrossRoads wrote?

CrossRoads:
Non-PWM pins will work. I am watching a motor spin right now from 17,15,16,14.
I moved them to 57, 55, 56, 54, which are A3 to A0, and it is also spinning.

So try changing the pins to 22,26,24,28.

Greetings.

@raym3d, this is a very old Thread. I have answered your question in your own Thread

...R