Summary of DC Motor Speed Control using PWM with PIC Microcontroller
This article explains how to control DC motor speed using Pulse Width Modulation (PWM) with a PIC Microcontroller, overcoming the inefficiencies of resistors. It details a project using the L293D driver and push buttons to adjust duty cycles via the CCP1 module on a PIC 16F877A.
Parts used in DC Motor Speed Control Project:
- PIC 16F877A Microcontroller
- L293D Motor Driver
- 12V DC Motor
- Two Push Button Switches
- MikroC Pro for PIC Compiler
- Power Supply (+5V for VDD, +12V for Motor)
I already posted about Interfacing DC Motor with PIC Microcontroller. In our robotics applications we may have to control the speed of the DC Motor. In this tutorial we will see how to control the speed of a DC Motor using Pulse Width Modulation (PWM). By using PWM we can easily control the average power delivered to a load and by thus we can easily control the speed of the DC Motor.
You may think that a variable resistor in series with a DC Motor can control its speed. There are three reasons for “Resistor is not a good choice for controlling the speed of a DC Motor”.
- The main problem is that the motor is a varying electrical load so a resistor can’t do this task. It needs more power during start up than in running state. It draws more current also when a mechanical load is applied to motor shaft.
- The resistor drops excess energy as heat. Thus it is not good for a battery powered device.
- We all know that motor requires more current, so resistors with higher power rating are required to drop excess energy.
PWM can be easily generated using the inbuilt CCP module of a PIC Microcontroller. CCP stands for Capture/Compare/PWM. CCP modules are available with a number of PIC Microcontrollers. Most of them have more than one CCP module. MikroC Pro for PIC Microcontroller provides built in library routines for PWM which makes our task very simple. Please refer the following articles.
In this example project DC Motor is interfaced with PIC Microcontroller using L293D Motor Driver. Two Push Button switches are provided to control the speed of the motor. Here we are using 12V DC Motor and average DC value delivered to motor can be varied by varying the duty ratio of the PWM. The average DC Voltage of 0% duty cycle is 0V, 25% duty cycle is 3V, 50% duty cycle is 6V, 75% duty cycle is 9V and for 100% duty cycle 12V.
Circuit Diagram – DC Motor Speed Control
Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.
Two push button switches are connected to 1st and 2nd pins of PORTD which is used to control the duty ratio of the generated PWM. Pressing the UP switch increases the duty cycle, which increases the motor speed while pressing the DOWN switch decreases the duty cycle, which decreases the motor speed. Here we use CCP1 module of PIC 16F877A to generate PWM and it is given to the enable pin of L293D. The direction of rotation of motor can be control using the 1st and 2nd pins of PORTB.
MikroC Code
void main()
{
short duty = 0; //initial value for duty
TRISD = 0xFF; //PORTD as input
TRISC = 0x00; //PORTC as output
TRISB = 0x00; //PORTB as output
PORTB = 0x02; //Run motor in anticlock wise
PWM1_Init(1000); //Initialize PWM1
PWM1_Start(); //start PWM1
PWM1_Set_Duty(duty); //Set current duty for PWM1
while (1) // endless loop
{
if (!RD0_bit && duty<250) //if button on RD0 pressed
{
Delay_ms(40);
duty = duty + 10; //increment current_duty
PWM1_Set_Duty(duty); //Change the duty cycle
}
if (!RD1_bit && duty >0) //button on RD1 pressed
{
Delay_ms(40);
duty = duty - 10; //decrement duty
PWM1_Set_Duty(duty);
}
Delay_ms(10); // slow down change pace a little
}
}
The parameter of PWM1_Set_Duty() is duty ratio which ranges from 0 to 255, ie 0 means 0% duty cycle and 255 means 100% duty cycle.
I think the program is self explanatory, so if you have doubts please comment below.
You can download the hex file, MikroC source code, Proteus files etc here…
DC Motor Speed Control with PIC Microcontroller
For more detail: DC Motor Speed Control using PWM with PIC Microcontroller
-
Why is a variable resistor not a good choice for controlling DC motor speed?
A resistor cannot handle the varying electrical load of a motor, drops excess energy as heat making it unsuitable for battery devices, and requires high power ratings. -
How can PWM be easily generated for this project?
PWM can be generated using the inbuilt CCP module of a PIC Microcontroller which stands for Capture/Compare/PWM. -
What voltage corresponds to a 50% duty cycle in this specific setup?
A 50% duty cycle delivers an average DC voltage of 6V when using a 12V motor supply. -
Which pins are used to control the motor direction?
The direction of rotation is controlled using the 1st and 2nd pins of PORTB. -
How does pressing the UP switch affect the motor?
Pressing the UP switch increases the duty cycle, which results in increased motor speed. -
What range does the duty ratio parameter take in the MikroC code?
The duty ratio ranges from 0 to 255, where 0 means 0% duty cycle and 255 means 100% duty cycle. -
Which module is used to generate the PWM signal for the motor enable pin?
The CCP1 module of the PIC 16F877A is used to generate the PWM given to the enable pin of the L293D.

