ElectroPeak
Published

The Beginner’s Guide To Control Motors by Arduino and L293D

In this article, you will learn how to control DC, Stepper, and servo motors by Arduino and L293D.

BeginnerProtip1 hour95,892
The Beginner’s Guide To Control Motors by Arduino and L293D

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
USB-A to B Cable
USB-A to B Cable
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
Mini Breadboard
×1
Breadboard Jumper Wires
×1
30V 3A Adjustable Power Supply
×1
ElectroPeak DC Gear Motor
×1
BYGH403 1.65A Stepper Motor
×1
TowerPro MG995 55G Metal Gear Servo
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Sample Code to Drive DC Motors

C/C++
// Basic sketch for trying out the L293D by www.Electropeak.com
// Running single DC motor by L293D
// 20/08/2018
 
 
#define MOTOR_EN_1_2  10
#define MOTOR_IN1     9
#define MOTOR_IN2     8
 
#define slow 64
#define normal 128
#define fast 255
 
int Speed; 
 
void Forward_Rev(void){
  analogWrite(MOTOR_EN_1_2, Speed);
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, LOW);
}
 
void Backward_Rev(void){
  analogWrite(MOTOR_EN_1_2, Speed);
  digitalWrite(MOTOR_IN1, LOW);
  digitalWrite(MOTOR_IN2, HIGH);
}
 
void Forward_ramp_up(void){
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, LOW);
  for (int i=0; i<255; i++) { analogWrite(MOTOR_EN_1_2, i); delay(10); } } void Forward_ramp_down(void){ digitalWrite(MOTOR_IN1, HIGH); digitalWrite(MOTOR_IN2, LOW); for (int i=255; i>=0; i--) {
    analogWrite(MOTOR_EN_1_2, i);
    delay(10);
  }
}
 
void Backward_ramp_up(void){
  digitalWrite(MOTOR_IN1, LOW);
  digitalWrite(MOTOR_IN2, HIGH);
  for (int i=0; i<255; i++) { analogWrite(MOTOR_EN_1_2, i); delay(10); } } void Backward_ramp_down(void){ digitalWrite(MOTOR_IN1, LOW); digitalWrite(MOTOR_IN2, HIGH); for (int i=255; i>=0; i--) {
    analogWrite(MOTOR_EN_1_2, i);
    delay(10);
  }
}
 
void Brake(void){
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, HIGH);
}
 
void setup() {
  
  Serial.begin(9600);
  Serial.println("L293D DC motor test");
 
  pinMode(MOTOR_EN_1_2, OUTPUT);
  pinMode(MOTOR_IN1, OUTPUT);
  pinMode(MOTOR_IN2, OUTPUT);
}
 
void loop() {
 
  Speed=normal; // Normal Speed
 
  Forward_Rev(); 
  delay(1000);
  Brake();
  delay(500);
  Backward_Rev(); 
  delay(1000);
  Brake();
  delay(500);
  Forward_ramp_up();
  Forward_ramp_down();
  Backward_ramp_up();
  Backward_ramp_down();
  
}

Sample Code to Drive 4-Wire Stepper Motors

C/C++
// Basic sketch for trying out the L293D by www.Electropeak.com
// Running 4-wire Stepper motor by L293D
// 20/08/2018
 
#define MOTOR_EN_1_2  10
#define MOTOR_EN_3_4  11
#define MOTOR_IN1     9
#define MOTOR_IN2     8
#define MOTOR_IN3     12
#define MOTOR_IN4     13
 
#define forward  0
#define backward 1
 
#define slow   10
#define normal 5
#define fast   2
 
#define single_step   0
#define power_step    1
#define half_step     2
 
int speeed; 
int direcshen;
int stepping;
 
void stepp(void){
  
  digitalWrite(MOTOR_EN_1_2, HIGH);
  digitalWrite(MOTOR_EN_3_4, HIGH);
  
if (direcshen==forward){
  if (stepping==single_step){
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, HIGH);
    delay(speeed);  
  }
  if (stepping==half_step){
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);  
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN4, HIGH);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, HIGH);
    delay(speeed);
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, HIGH);
    delay(speeed);     
  }
  if (stepping==power_step){
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN4, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN1, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN4, HIGH);
    delay(speeed);
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN4, HIGH);
    delay(speeed);        
  }  
 }
 if (direcshen==backward){
  if (stepping==single_step){
    digitalWrite(MOTOR_IN4, HIGH);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, HIGH);
    delay(speeed);  
  }
  if (stepping==half_step){
    digitalWrite(MOTOR_IN4, HIGH);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, HIGH);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);  
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN1, HIGH);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, HIGH);
    delay(speeed);
    digitalWrite(MOTOR_IN4, HIGH);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, HIGH);
    delay(speeed);     
  }
  if (stepping==power_step){
    digitalWrite(MOTOR_IN4, HIGH);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN1, LOW);
    delay(speeed);
    digitalWrite(MOTOR_IN4, LOW);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, HIGH);
    digitalWrite(MOTOR_IN1, HIGH);
    delay(speeed);
    digitalWrite(MOTOR_IN4, HIGH);
    digitalWrite(MOTOR_IN3, LOW);
    digitalWrite(MOTOR_IN2, LOW);
    digitalWrite(MOTOR_IN1, HIGH);
    delay(speeed);        
  }  
 }
}
 
void setup() {
  
  Serial.begin(9600);
  Serial.println("L293D Stepper motor test");
 
  pinMode(MOTOR_EN_1_2, OUTPUT);
  pinMode(MOTOR_EN_3_4, OUTPUT);
  pinMode(MOTOR_IN1, OUTPUT);
  pinMode(MOTOR_IN2, OUTPUT);
  pinMode(MOTOR_IN3, OUTPUT);
  pinMode(MOTOR_IN4, OUTPUT);
  
}
 
void loop() {
 
  direcshen=forward;     // choose forward or backward
  speeed=fast;           // choose slow, normal or fast
  stepping=half_step;    // choose single, power or half step
  for (int i=0;i<=60;i++)
  {stepp();}
 
  direcshen=backward;     // choose forward or backward
  speeed=normal;           // choose slow, normal or fast
  stepping=power_step;    // choose single, power or half step
  for (int i=0;i<=50;i++)
  {stepp();}
 
  direcshen=backward;     // choose forward or backward
  speeed=slow;;           // choose slow, normal or fast
  stepping=single_step;    // choose single, power or half step
  for (int i=0;i<=30;i++)
  {stepp();}
  
}

Sample Code to Drive Servo Motors

Plain text
#include 
 
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position
 
void setup() {
  digitalWrite(10,HIGH
  );
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
 
void loop() {
  for (pos = 0; pos <= 180; pos += 3) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(5); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 3) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  }
}

Credits

ElectroPeak

ElectroPeak

57 projects • 731 followers
At ElectroPeak we want to teach you to enjoy electronics more. We offer Top-notch guides and worry-free shopping experience.
Thanks to Saeed Olfat.

Comments