Half Drive Stepper Motor Control using Arduino

 Stepper motor direction and speed control in half drive mode with Arduino is illustrated in this Arduino electronics tutorial. The stepper motor used in Nema 17. L298N is used as stepper motor controller. The direction is controlled using a switch. The speed is controlled using RPM inputs and creating delay between stepper motor sequence input. Video demonstration, interfacing circuit drawing of stepper motor, Arduino, L298N, diodes and switches and Arduino code for Stepper motor without library are provided.

For full drive stepper motor control see Arduino Stepper Motor Control using L298N.

The following shows picture of interfacing Nema 17 stepper motor, Arduino Uno, L298N and diodes on breadboard.

circuit drawing of stepper motor, Arduino, L298N, diodes and switches

 The schematic drawing of interfacing Nema 17 stepper motor, Arduino, L298N, diodes and switch is shown below.

schematic drawing of interfacing stepper motor, Arduino, L298N, diodes

In the above schematic drawing, we have used the pin 10 of Arduino to enable the two full H bridge inside the L298N. The Arduino pins 9,8,7 and 6 are connected to in1, in2, in3 and in4 of L298N motor driver respectively. The outputs out1 and out2 of the L298N motor driver are connected to the phase A(red wire) and A'(blue wire) of the Nema 17 stepper motor. The out3 and out4 outputs are connected to the phase B(green wire) and phase B'(black wire) of the Nema 17 stepper motor. 8 protection diodes 1N4004 for digital circuits have been used. A switch is connected to the pin 2. When the switch is closed and grounded the stepper motor is rotated in clockwise direction and when open the motor is rotated in counter clockwise direction. The stepper motor direction control is achieved by reversing the signal sent to the phase A and phase A' wires. This reversal of signal input is done in software which is illustrated in the stepper motor code for Arduino without library provided below.

Half drive step sequence table is provided below.

Half drive step sequence table

 Many times stepper motor stops not working when direction is changed. This can happen because of many reasons, but one of them could be just faulty stepper motor. If spare stepper motor is available, the stepper motor should be replaced to be sure that it is due to the faulty stepper motor and not due to code or program.

The speed of the Stepper motor can be controlled using delay between step sequence. The delay in turn can be made to be dependent on user RPM(Revolution Per Minute) value. The following is formula for delay per step in microseconds calculated using stepper motor RPM and steps per revolution.

\[Delay/step(\mu s)=\frac{60000}{RPM*StepsPerRevolution}\]

For example, if the RPM is 30 and steps per revolution for Nema 17 is 1.8degree then the delay is 10\(\mu s\). This value of delay is used in the Arduino Code for Stepper Motor provided below.

The following video shows simulation of Stepper motor with Arduino, L298N motor driver, switch and diodes in Proteus Electronics Design Software.

 

Arduino Code for Stepper Motor

The following Arduino code uses half stepping sequence to rotate Nema 17 stepper motor in clockwise and counter clockwise direction and with speed determined by the RPM of 30.


int en = 10;
int in1 = 9;
int in2 = 8;
int in3 = 7;
int in4 = 6;

const int sw=2;
int swstate;

int rpm = 30;
int stepsPerRev = 200;
int Th=60000/(rpm*stepsPerRev);

void halfdrive_ccw()
{
   digitalWrite(in1, HIGH);	
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH); 
  delay(Th);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
 delay(Th);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, HIGH);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
 delay(Th);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(Th);
  digitalWrite(in1, LOW);	
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
  delay(Th);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
 delay(Th);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, HIGH);
 delay(Th);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(Th);
}

void halfdrive_cw()
{
   digitalWrite(in2, HIGH);	
  digitalWrite(in1, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH); 
  delay(Th);
  digitalWrite(in2, HIGH);
  digitalWrite(in1, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
 delay(Th);
  digitalWrite(in2, HIGH);
  digitalWrite(in1, HIGH);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
 delay(Th);
  digitalWrite(in2, LOW);
  digitalWrite(in1, HIGH);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(Th);
  digitalWrite(in2, LOW);	
  digitalWrite(in1, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
  delay(Th);
  digitalWrite(in2, LOW);
  digitalWrite(in1, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
 delay(Th);
  digitalWrite(in2, LOW);
  digitalWrite(in1, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, HIGH);
 delay(Th);
  digitalWrite(in2, LOW);
  digitalWrite(in1, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(Th);
}


void setup(){
  // set all the motor control pins to outputs
  pinMode(en, OUTPUT);
  digitalWrite(en, HIGH);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  //switch
  pinMode(sw,INPUT_PULLUP);
}

void loop() {
peripheral_loop();
   swstate = digitalRead(sw);
   switch(swstate){
  case LOW:
   halfdrive_cw();
   break;
  case HIGH:
   halfdrive_ccw();
   break;
   default:
   break;
   }

   }
 

In the above code two functions halfdrive_cw() and halfdrive_ccw() are used to rotate the stepper motor in clockwise and counter clockwise direction. The two function uses the same half drive sequence of input. However for direction control the in1 and in2 are different in the clockwise and counter clockwise functions. In this way we can control the stepper motor direction of rotation using the software. The speed is controlled using the delay() between the steps. When the delay() increases the speed of stepper motor is decreased. The delay() function has input Th which stands for Time delay in microseconds. This delay is calculated using the formula for delay per step provided above. In the main loop() we have used switch statement to either rotate the stepper motor in clockwise or counter clockwise direction.

The following video demonstrates Nema 17 stepper motor direction and speed control in half drive mode with Arduino. L298N is used as stepper motor controller. 

 


Other Stepper motor tutorials are as follows:

- Stepper Motor Control with Motor Shield and Arduino

- Stepper Motor control using ATmega32

- Stepper Motor Control using ATmega328p

- How to control Stepper Motor using ATmega32 and L293D IC

1 Comments

Previous Post Next Post