0% found this document useful (0 votes)
13 views11 pages

Lab 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Lab 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Department of Electrical & Electronic Engineering

Rajshahi University of Engineering & Technology

LAB Report

Course Code : Electrical Shop Practice

Course Title : EEE 3100

Experiment No. : 04

Experiment Name : Control and Analyze the Operation of DC Motors Using Arduino and
L293D Motor Driver IC

Submitted By: Submitted To:


Asif Zaman Rizve
Name : Al-Momin Hosen

Roll : 2101102 Lecturer

Section : B Department of Electrical & Electronic


Engineering
Rajshahi University of Engineering &
Technology

Date of Experiment : 22 April, 2025

Date of Submission : 13 May, 2025


Experiment No.: 04
Experiment Name: Control and Analyze the Operation of DC Motors Using Arduino and
L293D Motor Driver IC
Objective:
1. To understand how to control the speed and direction of a DC motor using an Arduino.
2. To learn the interfacing of L293D motor driver IC with Arduino and DC motor.
3. To implement PWM (Pulse Width Modulation) for speed control of the DC motor.
4. To analyze the motor’s behavior under different input conditions using Arduino code.

Theory:
DC motors are widely used in robotics, automation, and embedded systems due to their
simplicity and controllability. They convert electrical energy into mechanical motion, specifically
rotational motion. The speed of a DC motor is directly proportional to the voltage applied, while
the direction of rotation depends on the polarity. However, microcontrollers like Arduino cannot
directly drive a DC motor because they cannot supply the necessary current. To overcome this
limitation, motor driver ICs such as the L293D are used, which act as an interface between low-
power control signals and high-power motors.

L293D Motor Driver IC:

The L293D is a dual H-Bridge motor driver IC that enables control of two DC motors in both
forward and reverse directions. It can operate motors up to 36V and supply a maximum current
of 600 mA per channel. Each motor is controlled through two input pins (IN1 and IN2) that
determine its direction, and an enable pin (EN) that controls its speed using PWM signals. The
L293D also contains internal diodes to protect the circuit from voltage spikes caused by motor
back-EMF, making it safe for use with microcontrollers.
Figure 4.01: L293D Motor Driver IC with PIN Diagram.

The L293D is a 16-pin IC that allows us to control the direction and speed of two DC motors. It
works by taking logic inputs from the Arduino and delivering high current outputs to motors.Pin
Configuration (for one motor):

 IN1 and IN2 → Direction Control


 EN1 → Speed control via PWM
 OUT1 and OUT2 → Connected to the motor terminals
 VCC1 → Logic voltage (5V)
 VCC2 → Motor voltage (up to 36V)
 GND → Ground

H-Bridge and Direction Control :

An H-Bridge circuit is used to control the direction of current through the motor, allowing
bidirectional rotation. The L293D internally contains two H-Bridge circuits. By applying
different combinations of HIGH and LOW logic to the input pins of the L293D, the polarity
across the motor terminals can be controlled, thus changing the rotation direction. For example,
setting IN1 to HIGH and IN2 to LOW causes the motor to rotate in one direction, while
reversing these inputs changes the direction of rotation.

Speed Control Using PWM :

Speed control of the DC motor is achieved using a technique called Pulse Width Modulation
(PWM). In PWM, the voltage applied to the motor is rapidly switched on and off at a certain
frequency. The ratio of the ON time to the total time (called the duty cycle) determines the
effective voltage and hence the speed of the motor. The Arduino has built-in PWM support on
several digital pins, which can be used with the analogWrite() function to vary the duty cycle
from 0 (off) to 255 (full speed), enabling fine-grained speed control.

Interfacing Arduino with L293D :

To control a DC motor, the Arduino is connected to the L293D motor driver IC. The logic input
pins of the L293D receive control signals from the Arduino, while the enable pin receives the
PWM signal for speed control. The motor is powered by an external power supply connected to
the L293D’s motor power pin (VCC2), while the logic power (VCC1) is supplied from the
Arduino’s 5V pin. A common ground connection is essential to ensure proper operation. This
setup allows the Arduino to control the motor's speed and direction efficiently and safely.

Working Principle:

1. Direction Control: Two input pins (IN1 and IN2) control the direction.

 IN1 = HIGH, IN2 = LOW → Clockwise


 IN1 = LOW, IN2 = HIGH → Counter-Clockwise
2. Speed Control: Speed is controlled using PWM (Pulse Width Modulation) on the EN
(Enable) pin. The duty cycle of the PWM signal determines the speed of the motor.

Arduino Control:

 Arduino sends HIGH/LOW logic to L293D to determine direction.


 PWM signal from Arduino (using analogWrite) is used to control speed.
 The Arduino sketch (program) reads input (can be sensors, buttons, or potentiometer) and
outputs control signals accordingly.

Required Apparatus:
Table 4.1: Table for Required Apparatus
Sl. Components Model Specifications Quantity
no
3V~12V(DC), Maximum torque: 800gf cm
1 DC motor HR0242 min (3V) No-load speed: 1:48 (3V time) 2
The load current: 70mA (250mA MAX)
(3V)
Chip: ATmega328P (8-bit AVR) Clock
Speed: 16 MHz, Flash Memory: 32 KB,
SRAM: 2 KB , EEPROM: 1 KB, Digital
I/O Pins: 14 (6 provide PWM output: 3, 5,
2 Arduino UNO R3
6, 9, 10, 11), Analog Input Pins: 6 (A0–A5, 1
10-bit resolution) DC Current per I/O Pin:
20 mA DC Current for 3.3V Pin: 50 mA
3 Motor Driver IC L293D Supply voltage= 36 V max Logic supply 1
voltage= 36 V max Input Voltage= 7 V max
Enable Voltage= 7V max Total Quiescent
Logic Supply current= 60 mA max Total
Quiescent Supply current= 60 mA max

4 Wheel 2
5 Chassis Plywood 1
6 DC source 3.7 V 2
7 Battery casing 2×1 1
8 Hot gun and Glue 1
9 Software Arduino IDE
10 Jumper wires Male to Male wire 15
11 Bread board 1
Circuit Diagram:

Figure 4.02: Circuit Diagram to control DC motors with L293D Motor Driver IC & Arduino

Experimental Setup:
Figure 4.03: Experimental setup for this experiment

Code:
int enA = 9; digitalWrite(in1, LOW);
int in1 = 8; digitalWrite(in2, HIGH);
int in2 = 7; digitalWrite(in3, LOW);
int enB = 3; digitalWrite(in4, HIGH);
int in3 = 2; delay(2000);
int in4 = 4;
void setup() { digitalWrite(in1, LOW);
pinMode(enA,OUTPUT); digitalWrite(in2, LOW);
pinMode(enB,OUTPUT); digitalWrite(in3, LOW);
pinMode(in1,OUTPUT); digitalWrite(in4, LOW);
pinMode(in2,OUTPUT); }
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT); void speedControl() {
digitalWrite(in1,LOW); digitalWrite(in1, LOW);
digitalWrite(in2,LOW); digitalWrite(in2, HIGH);
digitalWrite(in3,LOW); digitalWrite(in3, LOW);
digitalWrite(in4, LOW); digitalWrite(in4, HIGH);
}
Void loop() { for (int i = 0; i < 255; i++) {
directionControl(); analogWrite(enA, i);
delay(1000); analogWrite(enB, i);
speedControl(); delay(20);
delay(1000); }
}
Void directionControl() {
analogWrite(enA, 255); for (int i = 255; i >= 0; --i) {
analogWrite(enB, 255); analogWrite(enA, i);
digitalWrite(in1, HIGH); analogWrite(enB, i);
digitalWrite(in2, LOW); delay(20);
digitalWrite(in3, HIGH); }
digitalWrite(in4, LOW);
delay(2000); digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

Experimental Procedure:

1. The wheels were attached to the shafts of the two DC gear motors.
2. The motors were securely mounted on the chassis using glue.
3. The L293D motor driver IC was inserted into a breadboard.
4. The terminals of Motor A were connected to pins 3 (OUT1) and 6 (OUT2) of the L293D,
while Motor B was connected to pins 11 (OUT3) and 14 (OUT4).
5. The control pins of the L293D were connected to the Arduino: IN1 (pin 2) to pin 8, IN2
(pin 7) to pin 7, IN3 (pin 10) to pin 2, and IN4 (pin 15) to pin 4.
6. The enable pins ENA (pin 1) and ENB (pin 9) were connected to Arduino pins 9 and 3
respectively for PWM control.
7. Vcc1 (pin 16) of the L293D was connected to the 5V pin of the Arduino, while Vcc2 (pin
8) was connected to an external 6V–12V motor power supply.
8. The ground pins (4, 5, 12, 13) of the L293D were connected to a common ground shared
with the Arduino and the motor power source.
9. The Arduino Uno was connected to a computer via USB cable.

10. The Arduino IDE was opened and the motor control code was uploaded to the board.
11. In the setup() function, all motor control pins were set as OUTPUT using the pinMode()
function and the motors were initialized to an OFF state.
12. In the loop() function, a direction control function was called to rotate the motors forward
for 2 seconds, then in reverse for 2 seconds, and then stop them.
13. A speed control function was then called to gradually increase the motor speed to
maximum and decrease it back to zero while maintaining reverse direction.
14. The setup was powered on and the operation of the motors was observed.
15. The motors were seen rotating in both forward and reverse directions, and their speed
varied as programmed.

Hands-on Learning Projects for Mastery of DC Motor Control with Arduino and L293D

Task No.: 01
Task Name: Designing Timed Sequential Control of Dual Motors in a Repetitive Cycle
Objective:
 To implement a time-based motor control system where Motor A runs for 5s , when B motor
is OFF, then Motor B runs for 5s and now motor a is OFF and then both motors run together
for 10s . The process will continue.
Code:
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;

void setup() {
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Turn off motors - Initial state


stopMotors();
}

void loop() {
// 1. Only Motor A runs for 5 seconds
runMotorA();
delay(5000); // Motor A runs 5s
stopMotors();
delay(2000); // 2s stop

// 2. Only Motor B runs for 5 seconds


runMotorB();
delay(5000); // Motor B runs 5s
stopMotors();
delay(2000); // 2s stop

// 3. Both Motors run together for 10 seconds


runMotorAB();
delay(10000); // Both Motors run 10s
stopMotors();
delay(2000); // 2s stop
}

// Function to stop both motors


void stopMotors() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

analogWrite(enA, 0);
analogWrite(enB, 0);
}

// Function to run only Motor A


void runMotorA() {
analogWrite(enA, 255); // Full speed A
analogWrite(enB, 0); // B motor speed 0

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

// Function to run only Motor B


void runMotorB() {
analogWrite(enA, 0); // A motor speed 0
analogWrite(enB, 255); // Full speed B

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

// Function to run both Motor A and B


void runMotorAB() {
analogWrite(enA, 255); // Full speed A
analogWrite(enB, 255); // Full speed B

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);

digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
Task No.: 02
Task Name: Motor Control Using Button press.
Objective:
 To design a control system where Motor A will Run for 5s and it will stop after 5s .But when
button are pressed only then motor B will run. Motor B will run continuously untill again the
button are pressed.
Code:
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;

// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;

// Button connection
int buttonPin = 2; // Push button connected to pin 2

bool motorB_Running = false; // Initially Motor B is OFF

void setup() {
// Set motor control pins as outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

pinMode(buttonPin, INPUT_PULLUP); // Button normally HIGH, pressed = LOW

stopMotors();
}

void loop() {
runMotorA();
delay(5000);

stopMotors();
waitForButtonPress();
runMotorB();

while (true) {
if (digitalRead(buttonPin) == LOW) { // Button pressed again
stopMotors();
delay(500);
break;
}
}
}

void stopMotors() {
analogWrite(enA, 0);
analogWrite(enB, 0);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

// Function to run only Motor A


void runMotorA() {
analogWrite(enA, 255); // Full speed A
analogWrite(enB, 0); // Motor B off
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

// Function to run only Motor B


void runMotorB() {
analogWrite(enA, 0); // Motor A off
analogWrite(enB, 255); // Full speed B
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

void waitForButtonPress() {
while (digitalRead(buttonPin) == HIGH) {
// Button not pressed yet, keep waiting
delay(10); // Small delay to avoid busy waiting
}
delay(200); // Debounce delay after press
}

Discussion :
In this lab experiment we learnt how to control two DC motors using the L293D motor driver IC
with an Arduino.

First, we placed the L293D IC on the breadboard and connected the motors to the driver IC with
jumper wires. Then, the driver IC was connected to the Arduino board. Both the motors and the
driver IC received their power from the Arduino's 5V output. After completing all the wiring, we
uploaded the code to the Arduino. When the code was uploaded, the motors started to run
properly.

In Task 1, we modified the code a bit so that Motor A ran for 5 seconds and then stoped, then
Motor B ran for another 5 seconds before stopping. After that, both motors ran together for 10
seconds and stopped again. This sequence repeated continuously.

In Task 2, Motor A was programmed to run for 5 seconds and stop, but Motor B didn’t start by
itself. Instead, it only ran when the push button was pressed, and kept running until the button
was pressed again to stop it. At first, the button wasn't working due to a wrong connection, but
after checking the circuit, it worked fine.

You might also like