Stepper motor problem with mega 2560

Hello. I have stepper motors that I want to control , however I´ve ran into little problem. I tried stepper code on cnc shield with arduino uno and now I wanted to control stepper through arduino mega 2560 and ramps 1.4. I got 12V 30amps power supply and I changed pins for controling steppers to able to work with mega 2560. And now here is my problem. I set my enviroment and I uploaded code , but my stepper motor hasn´t started movin´.

Can you help me out?

thanks for all the answers.

//here is my code:

//specify step pin, dir pin and steps per revolution
const int dirPin = A7;
const int stepPin = A6;
const int stepsPerRevolution = 200;

//specify analog outputs "HIGH" && "LOW"
const int highValue = 255;
const int lowValue = 0; 

void setup()
{
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop()
{
// Set motor direction clockwise
  digitalWrite(dirPin, LOW);
 
  // Spin motor slowly
  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);
 }
}

I forgot to mention that I´m working with y axis on ramps 1.4

The ramps 1.4 board is intended for 3D printing. That may be the cause of your problem?
If you want to use a Mega, perhaps the CNC shield can be mounted on it. I thought the pinout for the first pins is the same.

I don´t think so because it is still arduino and I should be able to control it like any other arduino

Y_step_pin = 60
Y_dir_pin = 61
Y_enable_pin = 56
Y_cs_pin = 49

tells my pins_ramps.h so a2 must be low and perhaps reset and sleep not connected?

I'll try it out ,but on my ramps isn't pin 60

Dňa so 1. 10. 2022, 17:50 Frits1956 via Arduino Forum <notifications@arduino.discoursemail.com> napísal(a):

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.

Or use a library like the AccelStepper library or the MobaTools stepper library. 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.

Here is a modified code from the Robin2's tutorial that has been tested, successfully, on my Mega with a Ramps 1.4 shield, a 200 step per rev. NEMA 17 stepper and a DRV8825 driver in the Y axis. Driver set to full steps. The motor turns, slowly, one revolution CW then one revolution CCW and so on.

// testing a stepper motor with a Pololu DRV8825 driver board or equivalent
// on an Mega the onboard led will flash with each step
// this version uses delay() to manage timing

const byte directionPin = A7;
const byte stepPin = A6;
const byte enablePin = A2;
int numberOfSteps = 200;
const byte ledPin = 13;
int pulseWidthMicros = 20;  // microseconds
int millisbetweenSteps = 50; // milliseconds - or try 1000 for slower steps


void setup()
{

   Serial.begin(9600);
   Serial.println("Starting StepperTest");
   digitalWrite(ledPin, LOW);

   delay(2000);

   pinMode(directionPin, OUTPUT);
   pinMode(stepPin, OUTPUT);
   pinMode(ledPin, OUTPUT);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
}

void loop()
{
   digitalWrite(directionPin, HIGH);
   for (int n = 0; n < numberOfSteps; n++)
   {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
      digitalWrite(stepPin, LOW);

      delay(millisbetweenSteps);

      digitalWrite(ledPin, !digitalRead(ledPin));
   }

   delay(1000);

   digitalWrite(directionPin, LOW);
   for (int n = 0; n < numberOfSteps; n++)
   {
      digitalWrite(stepPin, HIGH);
      // delayMicroseconds(pulseWidthMicros); // probably not needed
      digitalWrite(stepPin, LOW);

      delay(millisbetweenSteps);

      digitalWrite(ledPin, !digitalRead(ledPin));
   }

   delay(1000);
}

try using Z-axis with digitalwrite, pinout is
#define Z_STEP_PIN 46
#define Z_DIR_PIN 48
#define Z_ENABLE_PIN 62
#define Z_CS_PIN 40

if that do not work are you sure it is a ramps 1.4?
I teach children to build themself 3D printers with ramps and know much about that board. if you use a old ramps (1.0) then the pinout is:

// Steppers
//
#define X_STEP_PIN 26
#define X_DIR_PIN 28
#define X_ENABLE_PIN 24

#define Y_STEP_PIN 38
#define Y_DIR_PIN 40
#define Y_ENABLE_PIN 36

#define Z_STEP_PIN 44
#define Z_DIR_PIN 46
#define Z_ENABLE_PIN 42

#define E0_STEP_PIN 32
#define E0_DIR_PIN 34
#define E0_ENABLE_PIN 30

//

otherwise if you use 1.3 or 1.4 it must be

// Steppers
//
#define X_STEP_PIN 54
#define X_DIR_PIN 55
#define X_ENABLE_PIN 38
#define X_CS_PIN 53

#define Y_STEP_PIN 60
#define Y_DIR_PIN 61
#define Y_ENABLE_PIN 56
#define Y_CS_PIN 49

#define Z_STEP_PIN 46
#define Z_DIR_PIN 48
#define Z_ENABLE_PIN 62
#define Z_CS_PIN 40

#define E0_STEP_PIN 26
#define E0_DIR_PIN 28
#define E0_ENABLE_PIN 24
#define E0_CS_PIN 42

#define E1_STEP_PIN 36
#define E1_DIR_PIN 34
#define E1_ENABLE_PIN 30
#define E1_CS_PIN 44

//

Ramps 1.4 pin mapping (steppers) Arduino IDE pin names

Xstep = A0, Xdir = A1, Xenable = 38
Ystep = A6, Ydir = A7, Yenable = A2
Zstep = 46, Zdir = 49, Zenable = A8

Extruders
E0step = 26, E0dir = 28, E0enable = 24
E1step = 36, E1dir = 34, E1ensble = 30

this is what i was explaining in my post but perhaps you did not know that analog Mega pins can be written as numbers.
A0 = 54
A1 = 55
A2 = 56 and so on

Analog Uno pins can be written as:
14 = A0
15 = A1
16 = A2 and so on

Yes, I know that. In my code above, I referenced the pins with the Ax notation for the analog pins.

Thanks to all of you. I´m going to try everything and I´ll let you know.

BTW what is enable pin for ?

Each driver has an enable pin. A LOW on that pin and the driver's output stage is enabled and the stepper is powered whether it is moving or not. Put HIGH on the enable pin and the driver output stage is turned off. That will save power, but the un-powered stepper can be moved by external forces and the driver will ignore any step commands. The stepper is not locked.

thanks

I edited code. I made it shorter for my needs ,however i want to make that motor rotate in one side and now it makes one step, changes rotation and run to another side . And I don´t know why

This is the code :

// testing a stepper motor with a Pololu DRV8825 driver board or equivalent
// on an Mega the onboard led will flash with each step
// this version uses delay() to manage timing

const byte directionPin = A7;
const byte stepPin = A6;
const byte enablePin = A2;
int numberOfSteps = 200;
const byte ledPin = 13;
int pulseWidthMicros = 20;  // microseconds
int millisbetweenSteps = 10; // milliseconds - or try 1000 for slower steps


void setup()
{

   /*Serial.begin(9600);
   Serial.println("Starting StepperTest");
   digitalWrite(ledPin, LOW);
*/
   delay(2000);

   pinMode(directionPin, OUTPUT);
   pinMode(stepPin, OUTPUT);
   //pinMode(ledPin, OUTPUT);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
}


void loop()
{
   digitalWrite(directionPin, HIGH);
   for (int n = 0; n < numberOfSteps; n++)
   {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
      digitalWrite(stepPin, LOW);

      delay(millisbetweenSteps);
   }
}

With that code, the motor continuously turns in one direction. It will never stop. The code that I posted went back and forth. What do you want to do?

Did you read the tutorial that I linked?

i´ve read it twice and my ramps doesn´t want to cowork with me

I repeat.

With the code that you posted in post # 16, what does the motor do?

I want to run motor clockwise forever (right now)