We can control the speed of a DC motor through a microcontroller . All we need to know about PWM .That means "

**Pulse Width Modulation**" . It is a very popular technique and most important part of a microcontroller . In this tutorial we will use PWM to create signal . By changing pulse width of a signal ,we can control the speed of anything .

So if you don't know how to interface DC motor with microcontroller . It's recommended to read this tutorial ,

## Basic Description:

At first we will connect a DC motor with PIC microcontroller and two push buttons with it also . One of those buttons will be used to increase speed and another one for decreasing speed .That's all .
Now listen to me carefully , Just keep this in your mind

1. Increasing duty or Pulse Width will increase the speed of DC motor .

2.Decreasing duty or Pulse Width will decreasing the speed of DC motor.

Now , we need to know about PWM [ Pulse Width Modulation ]

## Pulse Width Modulation (PWM)

Pulse width modulation |

when Set_Duty(127); |

PWM |

**PWM**or

**CPP**module look at the picture .

In

**L293D**'s

**EN1**pin we provide the signal . If you provide highest pulse width , the speed will be highest . When decrease pulse width , the speed will also decrease .

Time Period = Times of ON state + Times of OFF state .

### Remember this :

####
**PWM has four functions **

####
**1. PWM1_Init( Set_Frequency );**

####
**2. PWM1_Start(); **

####
**3.PWM1_Set_Duty( Count );**

####
**4.PWM1_Stop(); **

Here most important thing is PWM1_Set_Duty(count) , If we increase the value of count the speed will increase . If we decrease the value of count , speed will decrease . Another important thing is the highest value of count is 255 and lowest is 0 . **count**proportional to

**PW**

So,

**count = K * PW ----eq(1)**where

**K = constant .**

**count/PW=K**

When

**PW=T , count=255**and when

**PW=T/2 , count=127.5 or 127**

255/T=127/(T/2) = K

**and , inserting values of PW and count in eq(1)**

255=K * T----eq(2)

127.5=K * (T/2)------eq(3)

Now eq(2)+eq(3) : 382.5= K { T +(T/2)}

or 382.5=K*(3T/2)

or 765=3*K*T

or T*K=255

or K=255/T

Now inserting value of K into eq(1) , we get

**Finally we get , count =255*(PW/T)**, That is not very necessary for this tutorial . But i shared all i know .

When value of count increase , the speed increase . When pulse width is equal to T it means total time period it remains ON . Now look at the picture given below :

Now create a project in proteus

## # Proteus Circuit :

## #MikroC Code :

```
void main() {
unsigned int count=0;
ADCON1=0x0F;
CMCON=7;
TRISA.F0=1;
TRISA.F1=1;
TRISC.F1=0;
TRISC.F0=0;
PWM1_Init(1000);
PWM1_Start();
PORTC.F0=1;
PORTC.F1=0;
PWM1_set_duty(count);
while(1){
if(PORTA.F0==0){
delay_ms(100);
if(count>=255) {
count=255; }else{
count=count+15;
PWM1_set_duty(count);
}
}
if(PORTA.F1==0){
delay_ms(100);
if(count<=0){
count=0; }else{
count=count-15;
PWM1_set_duty(count);
}
}
}
}
```
(adsbygoogle = window.adsbygoogle || []).push({});