My little place on the web

Steppermotor controlled by Arduino

This is a short how-to on controlling a bipolar stepper motor by an Arduino and a L298N based dual H-bridge controller board.

For a long time I thought about playing around with a stepper motor. The goal for this page is not to explain what a stepper motor is, but to document for myself What I did so I can reproduce the results at a later time.

The Wiki page on stepper motors does an excellent job at documenting the motor.

The components

This first test setup has three components, an Arduino (clone), A stepper driver and a stepper motor.

The Arduino clone is just like a regular Arduino but this one has a serial port and the labeling of the ports is more like the real Atmega328 pin mapping. The controller board was a bit of a chalange because I tried to design it myself with the two chips that were designed specifically for this: the L297 STEPPER MOTOR CONTROLLERS and the L298 Dual Full Bridge Driver. This is no simple task if you need to do it on a single layer PCB. So in the end I just bought the simpler L298 driver board of e-bay so I could get started.

The steppermotor I chose also came from e-bay, it works on 12V and it is unipolar. In the end the problem with this motor is that the wire coloring is different from any data-sheet I could find. So after a lot of searching and testing I came up with the right sequence. Coil A : Blue | Coil B : Yellow | Coil C : Green | Coil D : Red

The label on the motor reads: Huisitong MOTOR 42BYGH202AA 1.8 1A 0: 20110717000KU

The demo

The code

L298N_driver_test.ino
// test script
// Drive a Motor controller L298N
 
 
//IN1 IN2 : TTL Compatible Inputs of the Bridge A
//In3 In4 :TTL Compatible Inputs of the Bridge B.
//ENA ENB:TTL Compatible Enable Input: the L state disables the
//bridge A(enable A) and/or the bridge B (enable B).
 
#define ENA  8
#define IN1  9
#define IN2 10
#define IN3 11
#define IN4 12
#define ENB 13
 
void setup() {
  // put your setup code here, to run once:
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);
 
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, HIGH);
 
}
 
void loop() {
  // put your main code here, to run repeatedly: 
  engage();
  for (int i = 0 ; i < 50; i++ ) {  
    //waveDriveOneFaseOn();
    //waveDriveTwoFaseOn();
    halfStep();
  }
  delay(10);
  disengage();
  delay(2000);
 
 
}
 
void engage() {
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
}
 
void disengage() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
}
 
void  waveDriveTwoFaseOn() {
  //State sequence and output waveforms for the two phase on sequence
  // only four steps for one turn
  // 0101
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delayMicroseconds(1000);
 
  //1001
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delayMicroseconds(1000);
 
  //1010
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  delayMicroseconds(1000);
 
  //0110
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  delayMicroseconds(1000);
}
 
void halfStep() {
 
  //101011
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(ENB, HIGH); 
  delayMicroseconds(1000);
 
  //000011
  digitalWrite(ENA, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(ENB, HIGH);
 
 
  //110011
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(ENB, HIGH); 
  delayMicroseconds(1000);
 
  //110000
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, LOW); 
  delayMicroseconds(1000);
 
  //110101
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, HIGH); 
  delayMicroseconds(1000);
 
 
  //000101
  digitalWrite(ENA, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, HIGH); 
  delayMicroseconds(1000);
 
 
  //101101
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, HIGH); 
  delayMicroseconds(1000);
 
  //101000
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, LOW);   
  delayMicroseconds(1000);
}
 
void waveDriveOneFaseOn() {
 
  //000011
  digitalWrite(ENA, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  digitalWrite(ENB, HIGH);   
  delayMicroseconds(1000);
 
  //110000
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, LOW);   
  delayMicroseconds(1000);
 
  //000101
  digitalWrite(ENA, LOW);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, HIGH);   
  delayMicroseconds(1000);
 
  //101000
  digitalWrite(ENA, HIGH);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite(ENB, LOW);   
  delayMicroseconds(1000);
}