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);
}
}
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.
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:
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
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.
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);
}
}