Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ECEL 12 Experiment 3:

Name: Pearl Joyce A. Subere Section: BSECE-4A

I. Introduction and Basic Theory


DC Motor
A DC motor is an electro-mechanical device that converts direct current into mechanical
energy by means of rotation of a shaft. It works on the principle of Lorentz force by which the
current carrying conductor in a magnetic field experiences a force hence the conductor moves
on the direction of force called Lorentz force.

Generally, a DC motor consists of either an electromagnet or a permanent magnet and a


wounded coil known as the armature. Direct current is applied to the armature by means of a
carbon brush or by means of electromagnetic induction. Because of electromagnetic induction,
armature moves on the direction of the force. DC motors are widely used in industrial
automation, toys, and robotics applications. The speed of the DC motor can be controlled either
by controlling current to the armature or by using a variable power supply.
Speed Control of DC Motor Using PWM
DC motor speed control is perhaps the most common manipulation used in DC controllers.
This speed can be controlled in four different ways: flux variation, armature voltage variation, a
change in the supply voltage, and pulse width modulation (PWM). PWM is the technique
commonly used for achieving speed control in a DC motor. It delivers energy through a series of
pulses rather than a continuous signal. By altering the pulse width, the DC motor controller is
able to regulate the energy flow to keep it consistent.
As the name suggests, PWM is a type of modulation in which the width of the pulse is
changed without changing the frequency (i.e., the repetition rate) of the signal. The amount of
time the signal is in the HIGH state is called the duty cycle. As we will see, this quantity is
fundamental in using PWM for DC motor control.

Using PWM causes the average DC value of the signal to change when passed through a low
pass filter. If such a signal is fed to a DC motor, we can change the speed of the motor by
changing the duty cycle of the PWM signal. The change in pulse width is created by increasing
the on-time (HIGH value) of the pulse while reducing the off-time (LOW value) by the same
amount so that the frequency of the signal is constant. Increasing the on-time increases the
average DC voltage value of the signal, and vice versa. The following figure shows the variation
in the average DC value with duty cycle.

Average DC value of a PWM signal for various duty cycle values.


A simple relation to calculate the average DC value is –
𝑉𝑎𝑣𝑒 = 𝑉𝑟𝑒𝑓 × 𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒

where, V(REF) is the value of logic high. This allows analog control of a DC motor via digital
signal, making it possible to use microcontrollers to drive DC motors. A microcontroller can
change the pulse-width dynamically, providing an instant or steady change in the corresponding
DC motor speed. We can see how output voltage changes dynamically with duty cycle:

DC motor speed vs. duty cycle

II. Objectives
1. To create a program using Arduino Platform to control the DC motor using the PWM pin
of the Arduino Uno Board
2. To simulate the program using the UnoArduSim or any similar simulation software
3. To implement the program by loading it into the Arduino board
4. To determine the relationship between the pulse width and the motor speed
III. Equipment/Tools/Materials/Components Needed
1. Arduino UNO with USB Cable
2. Arduino IDE
3. DC Motor with L298N Driver
4. Potentiometer
5. PicSimLab
6. Virtual Serial Port
7. HC-SR04 Ultrasonic Sensor

IV. Diagram
L298N Motor Driver Module Pin Diagram

This module uses two techniques for the control speed and rotation direction of the DC motors. These
are H-Bridge – For controlling rotation direction and PWM – For controlling the speed.

Pin Pin Name Description

Power Supply Pins

1 VCC VCC pin is used to supply power to the motor. Its input voltage is between 5 to 35V.

2 GND GND is a ground pin. It needs to be connected to the power supply ground(negative).
+5V pin supplies power for the switching logic circuitry inside the L298N IC. If the 5V-
EN jumper is in place, this pin acts as output and can be used to power up a
microcontroller or other circuitry (sensor). If the 5V-EN jumper is removed, you need to
3 +5V connect it to the 5V power supply of the microcontroller.

Control Pins

1 IN1 These pins are input pins of Motor A. These are used to control the rotating direction of
Motor A. When one of them is HIGH and the other is LOW, Motor A will start rotating in
2 IN2 a particular direction. If both the inputs are either HIGH or LOW the Motor A will stop.

3 IN3
These pins are input pins of Motor B. These are used to control the rotating direction of
Motor A. When one of them is HIGH and the other is LOW, Motor A will start rotating in
4 IN4 a particular direction. If both the inputs are either HIGH or LOW the Motor A will stop.

Speed Control Pins


ENA pin is used to control the speed of Motor A. If a jumper is present on this pin, so the
pin connected to +5 V and the motor will be enabled, then the Motor A rotates maximum
speed.
if we remove the jumper, we need to connect this pin to a PWM input of the
microcontroller. In that way, we can control the speed of Motor A. If we connect this pin
1 ENA to Ground the Motor A will be disabled.
ENB pin is used to control the speed of Motor B. If a jumper is present on this pin, so the
pin connected to +5 V and the motor will be enabled, then the Motor B rotates maximum
speed.
if we remove the jumper, we need to connect this pin to a PWM input of the
microcontroller. In that way, we can control the speed of Motor B. If we connect this pin
2 ENB to Ground the Motor B will be disabled.

Output Pins

1 OUT1 & OUT2 This terminal block will provide the output for Motor A.

2 OUT3 & OUT4 This terminal block will provide the output for Motor B.

V. Procedure
In this laboratory activity, I will be using an L298N motor library because I will use an
L298N motor driver for my DC Motor.

DC Motor Speed Control Program using Arduino

#include "L298N_MotorDriver.h"

// In this dual driver board, I will use motor B.


// Pin 3 is connected to the ENB pin on the driver.
// Pin 2 and pin 4 can be used to change the direction of the motor.
// To make use of clockwise direction, I will make use of pin 4 connected to IN4.
// Set the pins for the motor.
L298N_MotorDriver motor(3,2,4);

// Define the pin on which the potentiometer is connected.


int potPin = A0;

void setup() {
// Turn on the motor.
motor.enable();
Serial.begin(9600);
}

void loop() {
// Read the value from the potentiometer.
int potReading = analogRead(potPin);
Serial.print("Potentiometer:"); Serial.print(potReading);

// Scale down the potentiometer reading from 0 - 1023 into 0 - 255.


byte motorSpeed = map(potReading, 0, 1023, 0, 255);
Serial.print(" Motor Speed:"); Serial.println(motorSpeed);

// Sets the speed for the motor from 0 - 255.


motor.setSpeed(motorSpeed);
}

Serial Monitor:
Serial Plotter:

Simulation using PICSimLab


Potentiometer Reading vs. Motor Speed Video Link:

https://drive.google.com/file/d/1SxukP8hxkLSgedY6BAIV0HI6wIFaX4om/view?usp=sharing
Pulse Width vs. Motor Speed Video Link:

https://drive.google.com/file/d/1uulaaqWlnYWgGf9aoiZXcEIKlrjWnlFF/view?usp=sharing
Actual Demonstration Video Link:
https://drive.google.com/file/d/1gCPf6GXhyq_8njKi_8bMVQCuwMzVh90J/view?usp=sharing
It can be observed from the videos
listed above that for the potentiometer
reading, it is proportional to the motor speed
only that the range of the potentiometer
reading which is 0-1023 is scaled down to the
range of the motor speed which is 0-255
using the map() function by the Arduino.
For the pulse width, it can also be
observed from the virtual oscilloscope that
the longer the pulse width, the higher is the
motor speed. In contrast, it can be viewed
that the shorter the pulse width, the lower
the motor speed.
It can be viewed in the third video
wherein I performed the circuit in actual by
creating connections and by uploading the program I created in the Arduino IDE to the Arduino
UNO board. As I turn the potentiometer, the speed of the rotation of the DC motor varies.

VI. Results and Discussions


In the procedure part, I have shown the result of the serial monitor and serial plotter
from Arduino IDE after I loaded the program I created to control the speed of the DC Motor.
There, I have used potentiometer to vary the speed of the DC Motor whose enable pin is
connected to the pin 3 on the Arduino which supports PWM function. I have also added there a
simulation and a view from the oscilloscope using the PICSimLab. It can be seen that from the
serial monitor, a specific reading from the potentiometer (0-1023) is equivalent to a specific
speed of the motor (0-255). It can be viewed from the serial plotter that the potentiometer
reading, and the speed of the motor are proportional. If we are to observe the oscilloscope
showing the data from the PWM that is being fed to the DC motor, we can observe that we
have various pulses. The duration of these pulses are directly proportional to the speed of the
motor. The longer the duration, the faster is the rotation of the DC motor. And, the shorter the
duration, the slower is the rotation of the DC motor.
VII. Observations
Based on the experiment conducted, it can be observed that it is very easy to control
the DC motor using PWM. You only need to familiarize the pins of the Arduino, and also the
pins of the DC motor driver. After that, it is very easy to create the proper connections, and to
create the program using the Arduino IDE according to your desire if you want to control the
speed or at the same time, the direction. Also, I have observed that when the DC motor has
only turned on and must rotate slowly, it is very hard for the DC motor to start rotating,
therefore, the response can still be further improved. However, if the motor must rotate fast, it
is very easy for the motor to start rotating and then to vary the speed of the motor.

VIII. Conclusions
I can conclude that PWM can effectively be used to control the speed of the DC motor. I
can also conclude that it is better if the system is closed loop because we have feedback of the
actual speed of the motor rather than only assigning them in the program. This way, we can
validate that what is reflected in the program is being correctly implemented by the DC motor. I
can also conclude that it is better if we use PID controller for instance to make the response of
our DC motor more desired and more stable.

IX. Recommendations
It is recommended to make use of PICSimLab as a simulation software for DC motors
because the program from the Arduino IDE can be easily used. Another advantage of using
PICSimLab is that we can integrate libraries in the Arduino IDE and it will also be applied in the
PICSimLab.
It is also recommended to create a closed-loop system such as by using sensors for the
speed of the rotation of the motor in order to create feedback and a PID controller to make the
system much more stable, and self-correcting.
And finally, it is recommended to make use of libraries especially when using various
devices to create the program. In my case, I made use of the library for the L298N DC Motor
Driver and for my design application, I made use of an HCSR04 Ultrasonic sensor. Every library
has its own built-in functions which we can easily incorporate in our program.

X. Design Applications
For the design application, I will design an anti-collision propeller using a DC motor wherein the
speed of the propeller decreases when a sensor senses an obstruction nearby. The range is
perhaps the most important factor when it comes to a (distance) measurement. Ultrasonic
sensors reach up to 8,000 mm (or 8 meters) and thus can be used within most small and
medium distances. The laser sensors start at a maximum measuring range of 10 cm and are
available up to even 3,000 meters (or 3 km). Distance lasers can be used for small, medium and
(very) large distances. For this application, we can use laser sensors for example. For the sake of
this experiment, we can make use of an ultrasonic sensor instead.
Note: I am not an expert in this field. The purpose of this activity is only to control the speed of
the motor driving the propeller.
Let us just assume the following conditions in the program.

• The speed of the motor will be 10 when the distance is between 0 to 25 cm.
• The speed of the motor will be 100 when the distance is between 25 to 50 cm.
• And finally, the speed of the motor will be 255 when the distance is over 50 cm.
Program

#include "L298N_MotorDriver.h" // I will use a library for L298N DC Motor Driver.


// In this dual driver board, I will use motor B.
L298N_MotorDriver motor(3,2,5); // Set the pins for the motor (ENB, IN3, IN4)

#include <HCSR04.h> // I will use a library for my ultrasonic sensor.


int triggerPin = 9; // Trigger Pin of Ultrasonic Sensor
int echoPin = 10; // Echo Pin of Ultrasonic Sensor
UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin);
int speed1 = 10; int speed2 = 100; int speed3 = 255;

void setup() {
motor.enable(); // Turn on the motor.
Serial.begin(9600);
}

void loop() {
double distance = distanceSensor.measureDistanceCm(); // Read and store the distance
from the Ultrasonic Sensor.
Serial.print("Distance: "); Serial.print(distance); Serial.print("cm"); // Display on the serial
monitor.
if (distance > 0.00 && distance <= 25.00) { // Do the following if distance is between 0 to 25
cm.
motor.setSpeed(speed1); // Set the speed of the motor.
Serial.print(" Speed: "); Serial.println(speed1); // Display on the serial monitor.
}
if (distance > 25.00 && distance <= 50.00) { // Do the following if distance is between 25 to
50 cm.
motor.setSpeed(speed2); // Set the speed of the motor.
Serial.print(" Speed: "); Serial.println(speed2); // Display on the serial monitor.
}
if (distance >= 50.00); { // Do the following if distance greater than 50 cm.
motor.setSpeed(speed3); // Set the speed of the motor.
Serial.print(" Speed: "); Serial.println(speed3); // Display on the serial monitor.
}
}
Actual Demonstration Video link:

https://drive.google.com/file/d/13PkuXT1LTvun0fTgIB40uAg1KpVoI6uX/view?usp=sharing
It can be observed from the video that as my hand gets nearer to the ultrasonic sensor,
the speed of the motor decreases. On the other hand, as my hand get farther from the
ultrasonic sensor, the speed of the motor increases.

References
Using PWM for DC Motor Control – Upverter Blog

Speed Control of DC Motor Using PWM - openlabpro.com

DC Motor Control using PWM with ADC (engineersgarage.com)

DC Motor Speed Control using Arduino and Potentiometer (circuitdigest.com)

You might also like