Search Code

Controlling a DC motor using an L293D motor driver with Arduino PWM

Controlling a DC motor using an L293D motor driver with Arduino PWM

This project demonstrates how to control the speed and direction of a DC motor using an L293D motor driver chip and Arduino's Pulse Width Modulation (PWM) capabilities. This setup allows for precise motor control, making it suitable for a variety of applications.

Here are some project ideas using this setup:

  • Robotics: Control the movement of a small robot's wheels.
  • Automated devices: Create automated curtains, blinds, or a small conveyor belt.
  • Fan control: Adjust the speed of a cooling fan based on temperature readings.
  • Solar trackers: Position small solar panels to follow the sun throughout the day.

Hardware/Components

  • Arduino Uno (or compatible board)
  • L293D Motor Driver IC
  • DC Motor (rated for the L293D's voltage and current limits, up to 600mA)
  • 9V Power Supply (for the motor)
  • Connecting wires
  • Breadboard (recommended)

Wiring Guide

The L293D chip acts as an interface between the Arduino and the DC motor. It allows the Arduino to control both the motor's speed and direction. The chip requires a separate power supply for the motor because the Arduino cannot supply enough current for most motors. (in video at 04:04)

%%WIRING%%

Code Explanation

The Arduino code controls the L293D driver, which in turn controls the DC motor. The code uses PWM to vary the motor speed, and digital signals to switch direction.

Here are the key configurable parameters in the code:


#define P1A 10 // Arduino pin connected to L293D input 1A
#define P2A 11 // Arduino pin connected to L293D input 2A
#define EN12 9 // Arduino pin connected to L293D enable pin (1,2EN)

const int speedStep = 15; // Increment for speed changes
const int speedDelay = 1000; // Delay between speed steps (in milliseconds)

The L293D() function controls the motor's direction and speed. 'L' sets the motor to rotate clockwise, 'R' sets it to counter-clockwise. The spd parameter controls the speed (0-255) and en enables (1) or disables (0) the motor. (in video at 09:24)


void L293D(char dir,int spd, int en)
{
  if(dir =='L') // Clockwise rotation
  {
    // ...
    analogWrite(P1A,spd); // Set speed using PWM
    digitalWrite(P2A,LOW); 
  }else{ // Counter-clockwise rotation
    // ...
    digitalWrite(P1A,LOW);
    analogWrite(P2A,spd); // Set speed using PWM
  }
}

Live Project/Demonstration

The video demonstrates the project in action, showing the motor speed and direction changes. The oscilloscope readings visualize the PWM signals controlling the motor. (in video at 11:07)

Chapters

  • [00:00] Introduction to DC Motor Control with L293D
  • [00:30] Project Demonstration and Explanation
  • [01:12] L293D Motor Driver Overview
  • [02:08] DC Motor Specifications
  • [02:30] L293D Datasheet Explanation
  • [04:40] Wiring Explanation and Demonstration
  • [07:05] Arduino Code Explanation
  • [11:07] Project Demonstration and Oscilloscope Readings
144-Arduino source for L293D motor driver (loop)
Language: C++
/*
 * This is the Arduino code to control the speed of a motor using an L293D DC motor driver.

Watch instructions for this video: https://youtu.be/akQoGNUzhHI

 // Written for Robojax.com video 

 * Code is available at http://robojax.com/learn/arduino

 * 
 // Written by Ahmad S. for Robojax.com on 
// August 11, 2018 at 13:23 in Ajax, Ontario, Canada
// This code is AS IS without warranty. You may share it if you keep this note with the code.
*/
// DC motor control
#define P1A 10 // define pin 10 as P1A
#define P2A 11 // define pin 11 as P2A
#define EN12 9 // define pin 9 as the 1,2EN enable

const int speedStep =15;
const int speedDelay = 1000;// delay between speed increment

void setup() {
  // L293 Motor Control Code by Robojax.com 20180811
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(P1A, OUTPUT);// define pin as OUTPUT for P1A
  pinMode(P2A, OUTPUT);// define pin as OUTPUT for P2A
  pinMode(EN12, OUTPUT);// define pin as OUTPUT for 1,2EN

  Serial.println("Robojax");
  Serial.println("L293D Motor Speed Control");
  
  // L293 Motor Control Code by Robojax.com 20180811   
}

void loop() {
  // L293D Motor Control Code by Robojax.com 20180811

 for(int speed=0; speed<=255; speed +=speedStep)
 {
  L293D('L',speed, 0);// 
  delay(speedDelay);// delay between each step
 }

  delay(50);// 50 millisecond delay
  // L293D Motor Control Code by Robojax.com 20180810 
}//loop end




/*
 * L293D(char dir,int spd, int en)
 * dir is a character: 'L' for clockwise direction
 *  or 'R' for counter-clockwise direction
 *  en is an integer: 1 to rotate, 0 to stop
 *  spd is the speed value from 0 to 255
 */
void L293D(char dir,int spd, int en)
{
  if(dir =='L')
  {
    if(en ==0){
       Serial.println("CW Motor Stopped");
    }else{
       Serial.print("Rotating CW: "); 
       Serial.println(spd);//print actual speed value        
    }
    digitalWrite(EN12 ,en);// Enable 1A and 2A 
    analogWrite(P1A,spd);// send PWM with spd value to P1A
    digitalWrite(P2A,LOW);// LOW signal to P2A       
   
  }else{
    if(en ==0){
       Serial.println("CCW Motor Stopped");
    }else{
       Serial.print("Rotating CCW: "); 
       Serial.println(spd);//print actual speed value     
    }    
    digitalWrite(EN12 ,en);// Disable 1A and 2A    
    digitalWrite(P1A,LOW);// Keep this pin LOW
    analogWrite(P2A,spd);// send PWM with spd value to  P2A  
  }
}//L293D end
145-Source for a simple L293D motor controller using Arduino (CW, CCW, and STOP)
Language: C++
++
/*

 * This is the Arduino code to control the speed of a motor using an L293D DC motor driver.
 * This code will control the motor with different directions and speed.

Watch instructions for this video: https://youtu.be/akQoGNUzhHI

 // Written for Robojax.com video 

 * Code is available at http://robojax.com/learn/arduino

 * 
 // Written by Ahmad Shamshiri for Robojax.com on 
// August 11, 2018 at 13:23 in Ajax, Ontario, Canada.
// This code is AS IS without warranty. You may share it if you keep this note with the code.
*/
// DC motor control
#define P1A 10 // define pin 10 as P1A
#define P2A 11 // define pin 11 as P2A
#define EN12 9 // define pin 9 as the 1,2EN enable

const int speedStep =15;
const int speedDelay = 1000;// delay between speed increment

void setup() {
  // L293 Motor Control Code by Robojax.com 20180811
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(P1A, OUTPUT);// define pin as OUTPUT for P1A
  pinMode(P2A, OUTPUT);// define pin as OUTPUT for P2A
  pinMode(EN12, OUTPUT);// define pin as OUTPUT for 1,2EN

  Serial.println("Robojax");
  Serial.println("L293D Motor Speed Control");
  
  // L293 Motor Control Code by Robojax.com 20180811   
}

void loop() {
  // L293D Motor Control Code by Robojax.com 20180811


  L293D('L',255, 1);// maximum speed to the LEFT
  delay(3000);// wait for 3 seconds

  L293D('L',255, 0);// stop motor
  delay(2000);// wait for 2 seconds

  L293D('L',127, 1);// half speed to the LEFT
  delay(3000);// wait for 3 seconds  

  L293D('L',255, 0);// stop motor
  delay(2000);// wait for 2 seconds

  L293D('R',90, 1);// slow to the RIGHT
  delay(3000);// wait for 3 seconds  

  L293D('L',255, 0);// stop motor
  delay(2000);// wait for 2 seconds

  // L293D Motor Control Code by Robojax.com 20180810 
}//loop end




/*
 * L293D(char dir,int spd, int en)
 * dir is a character: 'L' for clockwise (CW) direction
 *  or 'R' for counter-clockwise (CCW) direction.
 *  en is an integer: 1 to rotate, 0 to stop.
 *  spd is the speed value from 0 to 255.
 */
void L293D(char dir,int spd, int en)
{
  if(dir =='L')
  {
    if(en ==0){
       Serial.println("CW Motor Stopped");
    }else{
       Serial.print("Rotating CW: "); 
       Serial.println(spd);//print actual speed value        
    }
    digitalWrite(EN12 ,en);// Enable 1A and 2A 
    analogWrite(P1A,spd);// send PWM with spd value to P1A
    digitalWrite(P2A,LOW);// LOW signal to P2A       
   
  }else{
    if(en ==0){
       Serial.println("CCW Motor Stopped");
    }else{
       Serial.print("Rotating CCW: "); 
       Serial.println(spd);//print actual speed value     
    }    
    digitalWrite(EN12 ,en);//enable 1A and 2A    
    digitalWrite(P1A,LOW);// Keep it LOW P1A
    analogWrite(P2A,spd);// send PWM with spd value to  P2A  
  }
}//L293D end

Files📁

No files available.