Controlling Stepper and DC Motor with Arduino

Hello there, it's my first post and I hope I post in the right place :stuck_out_tongue:

I'm new to Arduino and I'm trying to build a project where with the Arduino MEGA I'll control 2 Motors, a Stepper Motor NEMA 17 and a DC Motor. The concept is, that by choosing one of 4 options on my LCD screen (A,B,C or D) the Motors with move accordingly. For example if I pick option A, there has to be this sequence: Stepper Motor moves 50 steps, waits 5 seconds, then the DC Motor moves for 3 seconds, waits 5 seconds, then the DC Motor moves the other direction, waits 5 seconds. That's one cycle, and for option A I need this to repeat for 4 times.

No matter what I've tried, it doesn't seem to work, I use L298N driver for both motors, when I try simplier codes like only the Stepper moving or only the DC Motor moving it works fine. So I guess I do something damn wrong with the code :stuck_out_tongue: I even used ChatGPT but I can't find a solution, in every code I try the motions are mixed, sometimes the DC Motor doesn't do the movements, I need some help here.

So to recap, I just need some help for option A, and I'll figure out how to make the rest. The motors have to move like that:

User clicks A from Keyboard.
Stepper Motor moves 50 steps.
Pause in the system for 5 seconds.
The DC Motor moves for 3 seconds.
Pause for 5 seconds.
The DC Motor moves for 3 seconds in the opposite direction.
Pause for 5 seconds.
Stepper Motor moves 50 steps.
Pause in the system for 5 seconds.
The DC Motor moves for 3 seconds.
Pause for 5 seconds.
The DC Motor moves for 3 seconds in the opposite direction.
Pause for 5 seconds.
In the end the Stepper Motor has to move back 100 steps so it's back to it's original place.

Thanks in advance if someone finds a way to help, I wish I posted on the right place, God bless you all guys :smiley:

In case I didn't mention something important forgive me, it's actually my first project so I'm learning!

Hi, @vangkt
Welcome to the forum.

Can you please post specs/data for your stepper?
Nema17 does not tell us any electrical parameters.
Can you please post specs/data for your DC motor?

What L298 module do you have?

Also what LCD screen.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Wow I never expected a so fast answer! Yeah, I'll tell you right now!

My Stepper Motor is ST4018M1404-B, 1.4A per phase I don't know what else I have to mention from the datasheet :frowning: I think that it needs 200 steps for full rotation, but I kinda lose it when it comes to convert degrees to steps. The problem also is that for one option I need to divide 360 in 6 steps, but with the 200 steps that the motor does I get 33,333. I'll need some help here as well :stuck_out_tongue:

My DC Motor is DC Motor Faulhaber 1524A012S R.

As a driver I use L298N Dual H-Bridge Motor Driver Module for both Motors. I also bought A4988 to use on the Stepper, but I can't make it work for some reason, probably I can't limit the current the correct way, but from what I red in the internet I can use L298N for Stepper as well, correct me if not.

There's no need for someone to bother with the screen and program, it's not right to get help on everything, but in order for me to try and see if the whole thing works from Screen to Action, I need first to fix the action (= Motors movement). In terms of "user interface", meaning LCD Screen giving all the correct messages and the user clicking from Keypad it's all good, after he picks option A, the screen displays "Running". But during "Running" the system has to actually run haha!

I look forward to read your answer! If I need to say anything more, please ask me to!

In terms of coding, do I have to post my code? I mean, I think this project code should be really simple and I just do it wrong, but if it's needed I can post all the different versions of code that I tried so we can take a look at it, if you want!

I believe I need to explain better what the project is tho.

Let me clarify the concept of the project.

Imagine there are 6 round buckets arranged in a circle. At the center of these buckets is a vertical axis, which I call the Z-axis. The Z-axis rotates to position itself over each bucket and then moves vertically, down and up, before moving to the next bucket.

The stepper motor is responsible for rotating the Z-axis, as it is connected at the bottom of the axis. For example, if the stepper motor rotates by 33.33 degrees, the axis will move precisely to align with the next bucket. With 6 buckets making a full circle, the stepper motor completes 360 degrees in increments of 33.33 degrees.

The DC motor is mounted perpendicularly to the Z-axis (at a 90-degree angle). Its role is to control a screw mechanism that moves vertically—either up or down—depending on the direction of the DC motor's rotation.

Example Sequence :

  1. Start: The stepper motor rotates the Z-axis to align with the first bucket.
  2. The DC motor moves the screw downward. (Currently, I use a timer to control this movement, but I plan to integrate sensors for more precise control.)
  3. After staying in the lowered position for 5 seconds, the DC motor moves the screw back up.
  4. The stepper motor rotates the Z-axis to the next bucket.
  5. The DC motor repeats the movement: down, wait for 5 seconds, and back up.
  6. The process continues until all buckets are serviced.
    That's the option A
    If the user picks option B, it will move arround to only 4 of the 6 buckets, but that's up to me to make, if we manage to fix the option A, It will be similar!

In summary, the project involves a central axis (Z-axis) that rotates around itself using the stepper motor and performs vertical up-and-down motions using the DC motor.

Please don't. There's nothing useful in that. Post the the current code.

1 Like

Your homework assignment consists of Option A, then you copy/paste B, C and D. How is that "building a project?" Use the lessons you learned in school, read some internet information, try some examples, write and test your own program, then post it here with any questions.

Does this make sense?

Position bucket #1 at 0 steps,     0°, +33 <--+
                 2    33        59.4°, +33    |
                 3    66       118.8°, +34    |
                 4    100        180°, +33    |
                 5    133      239.4°, +33    |
                 6    166      298.8°, +34 >--+


Hi, @vangkt
Thanks for the info;
Your motor;

As suggested, just do part A.
Can you please post a circuit diagram of how you have the A4988 wired and the code you have tried.
The A4988 is your best option for the stepper as it is a current controlled device.
How are you powering the stepper.

Stepper params.


Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Do you mean 33.33 steps?

33.33 steps * 6 "cups" = 200 (199.9) steps * 1.8 degrees per step = 360 (359.9) degrees

This is like a pipette dipper for sampling, or an ammunition auto loader.

I have a stepper motor that rotates 1.8 degrees per step. That is 360 degrees per 1 rotation divided by 1.8 degrees per step equals 200 steps per 1 rotation. I told my Arduino to put out 200 step pulses and the motor rotated one time and stopped. What is so hard to understand?

I do not see a real difficulty of understanding. The homework assignment is due and ChatGPT got in the way.

Are you also going to integrate thrust bearing? Stepper motors do not have axial thrust bearings!

Stepper motors take time to move, so the stepper needs to hit a limit switch to indicate when the stepper has reached 50 steps, and again at 100 steps, before the pause happens, or the pause could be over before the stepper arrives.

How's your homework going? Questions?

Enter 'A' or 'a': A
Stepper motor 50 steps fowrard. Position: 50
Limit switch... tripped. Pause 5 seconds: 012345
DC motor fowrard 3 seconds: 0123
Pause 5 seconds: 012345
DC motor reverse 3 seconds: 0123
Pause 5 seconds: 012345
Stepper motor 50 steps fowrard. Position: 100
Limit switch... tripped. Pause 5 seconds: 012345
DC motor fowrard 3 seconds: 0123
Pause 5 seconds: 012345
DC motor reverse 3 seconds: 0123
Pause 5 seconds: 012345
Stepper motor 50 steps reverse. Position: 50
Stepper motor 50 steps reverse. Position: 0  I'M HOME!
sketch.ino for wokwi.com
// https://forum.arduino.cc/t/controlling-stepper-and-dc-motor-with-arduino/1341679/

#define stepsPerRevolution 200

int home;
byte buttonPin = 3, stepPin = 6, dirPin = 5, dcFwdPin = 14, dcRevPin = 15;
char key;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  digitalWrite(dirPin, HIGH);
  Serial.print("Enter 'A' or 'a': ");
}

void loop() {
  getinput();
  if ( key == 'A' || key == 'a') { // User clicks A from Keyboard.
    Serial.println(key);
    fiftysteps(1); // Stepper Motor moves 50 steps.
    checkbutton(); // needs limit switch
    Serial.print("Pause"); pause(5); // Pause in the system for 5 seconds.
    dcmotor(1); // The DC Motor moves for 3 seconds.
    Serial.print("Pause"); pause(5); // Pause in the system for 5 seconds.
    dcmotor(0); // DC Motor moves for 3 seconds in the opposite direction.
    Serial.print("Pause"); pause(5); // Pause in the system for 5 seconds.
    fiftysteps(1); // Stepper Motor moves 50 steps.
    checkbutton(); // needs limit switch
    Serial.print("Pause"); pause(5); // Pause in the system for 5 seconds.
    dcmotor(1); // The DC Motor moves for 3 seconds.
    Serial.print("Pause"); pause(5); // Pause in the system for 5 seconds.
    dcmotor(0); // The DC Motor moves for 3 seconds in the opposite direction.
    Serial.print("Pause"); pause(5); // Pause in the system for 5 seconds.
    fiftysteps(0); // Stepper Motor moves back 100 steps so it's back to it's original place.
    fiftysteps(0); // Stepper Motor moves back 100 steps so it's back to it's original place.
  }
}

char getinput() {
  if (Serial.available()) {
    key = Serial.read();
    delay(10); // let buffer clear
  }
}

void fiftysteps(int dir) {
  Serial.print("Stepper motor 50 steps ");
  Serial.print(dir ? "fowrard." : "reverse.");
  digitalWrite(dirPin, dir);
  for (int i = 0; i < 50; i++) {
    home += (dir ? +1 : -1);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
  Serial.print(" Position: ");
  Serial.print(home);
  if (!home)
    Serial.println("  I'M HOME!");
  else
    Serial.println();
}

void checkbutton() {
  Serial.print("Limit switch... ");
  while (digitalRead(buttonPin));
  Serial.print("tripped. ");
}

void pause(int seconds) {
  Serial.print(" ");
  Serial.print(seconds);
  Serial.print(" seconds: ");
  for (int i = 0; i < seconds + 1; i++) {
    Serial.print(i);
    delay(1000);
  }
  Serial.println();
}

void dcmotor(int dir) {
  Serial.print("DC motor ");
  Serial.print(dir ? "fowrard" : "reverse");
  if (dir) {
    digitalWrite(dcFwdPin, HIGH);
    digitalWrite(dcRevPin, LOW);
  } else {
    digitalWrite(dcFwdPin, LOW);
    digitalWrite(dcRevPin, HIGH);
  }
  pause(3);
  digitalWrite(dcFwdPin, LOW);
  digitalWrite(dcRevPin, LOW);
}
diagram.json for wokwi.com
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-arduino-nano",
      "id": "nano",
      "top": -83.4,
      "left": 39.9,
      "rotate": 90,
      "attrs": {}
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -41.8,
      "left": 172.8,
      "attrs": { "color": "yellow" }
    },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper1",
      "top": -201.61,
      "left": 253.02,
      "attrs": { "size": "8" }
    },
    { "type": "wokwi-a4988", "id": "drv1", "top": -120, "left": 177.6, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -152.84, "left": 230.4, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": -9.6, "left": 239.4, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -186,
      "left": 61.4,
      "attrs": { "color": "green" }
    },
    { "type": "wokwi-led", "id": "led2", "top": -186, "left": 42.2, "attrs": { "color": "red" } },
    {
      "type": "wokwi-text",
      "id": "txt1",
      "top": -182.4,
      "left": 19.2,
      "attrs": { "text": "REV" }
    },
    { "type": "wokwi-text", "id": "txt2", "top": -182.4, "left": 96, "attrs": { "text": "FWD" } },
    { "type": "wokwi-text", "id": "txt3", "top": 0, "left": 192, "attrs": { "text": "LIMIT" } }
  ],
  "connections": [
    [ "nano:GND.2", "btn1:2.l", "black", [ "v0" ] ],
    [ "nano:3", "btn1:1.l", "green", [ "h0" ] ],
    [ "drv1:DIR", "nano:5", "green", [ "h0" ] ],
    [ "drv1:STEP", "nano:6", "green", [ "h0" ] ],
    [ "drv1:SLEEP", "drv1:RESET", "green", [ "h-19.2", "v-9.6" ] ],
    [ "vcc1:VCC", "drv1:VMOT", "red", [ "v0" ] ],
    [ "vcc1:VCC", "drv1:VDD", "red", [ "v0" ] ],
    [ "gnd1:GND", "drv1:GND.1", "black", [ "v0" ] ],
    [ "gnd1:GND", "drv1:GND.2", "black", [ "v0" ] ],
    [ "drv1:2B", "stepper1:A+", "green", [ "h0" ] ],
    [ "drv1:2A", "stepper1:A-", "green", [ "h0" ] ],
    [ "drv1:1A", "stepper1:B-", "green", [ "h0" ] ],
    [ "drv1:1B", "stepper1:B+", "green", [ "h0" ] ],
    [ "nano:A0", "led1:A", "green", [ "h0" ] ],
    [ "nano:GND.1", "led1:C", "black", [ "h0" ] ],
    [ "nano:A1", "led2:A", "green", [ "h0" ] ],
    [ "nano:GND.1", "led2:C", "black", [ "h0" ] ]
  ],
  "dependencies": {}
}

The OP has 6 "stations", 200 is not evenly divisible by 6, a 200 step motor cannot stop at 1/3 or 2/3 of a step. Microstepping could get closer (with diminished holding torque) but never exactly 60°. With 1/16 microstepping, 533 out of 3200 microsteps = 59.9625°.

And this problem is the very reason people design stepper motor systems with belts and pulleys to make the angular movement to be what they need.
I don't understand why people want to always connect directly to a stepper motor shaft and then try to force some other movements from it.

1 Like

How many model railroaders have built turntables, laid their tracks to it and then discovered their stepper won't stop in alignment with the track?

1 Like

I like the suggestion by JCA34F in post #9 - using 33 step x 4 and 34 step x 2 spacing. Or maybe not using 2 steps out of the 200 as not rotating the full circle or adding 2 extra steps to get out of trouble if complete rotation is a must.

1 Like

The ones that align the tracks to the stepper motors.