Friday, November 1, 2019

Code Explanation to control Nema 17 with Arduino

Complete code with working video control Nema 17 with Arduino is given at the end of this tutorial, here we are explaining the complete program to understand the working of the project.



First of all, add the stepper motor library to your Arduino IDE. You can download the stepper motor library from here.


After that define the no of steps for the NEMA 17.  As we calculated, the no. of steps per revolution for NEMA 17 is 200.
#include <Stepper.h>
#define STEPS 200

After that, specify the pins to which driver module is connected and define the motor interface type as Type1 because the motor is connected through the driver module.
Stepper stepper(STEPS, 2, 3);
#define motorInterfaceType 1

Next set the speed for stepper motor using stepper.setSpeed function. Maximum motor speed for NEMA 17 is 4688 RPM but if we run it faster than 1000 RPM torque falls of quickly.
void setup() {
    stepper.setSpeed(1000);

Now in the main loop, we will read the potentiometer value from A0 pin. In this loop, there are two functions one is potVal, and the other is Pval. If the current value, i.e., potVal is higher than the previous value, i.e., Pval than it will move ten steps in the clockwise direction and if the current value is less than previous value than it will move ten steps in the counter-clockwise direction.
potVal = map(analogRead(A0),0,1024,0,500);
  if (potVal>Pval)
      stepper.step(10);
  if (potVal<Pval)
      stepper.step(-10);

Pval = potVal;

Now connect the Arduino with your laptop and upload the code into your Arduino UNO board using Arduino IDE, select the Board and port no and then click on the upload button.
Now you can control the direction of Nema17 stepper motor using the potentiometer. The complete working of the project is shown in the video below. If you have any doubts regarding this project, post them in the comment section below.   
Code
#include <Stepper.h>
#define STEPS 200
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver
Stepper stepper(STEPS, 2, 3); // Pin 2 connected to DIRECTION & Pin 3 connected to STEP Pin of Driver
#define motorInterfaceType 1

int Pval = 0;
int potVal = 0;
void setup() {
  // Set the maximum speed in steps per second:
  stepper.setSpeed(1000);
}
void loop() {

  potVal = map(analogRead(A0),0,1024,0,500);
  if (potVal>Pval)
      stepper.step(10);
  if (potVal<Pval)
      stepper.step(-10);
Pval = potVal;

Other stepper you may interest:geared stepper motor   linear stepper motor

No comments:

Post a Comment

Motion Controller and Driver Selection Tips You Should Know

Motion Controller The first thing that should be checked when selecting a motion controller is compatibility with other system components ...