Stepper motor on Arduino Mega digital pins

Hello, I would like you to confirm if I can use digital pins from the Arduino Mega to control Stepper Motors.

I read several forums and they say yes, but they don't explain how to do it. I am a newbie to Arduino.

These are the articles:

This page also explains it simply (in Arduino UNO): https://www.arduino.cc/en/Tutorial/LibraryExamples/StepperSpeedControl
But, the ones on that last page only use a single digital pin which is 8.

I have 12 stepper motors, 3Nm. Arduino Mega. Driver DM542. 4.2 A, 48V.

So I want to use digital pins, because PWM is not enough.

Greetings.

raym3d:
So I want to use digital pins, because PWM is not enough.

PWM is not relevant for controlling stepper motors and the PWM pins are digital pins.

Just connect the Step pin of your driver to one of the Arduino I/O pins and connect the Direction pin of the driver to another Arduino I/O pin. Also connect the GNDs from the driver to the Arduino GND. When you cause a HIGH - LOW pulse on the step pin the motor will move one step. It's direction will be determined by whether the Direction pin is HIGH or LOW.

Try this Simple Stepper Code

...R
Stepper Motor Basics

Thank you for your answer, I already read the publications you made in:

But, something was not clear to me, how do I control the speed? That is, I already know that I can control EN, DIR, PUL with the digital pins, but, I did not understand the speed, I suppose that if I say that X number of steps in X time it would be a way to control the speed, or with some function Like the acceleration one, but, I would like you to tell me, since you have more experience.

Greetings.

If you look at my simple stepper code you will see a variable named millisBetweenSteps. That is the value which controls the speed. For example if you want 40 steps per second then you need 1 step every 25 milliseconds.

If you need acceleration then it may make sense to use the AccelStepper library or you could try this simple acceleration code

...R

Thank you very much for your answer. Here you showed me an example: https://forum.arduino.cc/index.php?topic=506797.msg3455713#msg3455713

But can you send me others examples?

My stepper motors are about to arrive and I want to study various codes in depth before they arrive.

Greetings.

raym3d:
But can you send me others examples?

You already have links to all of my stepper motor examples.

...R

Hi.

My stepper motors have just arrived. I tried the code you put in: https://forum.arduino.cc/index.php?topic=506797.msg3455713#msg3455713

They work for me when I put them on PWM pins like 9 and 8. But, when I put them on digital pins like 50 and 52 they don't work. You can help? You told me that it does work, and I had also read elsewhere that it does work, but I don't know why it doesn't work for me.

Note: I have Arduino Mega, a Nema 23 3Nm Stepper Motor. Everything is well connected because it went with 9 and 8.

False alarm. EVERYTHING WORKS WELL (I had it wrongly connected to the pins). But, thank you for the help that I know I was going to receive from you.

But, I have another question, maybe a little basic:

I have 12 stepper motors / drivers. Nothing happens if I put the 5V output from Arduino to those 12 drivers? Remember that the 5V to the driver is to send signals.

I haven't measured the amperage it requires yet, but maybe you can show me the way.

5V from the Power header? That is a power supply output, it is not a controllable signal. It can only supply a few hundred mA of current.

raym3d:
I have 12 stepper motors / drivers. Nothing happens if I put the 5V output from Arduino to those 12 drivers? Remember that the 5V to the driver is to send signals.

How much current does the logic power supply of your drivers require? With 12 of those large DM542 drivers you may need a separate 5v power supply for them.

Have you tried with only (say) 4 drivers connected?

...R

raym3d:
At the bottom of page 4 it says that it draws a maximum of 16 mA.
16*12 = 192 mA

Possibly that's too much for the 5v supply on the Mega and on the PC

You did not answer my question about trying 4 drivers.

...R

I think the Raspberry Pi has a 5V to 1A supply. I'm still not trying 4 until I'm sure what a driver consumes and what the power supply supplies.

Do you think 5V can be connected to Raspberry?
I'm asking because I tried to connect 5V from a charger and it didn't work (just the positive wire, leaving the black wire free). It worked when I connected it to the Arduino.

raym3d:
Do you think 5V can be connected to Raspberry?

AFAIK a RaspberryPi is a 3.3v device

...R

Pi is powered from 5.1V, 3A supply, has it's own onboard regulator to make 3.3V for internal use. IO on the 20 pin jumper is 3.3V.
USB interfaces are 5V.

So do I need an external 5V power supply? A cellular power supply, for example, or what do you recommend I do?

I suggest you get one of the cheap mains powered USB chargers - ideally one that can supply 5v at 2 amps.

...R

Just to double check, I must use a 2A, 5V charger, the kind with the USB cable. I cut the USD wire and connect only the positive line, right?

raym3d:
Just to double check, I must use a 2A, 5V charger, the kind with the USB cable.

"must" is not the right word. I suggested a USB charger as a cheap solution. There are other options.

The type of charger I'm thinking of has a USB socket so, yes, you could use a USB cable and cut the other end off.

...R

I also have questions about the code you shared earlier: https://forum.arduino.cc/index.php?topic=506797.msg3455713#msg3455713

Remember that I use this driver to control the stepper motor: https://kitaez-cnc.com/f/dm542.pdf

Page 10 of that PDF shows the Microsteps selector that the driver brings. For example, 2 microstep = 400 steps one revolution. If in your code I write int numSteps = 1000; it makes a single turn, if I put 800 it makes two turns. I mean, that part works perfectly.

This are my questions:

  1. If I place:
    unsigned long fastMicrosBetweenSteps = 1500; As your code shows, it takes 1.5 seconds to go around, but how does that work on a technical level?

  2. What does unsigned long slowMicrosBetweenSteps = 6000; // microseconds? The previous one is to place the speed in giving all the microsteps that I indicate, but what about this one? What does?

  3. I don't know what it's for either: curMicros, prevStepMicros, stepIntervalMicros, stepAdjustmentMicros, numAccelSteps, stepsToGo, byte direction = 1;

  4. I will control the motors from the serial monitor, so that it does not work automatically I just have to send signals with digitalWrite (stepPin, HIGH); digitalWrite (stepPin, LOW); ", right?

I apologize for so many questions, the code works and I can work on that, but I would have a calm mind if I know what each thing is for.

Thank you very much for all your help.

It sounds as if you are very unfamiliar with programming - and if so this may be a little bit too complicated.

The idea is that the motor makes one step whenever the time in stepIntervalMicros has elapsed.

And in order to provide acceleration the value in stepIntervalMicros is gradually changed from slowMicrosBetweenSteps to fastMicrosBetweenSteps

The variable stepsToGo keeps track of how many more steps are required to complete the move.

The variable stepAdjustmentMicros holds the value by which stepIntervalMicros is changed on its way from slowMicrosBetweenSteps to fastMicrosBetweenSteps

When you are not sure how a program works its a good idea to pretend your brain is the computer and work through each line of code writing down the values for each calculation.

…R