 Hello friends! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge about how can you make a simple program for DC Motor Speed Control using Arduino UNO. In my previous tutorial, DC Motor Direction Control using Arduino, I have just controlled the DC motor in both directions at constant speed using Arduino. I have also performed the DC Motor Direction Control in Matlab by sending different commands through serial port from Matlab and LabVIEW to the Arduino and then controlled the direction of rotation of DC motor. But in this tutorial I will rotate the same DC motor at variable speed in both clockwise and anti clockwise directions.

In my previous tutorial, we have seen that input pins In1 In2 of motor control driver L298 (H-Bridge) are useful to control the direction of rotation of the DC motor. In this tutorial, I have controlled its speed as well by providing different voltage levels at the enable pin of the DC motor control driver L298. It will be helpful to vary the speed of the DC motor in either clockwise or in anti clockwise direction. So, let’s get started with DC Motor Speed Control using Arduino UNO:

#### DC Motor Speed Control using Arduino UNO

In this tutorial we will learn that how to make an algorithm for DC Motor Speed Control using Arduino UNO. Speed control of any motor is always done y Pulse Width Modulation, abbreviated as PWM. PWM pulse can be generated using Arduino and L298 Enable Pin is used to get that PWM pulse and then it controls the motor speed accordingly. Before going into the further details I would like to tell you about the concept of PWM for controlling DC motor. Moreover, you can download the complete Arduino code for DC Motor Speed Control using Arduino by clicking the below button:

Arduino Source Code

##### Pulse Width Modulation (PWM)

PWM stands for Pulse Width Modulation. It basically describes the type of the digital signal. PWM technique is an excellent technique to control the analog circuits with microcontroller’s digital PWM output. In this technique we can get analog results with the digital means. Digital control is used to create square wave. This pattern can vary voltages between full on i.e. 5V and full off i.e. 0V. The duration of on time i.e. when the the signal is present is known as pulse width. PWM waves for the different duty cycles are shown in the figure below. Duty cycle is basically the proportion of the time during which a system is operated. It can be expressed as a percentage. For example motor rotates for 1 second out of 100 seconds, it duty cycle can be represented as 1/100 or as 1%. For Arduino software coding the command analogWrite(255) shows the maximum i.e. 100% duty cycle. To achieve 50% duty cycle we have to update this command to analogWrite(127). Arduino UNO’s pin no 3, 5, 6,10 and 11 are used as PWM pins. In this project we can control the speed of the DC motor by providing high and low voltages to the enable pin of the motor control driver L298. For example, if a motor rotates with the maximum speed and 100% duty cycle at 12V and we provide it with the 6V then it will rotate with the half of the initial speed having 50% duty cycle.

##### Motor Controller L298

The pins EnA and EnB of the motor controller L298 are used as the PWM pins. We can rotate the DC motor at different speed providing different high and low voltage levels to these pins of the motor control driver. If we start to reduce the maximum voltage at which the motor rotates at maximum speed, the speed of the motor also starts to reduce. In this way these enable pins are helpful to control the speed of the DC motor. ##### Algorithm design and descrition

In this section of the tutorial DC Motor Speed Control using Arduino UNO, I am going to explain you about designing as well as a detailed description of the designed algorithm. I will tell you about the entire algorithm in step by step procedure.

Note:Since you are working on the DC motor so you must also have a look at my previous tutorials, they will be helpful for you to simulate this project as well.

Open your Arduino software, copy and paste the source code given below in your software.

```#include <LiquidCrystal.h>
//Keyboard Controls:
//
// C - Clockwise
// S - Stop
// A - Anti-clockwise

// Declare L298N Controller pins
// Motor 1
int count=255;
int dir1PinA = 2;
int dir2PinA = 5;
int speedPinA = 6; // PWM control
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

void setup() {

Serial.begin(9600); // baud rate

lcd.begin(20, 4);
lcd.setCursor(5,0);
lcd.print("DC Motor");
lcd.setCursor(5,1);
lcd.print("Direction");
lcd.setCursor(5,2);
lcd.print("Control");
lcd.setCursor(2,3);
lcd.print("via Arduino UNO");

delay(3000);

lcd.clear ();

lcd.setCursor(0,2);
lcd.print("www.TheEngineering");
lcd.setCursor(4,3);
lcd.print("Projects.com");
//Define L298N Dual H-Bridge Motor Controller Pins

pinMode(dir1PinA,OUTPUT);
pinMode(dir2PinA,OUTPUT);
pinMode(speedPinA,OUTPUT);

analogWrite(speedPinA, 255);//Sets speed variable via PWM

}

void loop() {

// Initialize the Serial interface:

if (Serial.available() > 0) {
int speed; // Local variable

switch (inByte) {

case 'C': // Clockwise rotation
//analogWrite(speedPinA, 255);//Sets speed variable via PWM
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, HIGH);
Serial.println("Clockwise rotation"); // Prints out “Motor 1 Forward” on the serial monitor
Serial.println("   "); // Creates a blank line printed on the serial monitor
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("Clockwise rotation");
break;

case 'S': // No rotation
//analogWrite(speedPinA, 0); // 0 PWM (Speed)
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, LOW);
Serial.println("No rotation");
Serial.println("   ");
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("No rotation");
break;

case 'H': //Accelrating motor
count=count+20;
if (count>255)
{
count =255;
}
analogWrite(speedPinA,count);
delay(50);

//digitalWrite(dir1PinA, LOW);
//digitalWrite(dir2PinA, HIGH);
Serial.println("Motor is accelrating slowly");
Serial.println("   ");
Serial.println(count);
lcd.setCursor(0,0);
lcd.print("Motor is accelrating");
break;

case 'L': //Deaccelrating motor
count=count-20;
if (count<20)
{
count=20;
}
analogWrite(speedPinA,count);
delay(50);

//digitalWrite(dir1PinA, LOW);
//digitalWrite(dir2PinA, HIGH);
Serial.println("Motor is deaccelrating slowly");
Serial.println("   ");
Serial.println(count);
lcd.setCursor(0,0);
lcd.print("Motor Deaccelrates");
break;

case 'A': // Anti-clockwise rotation
//analogWrite(speedPinA, 255); // Maximum PWM (speed)
digitalWrite(dir1PinA, HIGH);
digitalWrite(dir2PinA, LOW);
Serial.println("Anti-clockwise rotation");
Serial.println("   ");
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("Anti-clockwise");
break;

default:
// Turn off the motor if any other key is being pressed
for (int thisPin = 2; thisPin < 11; thisPin++) {
digitalWrite(thisPin, LOW);
}
Serial.println("Wrong key is pressed");
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("Wrong key is pressed");
}
}
}```
• In the previous tutorials, DC Motor Direction Control using Arduino and DC Motor Direction Control using Matlab we have learnt that how to control the direction of the DC motor.
• We used the commands C, A and S for the clockwise rotation, anti clockwise rotation and stopping the motor respectively.
• In this tutorial, we have added two further commands and for accelerating and de-accelerating the DC motor.
• If we send the command H different times consecutively the speed of the motor will increase continuously.
• If we send the command different times consecutively, the speed of the motor will start to decrease.
• Open the serial monitor at the top right of the Arduino Software.
• And enter the commands in serial monitor periodically as explained above.
##### Actual Hardware Setup
• When we enter the command in the serial monitor of the Arduino software. Motor will start rotating in the clockwise direction and a statement Clockwise rotation will be printed on serial port.
• The same statement will be printed on the LCD as well as shown in the figure below. • When we enter the command in the serial monitor of the Arduino software. Motor will start rotating in the anti clockwise direction and a statement Anti clockwise rotation will be printed on serial port.
• The same statement will be printed on the LCD as well as shown in the figure below. • When we enter the command in the serial monitor of the Arduino software. Motor will start accelerating and a statement Motor is accelerating will be printed on serial port.
• The same statement will be printed on the LCD as well as shown in the figure below. • When we enter the command in the serial monitor of the Arduino software. Motor will start to deaccelerate and a statement Motor Deaccelerates will be printed on serial port.
• The same statement will be printed on the LCD as well as shown in the figure below. Thats all from the tutorial DC Motor Speed Control using Arduino UNO. I hope you have enjoyed this tutorial. If you face any sort of problem, you can ask me anytime without feeling any kind of hesitation. I will further explore my knowledge about Arduino projects in my later tutorials. Till then, Take care 🙂