Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
engineer2you
Published

DVD Stepper Motor Arduino

DVD stepper motor is bipolar type. Using H-bridge L298N to be as drive.

IntermediateFull instructions provided2 hours7,708
DVD Stepper Motor Arduino

Things used in this project

Hardware components

UTSOURCE Electronic Parts
UTSOURCE Electronic Parts
×1

Story

Read more

Code

Code snippet #1

C/C++
const byte pin_A1 = 4;
const byte pin_A2 = 5;
const byte pin_ENA = 2;

const byte pin_B1 = 7;
const byte pin_B2 = 8;
const byte pin_ENB = 3;

const byte speed_motor = 10;

void setup() {
  pinMode(pin_A1,OUTPUT);
  pinMode(pin_A2,OUTPUT);
  pinMode(pin_ENA,OUTPUT);

  pinMode(pin_B1,OUTPUT);
  pinMode(pin_B2,OUTPUT);
  pinMode(pin_ENB,OUTPUT);

  // start serial port at 9600 bps:
  Serial.begin(9600);

  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

}

void loop() {
  run_forward();
}

void run_forward()
{
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,1);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,1);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,1);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,1);
  delay(speed_motor);
}

void run_backward()
{
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,1);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,1);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,1);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,1);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
}

Code snippet #2

Plain text
const byte pin_A1 = 4;
const byte pin_A2 = 5;
const byte pin_ENA = 2;

const byte pin_B1 = 7;
const byte pin_B2 = 8;
const byte pin_ENB = 3;

const byte speed_motor = 10;

void setup() {
  pinMode(pin_A1,OUTPUT);
  pinMode(pin_A2,OUTPUT);
  pinMode(pin_ENA,OUTPUT);

  pinMode(pin_B1,OUTPUT);
  pinMode(pin_B2,OUTPUT);
  pinMode(pin_ENB,OUTPUT);

  // start serial port at 9600 bps:
  Serial.begin(9600);

  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

}

void loop() {
  run_forward();
}

void run_forward()
{
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,1);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,1);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,1);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,1);
  delay(speed_motor);
}

void run_backward()
{
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,1);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,1);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,1);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,1);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
}

Code snippet #3

Plain text
const byte pin_A1 = 4;
const byte pin_A2 = 5;
const byte pin_ENA = 2;

const byte pin_B1 = 7;
const byte pin_B2 = 8;
const byte pin_ENB = 3;

const byte speed_motor = 10;

void setup() {
  pinMode(pin_A1,OUTPUT);
  pinMode(pin_A2,OUTPUT);
  pinMode(pin_ENA,OUTPUT);

  pinMode(pin_B1,OUTPUT);
  pinMode(pin_B2,OUTPUT);
  pinMode(pin_ENB,OUTPUT);

  // start serial port at 9600 bps:
  Serial.begin(9600);

  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

}

void loop() {
  run_forward();
}

void run_forward()
{
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,1);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,1);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,1);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,1);
  delay(speed_motor);
}

void run_backward()
{
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,1);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,1);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
  digitalWrite(pin_ENA,0);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,0);
  digitalWrite(pin_ENB,1);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,1);
  delay(speed_motor);
  digitalWrite(pin_ENA,1);
  digitalWrite(pin_A1,0);
  digitalWrite(pin_A2,1);
  digitalWrite(pin_ENB,0);
  digitalWrite(pin_B1,0);
  digitalWrite(pin_B2,0);
  delay(speed_motor);
}

Credits

engineer2you

engineer2you

24 projects • 93 followers
i'm mechatronics engineer who loves making funny project and sharing for you. See me at: https://www.youtube.com/c/engineer2you

Comments