DC Motor Control
DC Motor Control
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.
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:
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.
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.
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.
#include "L298N_MotorDriver.h"
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);
Serial Monitor:
Serial Plotter:
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.
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
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