Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
Aim: DC Motor Interfacing with Arduino
Controlling a DC Motor
We can only have full control over a DC motor if we can control its speed and spinning direction. This is
possible by combining these two techniques.
• PWM – to control speed
• H-Bridge – to control the spinning direction
PWM – to control speed
The speed of a DC motor can be controlled by changing its input voltage. A widely used technique to
accomplish this is Pulse Width Modulation (PWM).
PWM is a technique in which the average value of the input voltage is adjusted by sending a series of ON-OFF
pulses. This average voltage is proportional to the width of the pulses, which is referred to as the Duty Cycle.
The higher the duty cycle, the higher the average voltage applied to the DC motor, resulting in an increase in
motor speed. The shorter the duty cycle, the lower the average voltage applied to the DC motor, resulting in a
decrease in motor speed.
The image below shows PWM technique with various duty cycles and average voltages.
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
H-Bridge – to control the spinning direction
The spinning direction of a DC motor can be controlled by changing the polarity of its input voltage. A widely
used technique to accomplish this is to use an H-bridge.
An H-bridge circuit is made up of four switches arranged in a H shape, with the motor in the center.
Closing two specific switches at the same time reverses the polarity of the voltage applied to the motor. This
causes a change in the spinning direction of the motor.
The following animation shows the working of the H-bridge circuit.
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
L298N Motor Driver Chip
At the center of the module is a big, black chip with a chunky heat sink – the L298N.
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
The L298N chip contains two standard H-bridges capable of driving a pair of DC motors, making it ideal for
building a two-wheeled robotic platform.
The L298N motor driver has a supply range of 5V to 35V and is capable of 2A continuous current per channel,
so it works very well with most of our DC motors.
Technical Specifications
Here are the specifications:
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
L298N Motor Driver Module Pinout
The L298N module has 11 pins that allow it to communicate with the outside world. The pinout is as follows:
Direction Control Pins
The direction control pins allow you to control whether the motor rotates forward or backward. These pins
actually control the switches of the H-Bridge circuit within the L298N chip.
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
The module has two direction control pins. The IN1 and IN2 pins control the spinning direction of motor A;
While IN3 and IN4 control the spinning direction of motor B.
The spinning direction of the motor can be controlled by applying logic HIGH (5V) or logic LOW (Ground) to these
inputs. The chart below shows various combinations and their outcomes.
Speed Control Pins
The speed control pins ENA and ENB are used to turn on/off the motors and control their speed.
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
Pulling these pins HIGH will cause the motors to spin, while pulling them LOW will stop them. However, with
Pulse Width Modulation (PWM), the speed of the motors can be controlled.
The module usually comes with a jumper on these pins. When this jumper is in place, the motor spins at full
speed. If you want to control the speed of the motors programmatically, remove the jumpers and connect them
to the Arduino’s PWM-enabled pins.
Wiring an L298N Motor Driver Module to an Arduino
Now connect the L298N module’s Input and Enable pins (ENA, IN1, IN2, IN3, IN4 and ENB) to the six Arduino
digital output pins (9, 8, 7, 5, 4 and 3). Note that both Arduino output pins 9 and 3 are PWM-enabled.
Finally, wire one motor to terminal A (OUT1 and OUT2) and the other to terminal B (OUT3 and OUT4). You can
swap out your motor’s connections. There is technically no right or wrong way.
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
Arduino 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() {
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
// 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
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
void loop() {
directionControl();
delay(1000);
speedControl();
delay(1000);
}
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
// This function lets you control spinning direction of motors
void directionControl() {
// Set motors to maximum speed
// For PWM maximum possible values are 0 to 255
analogWrite(enA, 255);
analogWrite(enB, 255);
// Turn on motor A & B
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(2000);
// Now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// Turn off motors
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
// This function lets you control speed of the motors
void speedControl() {
// Turn on motors
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Accelerate from zero to maximum speed
for (int i = 0; i < 256; i++) {
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
// Decelerate from maximum speed to zero
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
for (int i = 255; i >= 0; --i) {
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
// Now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Post Lab Exercise:
1. To control a DC motor's movement in left, right, forward, and backward directions using Arduino
Name: Prem Joshi
FSSI Post Lab Exercise 13
Circuit Diagram
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
Code
/*PostLab13: To control a DC motor's movement
in left, right, forward, and backward directions using Arduino*/
// 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()
{
pinMode(enA, OUTPUT);// Set all the motor control pins to outputs
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
digitalWrite(in1, LOW);// Turn off motors - Initial state
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
directionControl();//Function is used to control the motor spin directions
//Only direction control function used for PostLab
delay(1000);
}
void directionControl()
{
analogWrite(enA, 255);// Set motors to run at maximum speed (PWM range = 0 to 255 Max)
analogWrite(enB, 255);
//FORWARD
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(2000);
//MOTORS OFF
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
digitalWrite(in4, LOW);
delay(1000);
//BACKWARD
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
//MOTORS OFF
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(1000);
//RIGHT
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
//MOTORS OFF
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(1000);
//LEFT
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication
Technology
Subject: Foundation skills in sensor
Aim: DC Motor Interfacing with Arduino
interfacing (01CT1103)
Experiment No: 13 Date: 12 Oct’24 Enrollment No: 92400133090
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(2000);
//MOTORS OFF
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(1000);
}