Menu Close

DC Motor control using PWM Signals – Arduino

This tutorial of Robo India explains how to control the speed of DC mtor using PWM signals.

1. Introduction 

The DC motor speed in general is directly proportional to the supply voltage, so if reduce the voltage from 9 volts to 4.5 volts then our speed become half of what it originally had. But in practice, for changing the speed of a dc motor we cannot go on changing the supply voltage all the time. The speed controller PWM for a DC motor works by varying the average voltage supplied to the motor

PWM signal is essentially a high frequency square wave ( typically greater than 1KHz). The Duty Cycle of this square wave is varied in order to vary the power supplied to the load.

The input signals we given to PWM controller might be an analog or digital signal according to the design of the PWM controller. The PWM controller accepts the control signal and adjusts the duty cycle of the PWM signal according to the requirements. These diagram below shows the waveforms obtained as output at different voltage requirements.

In these waves frequency is same but the ON and OFF times are different.

 1.2 Hardware required

S.No.ItemQuantity
1Arduino UNO 1
2Motor Driver 1
3DC Motor 2
4Female to Male Jumper wire 10
56xAA Battery 1

1.3 Motor Driver L293D IC Pin Out

The Motor Driver is a module for motors that allows you to control the working speed and direction of two motors simultaneously .This Motor Driver is designed and developed based on L293D IC.

The L293D is a 16 pin IC, with eight pins, on each side, to controlling of two DC motor simultaneously. There are 4 INPUT pins, 4 OUTPUT pins and 2 ENABLE pin for each motor.

Pin 1: When Enable1/2 is HIGH, Left part of IC will work, i.e motor connected with pin 3 and pin 6 will rotate.

Pin 2: Input 1, when this pin is HIGH the current will flow though output 1.

Pin 3: Outputt 1, this pin is connected with one terminal of motor.

Pin 4/5: GND pins

Pin 6: Output 2, this pin is connected with one terminal of motor.

Pin 7: Input 2, when this pin is HIGH the current will flow though output 2.

Pin 8: VSS, this pin is used to give power supply to connected motors from 5V to 36V maximum depends on Motor connected.

Pin 9: When Enable 3/4 is HIGH, Right part of IC will work, i.e motor connected with pin 11 and pin 14 will rotate.

Pin 10: Input 4, when this pin is HIGH the current will flow though output 4.

Pin 11: Output 4, this pin is connected with one terminal of motor.

Pin 12/13: GND pins

Pin 14: Output 3, this pin is connected with one terminal of motor.

Pin 15: Input 3, when this pin is HIGH the current will flow though output 3.

Pin 16: VCC, for supply power to IC i.e 5V.

2. Connections with Arduino

1. Module 5V (VCC) – Arduino 5V.

2. Module GND – Arduino GND.

3. Module 1 – Arduino D8.

4. Module 2 – Arduino D9.

5. Module 3 – Arduino D10.

6. Module 4 – Arduino D11.

7. Module EN12 – Arduino D5.

8. Module EN34 – Arduino D6.

9. Module Motor Screw terminals – DC motors.

10. Module VSS power Screw terminal- External power source of 9V.

Make sure to remove the Jumper preset on Enable pins of module, so that we can connect PWM input to this pin and control the speed of motors. If we connect these pins to ground , then the motor will get disabled.

3. Programming:

Here is the code to run this circuit.

You may download this code (Arduino Sketch) from here.

//Tutorial by RoboIndia on Motor Control using PWM Signals
//Hardware Required: Motor Driver (By RoboIndia & Arduino)
 
                              //Motor A
const int inputPin1  = 10;    // Pin 15 of L293D IC
const int inputPin2  = 11;    // Pin 10 of L293D IC
                              //Motor B
const int inputPin3  = 9;    // Pin  7 of L293D IC
const int inputPin4  = 8;    // Pin  2 of L293D IC
int EN1 = 5;                 // Pin 1 of L293D IC
int EN2 = 6;                 // Pin 9 of L293D IC
void setup()

{
  
    pinMode(EN1, OUTPUT);   // where the motor is connected to
    pinMode(EN2, OUTPUT);   // where the motor is connected to
    pinMode(inputPin1, OUTPUT);
    pinMode(inputPin2, OUTPUT);
    pinMode(inputPin3, OUTPUT);
    pinMode(inputPin4, OUTPUT);  
    Serial.begin(9600);
    Serial.println("Enter values between 0 - 255");
}

void loop()

{
  if(Serial.available())         
    { 
      int speed = Serial.parseInt();  //Receive Value from serial monitor
      Serial.println(speed)
        analogWrite(EN1, speed);      //sets the motors speed
        analogWrite(EN2, speed);      //sets the motors speed
        digitalWrite(inputPin1, HIGH);
        digitalWrite(inputPin2, LOW);
        digitalWrite(inputPin3, HIGH);
        digitalWrite(inputPin4, LOW);  
     }
}

 4. Output

After the connection you will copy and paste this code in Arduino IDE than upload the code. Open the Serial Monitor and send the input values to Arduino. You can control the speed of the DC motor by sending different values between 0 -255.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

2 Comments

  1. krishna

    good article. but I’ve a doubt regarding the speed control of a dc motor. here we’re varying the duty cycle of a PWM signal to control the rpm. Can we do it programmatically? Or can we design a controller to control dc motor rpm??

Leave a Reply