In this Arduino electronics tutorial, Arduino is used to rotate Nema 17 Stepper motor in clockwise and counter clockwise. To drive the Nema 17 stepper motor L298N motor driver is used as stepper motor controller. Arduino code for stepper motor control is provided, circuit schematic drawing of interfacing Arduino, L298N, Nema 17 stepper motor and diodes with switch is provided. Video demonstration is provided at the end.
Here we have used full drive mode of stepper motor, for half drive mode see Half Drive Stepper Motor Control using Arduino.
Note that for low torque and light load, Nema 17 can work with L298N but for higher torque and higher load other L298N is not recommended. Pololu A4988 (capable of driving up to 1.5A stepper motors) or DRV8825 (capable of driving up to 2A stepper motors) are better alternative to L298N as Nema 17 stepper motor driver. The Arduino Stepper Motor Tutorial illustrates with program code how to use A4988 stepper motor driver as stepper motor controller to control Nema 17 stepper motor.
The following pictures shows interfacing of Arduino, Nema 17 and L298N stepper motor driver, diodes and switch.
The following shows the circuit schematic diagram of interfacing Arduino, Nema 17 stepper motor, L298N, diodes.
The Arduino sends stepping sequence to the L298N for step direction control and speed control. In the above Arduino L298N stepper motor driver schematic, the Arduino pin 10 is connected to the ENA and ENB pins of the L298N. The Arduino pins 9 and 8 are connected to the IN1 and IN2 pins of L298N. The Arduino pins 7 and 6 are connected to IN3 and IN4 pins of L298N. The SENSEA and SENSEB are connected to ground. The OUT1 and OUT2 of L298N are connected to one phase A of the Nema stepper motor. The OUT3 and OUT4 of L298N are connected to second phase B of the Nema stepper motor. Diodes are used to protect the motor driver from damage due to sudden back emf from the stepper motor.
Also in the circuit schematic drawing, a switch is connected to the pin 2 of Arduino. When the switch is open, the stepper motor rotates in clockwise direction and when closed the stepper motor rotates in counter clockwise direction.
In this example, the stepper motor is rotated clockwise and anti-clockwise direction. The following is shows the sequence of digital input signal that is sent by Arduino.
The A, A',B and B' phases refers to the Arduino digital pin 9, 8, 7 and 6. A high signal sent via pin 9 for example is 1 in the above table.
Nema 17 Stepper motor code using Arduino
The following is the Arduino l298n code to rotate Nema 17 clockwise and counter clockwise direction using L298N stepper driver.
int en = 10;
int in1 = 9;
int in2 = 8;
int in3 = 7;
int in4 = 6;
const int sw=2;
int swstate;
const int t= 10;
void cw()
{
analogWrite(en, 200);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(t);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(t);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(t);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(t);
}
void ccw()
{
analogWrite(en, 200);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(t);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(t);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(t);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(t);
}
void setup(){
//set motor control pins to outputs
pinMode(en, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
//switch
pinMode(sw,INPUT_PULLUP);
}
void loop() {
peripheral_loop();
swstate = digitalRead(sw);
if(swstate == HIGH){
cw();
}
else{
ccw();
}
}
In the above arduino code for stepper motor control, we have created two functions cw() and ccw() that rotates the stepper motor in clockwise and counter clockwise direction. In these function the sequence of steps for clockwise and counter clockwise direction provided in the above table are realized using digitalWrite() function. In the loop() function we use if statement to monitor the switch connected to pin 2. The switch is used to control the direction of stepper motor rotation direction. When switch is open the stepper motor is rotated in clockwise direction and when closed the stepper motor is rotated in anti-clockwise direction.
The following video demonstrates Stepper motor control using Arduino and L298N stepper motor driver.