Nema17 and a4988

Hello,

I have an Arduino Uno and A4988 for controlling a NEMA17 motor. I would need the motor to rotate in one direction for 2 meters, and after turning off and on the circuit, it should rotate back. This should work for a one-way cable car that travels back and forth. Thank you for your help.I have the connection according to this manual https://www.youtube.com/watch?v=5CmjB4WF5XA&t=344s

Welcome back to the Arduino forum. So, what do you need help with?

Straightforward, whatever "the circuit" might be.

Study some of the countless examples of stepper code on the web, and give it a try!

Don't forget to set the current limit on the stepper driver to less than 1 Ampere per winding.

Would you make me a code, please. Cause I tried and the engine didn't spin the way it was supposed to

Post the code, using code tags, a wiring diagram and links to the parts. Did you set the current limit correctly?

Please explain what this means: "the engine didn't spin the way it was supposed to".

My code, but it's not working for me
`
#include <Stepper.h>

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 2, 3);

const byte PinBut = 0;
byte butLst;

enum { Idle, CW, CCW };
int state = Idle;

// -----------------------------------------------------------------------------
void loop ()
{
// process state
switch (state) {
case Idle:
break;

case CCW:
myStepper.step (-1);
break;

case CW:
myStepper.step (1);
break;
}

// check for button press
byte but = digitalRead (PinBut);
if (butLst != but) {
butLst = but;
delay (20); // debounce
if (LOW == but) {
if (CCW < ++state) // advance state
state = Idle;
Serial.println (state);
}
}
}

// -----------------------------------------------------------------------------
void setup () {
Serial.begin (9600);

myStepper.setSpeed (200);

pinMode (PinBut, INPUT_PULLUP);
butLst = digitalRead (PinBut);
}`

For informed help, please read and follow the instructions in the "How to get the best out of this forum" post, linked at the head of every forum category.

I moved your topic to an appropriate forum category @lukyzeu.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I noticed some errors in your code and procedure against the forum rules in your post,

It is part of the forum rules that when posting codes it is done using tags.
These tags can be easily used by clicking " < code > " in the answer board toolbar.
Redo your post by correcting this mistake.

In your code, the problem I saw is that you are using pin 0 of the Arduino to connect the button.
Neither pin 0 nor pin 1 should be used as they are used by the serial to load codes and send messages.

If your assembly was the same as the video you posted the link to, your project will not work, as there is no connection between reset with the sleep pin.
Recommended connection for the A4988 module to work.

Finally, you say that your motor has "rotate in one direction for 2 meters" .
What does it mean to run 2 meters?
How many steps will it take to rotate 2 m?

See your mock code here.
When you press the button for the first time, the motor turns one way, the second time it turns the other way, and the third time it stops.

" Step - Wokwi ESP32, STM32, Arduino Simulator

Thank you for your help. Just as you sent me the schematic, I have the A4988 connected to a 12V power supply, so I need two phases and not just one, thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.