Speed Control of DC Motor using PIC Microcontroller

Hello friends, hope you all are fine and having fun with your lives. Today's tutorial is about speed control of DC Motor using PIC Microcontroller. I haven't written this tutorial by myself. Instead this tutorial is written by one of my very good friend Salah Dahouathi. We had an interaction through his awesome Facebook group Let's Think Binary and I would suggest you guys to join it as well, because he posted many projects in his group. So, after having a look at his work, I asked him to write for our blog which he gladly accepted and here's his first post. :) I hope you guys are gonna enjoy this one and will get knowledge out of it. So let's get started with this speed control of DC Motor using PIC Microcontroller tutorial.

Many applications in industry such us robotics controls, swing machines, electronic bikes, winding machines, Spinning and Weaving machines and many others applications need a variable speed of DC motor. The best method for speed control of DC motor is the use of Pulse Width Modulation technique. This is a method to control the output voltage with the of constant frequency switching and by adjusting on duration of switching and in other words by changing duty cycle of switching.

Speed Control of DC Motor using PIC Microcontroller

  • You can download the complete Proteus Simulation along with the Programming Code in MikroC Pro for PIC Compiler for Speed Control of DC Motor by clicking the below button:
Download Proteus Simulation

  • The scheme of the project is given in the circuit below :
  • Where :
    • CONTROL STAGE : The microcontroller is PIC16F877A
    • POWER STAGE : BUCK structure : L, C, D, MOSFET
    • INTERFACE STAGE : IR2110
  • Now in the above project, I have used IR2110 using which I have controlled the speed of DC Motor.
  • You can see the PWM pin of PIC Microcontroller is coming to IR2110.
  • Mr. Salah has designed the code in MikroC Pro for PIC compiler and the code is given below:
unsigned short current_duty1, current_duty2,dt;

void main()
{
  ADCON1 |= 6;                    // all ports as digital
  CMCON  |= 7;                  // comparators off
  TRISD = 0X03;
  PORTD=0;

 PWM1_Init(5000);                     // Initialize PWM module at 5KHz
 PWM1_Start();                       // start PWM1

 while (1) {                         // endless loop

    if (RD0_bit==1 && RD1_bit == 0)           // button on RD0 pressed
      {
      Delay_ms(40);
      //current_duty1=127;                 // Set duty ratio to 50%
      current_duty1=64;                 // Set duty ratio to 25%
                                         //   duty ratio can be calculated as (Percent*255)/100
      PWM1_Set_Duty(current_duty1);
      }
    else if (RD1_bit==1 && RD0_bit == 0 )     // button on RD1 pressed
      {
      Delay_ms(40);
      //current_duty1=192;                 // Set duty ratio to 75%
      current_duty2=216;                   // Set duty ratio to 85%
                                          //   duty ratio can be calculated as (Percent*255)/100
      PWM1_Set_Duty(current_duty2);
      }
     else
      {
       Delay_ms(40);
      dt=0;
      PWM1_Set_Duty(dt);

      }
     Delay_ms(10);                      // slow down change pace a little
   }
 }
  • In the above code, you can see he has used the PWM to do the speed control the DC Motor.
  • Few of important commands used in above code are given below:
    • PWM1_Init(constant long frequency) : This function initializes the PWM module with duty ratio 0. Frequency parameter is the desired frequency in Hz. It should be a numeric constant, should not be a variable.
    • PWM1_Set_Duty(unsigned short duty_ratio) : This function is used to set the duty cycle of the PWM. The parameter duty_ratio takes values from 0 to 255, ie 0 means 0% , 127 means 50% and 255 means 100% duty cycle. The PWM1_Init() routine must be called before using this.
    • PWM1_Start() : This function starts the PWM output. PWM1_Init() must be called before calling this routine,
    • PWM1_Stop() : This function stops the PWM output. PWM1_Init() must be called before calling this routine. PWM1_Start() should be called before calling this function otherwise calling this function will not have any effect as PWM module is not running.
  • When you run your simulation then in High speed it will look something as shown in below figure:
  • As you can see in the above figure that the voltage across DC Motor is around 18V and if you are running the simulation then you will see the motor will be moving quite fast.
  • Now let's move our motor in slow speed, in order to do that you have to turn ON Low speed switch as shown in below figure:
  • Now if you check the voltage across DC Motor then you can see its 6V and the Dc Motor will now move at slow speed.
  • Here's the video for the above project which will give you better idea of How it works:

That's all for today, thanks for reading Speed Control of DC Motor using PIC Microcontroller. I hope you have enjoyed this tutorial. In the end, I again wanna thanks to my dear friend Salah Dahouathi who has spend time in designing this great simulation, programming code and tutorial. You should also like his Facebook Group where he keeps on posting such Arduino related projects, name of his Facebook group is Let's Think Binary. So, will see you guys in next tutorial, till then take care and have fun !!! :)

DC Motor Speed Control using Arduino in Proteus

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a tutorial on DC Motor Speed Control using Arduino in Proteus ISIS. In my previous post, we have seen How to design a DC Motor Direction Control Project using Arduino in Proteus ISIS and if you haven't checked it out then I would recommend you to have a look at it first. Because, in today's tutorial, I am gonna extend that tutorial and will add the DC Motor Speed Control in it. So, today, we will control both the direction as well as speed of the DC Motor. Moreover, you should also have a look at How to use Arduino PWM Pins if you are not much familiar with PWM control.

In the previous tutorial, we have seen How to control the direction of a DC Motor, which is important when you are working on some robot and you need to move that robot in both forward and reverse direction. So, in such cases you need to do the direction control of DC motor. But in most projects, along with direction, we also need to control the speed of DC motor so that we can implement some PID algorithm on the motors. So, in such cases, there comes a need for DC Motor Speed control, which we are gonna cover in today's post. So, let's get started with it.

DC Motor Speed Control using Arduino in Proteus

  • As I have explained earlier, I am gonna take it further from our previous tutorial. So, in previous tutorial, what we have done is, we have controlled the direction of DC Motor using Serial Terminal.
  • When we send commands on the Serial Terminal the motor moves in clockwise or Anti-clockwise direction.
  • So, now the above mentioned functionality will remain the same but an addition will be of speed control.
  • I have placed a LDR sensor in the simulation and depending on the value of that LDR sensor our DC motor speed will either increase or decrease.
  • So, you can download the complete simulation of DC Motor Speed Control by clicking the below button:
Download DC Motor Simulation

  • As I always recommend, design this simulation on your own so that you learn most of it.
  • So, first of all, design a circuit as shown in below figure:
  • As you can see in the above figure, its exactly the same as we designed for Direction Control of DC Motor in Proteus ISIS with a slight difference.
  • The difference is NPN transistor which is used for DC Motor speed control.
  • The base of this NPN transistor is connected with PWM pin of Arduino board.
  • So, I am generating a PWM pulse on this pin which is then applied on the base of transistor.
  • Now if I increase the duty cycle of this PWM pulse then the transistor induction will increase and thus the speed of the DC motor.
  • Now in order to control this PWM pulse I have used the LDR sensor, now depending on the LDR sensor the speed of DC motor will increase or decrease.
  • Now upload the below code in your Arduino software and Get the hex file from Arduino software.
int Motor1 = 2;
int Motor2 = 3;

int PWMControl= 6;

int PWM_Input = A0;

int PWM_Value = 0;
void setup() {
  pinMode(Motor1, OUTPUT);
  pinMode(Motor2, OUTPUT);
  pinMode(PWMControl, OUTPUT);
  pinMode(PWM_Input, INPUT);
  Serial.begin(9600);
}

void loop() {
  PWM_Value = analogRead(PWM_Input);
  PWM_Value = map(PWM_Value, 0, 1023, 0, 255);
  analogWrite(PWMControl, PWM_Value);
  if(Serial.available())
  {
    char data = Serial.read();
    Serial.println(data);
    if(data == 'C'){MotorClockwise();}
    if(data == 'A'){MotorAntiClockwise();}
    if(data == 'S'){MotorStop();}
    
  }
}

void MotorAntiClockwise()
{
  digitalWrite(Motor1, HIGH);
  digitalWrite(Motor2, LOW);
}

void MotorClockwise()
{
  digitalWrite(Motor1, LOW);
  digitalWrite(Motor2, HIGH);
}

void MotorStop()
{
  digitalWrite(Motor1, HIGH);
  digitalWrite(Motor2, HIGH);
}
  • So, now I am starting the simulation and then will send the commands via virtual Terminal and it will start moving and then by changing the LDR position DC motor speed control will take place.
  • I know its not clear from above figure so that's why I have designed this video. In the below video you will get the clear idea of DC Motor speed motor.
So, that's all for today. I hope you have got the idea of DC Motor Speed Control. Take care and have fun !!! :)  
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir