A DC motor is an electro-mechanical device that converts direct current into mechanical energy by means of rotation of a shaft. It works on the principle of Lorentz force by which the current carrying conductor in a magnetic field experiences a force hence the conductor moves on the direction of force called Lorentz force. In this chapter, speed control of a DC motor using pulse width modulation (PWM) technique is described.

DC Motor

Generally, a DC motor consists of either an electromagnet or a permanent magnet and a wounded coil known as the armature. Direct current is applied to the armature by means of a carbon brush or by means of electromagnetic induction. Because of electromagnetic induction, armature moves on the direction of the force. DC motors are widely used in industrial automation, toys and robotics applications. The speed of the DC motor can be controlled either by controlling current to the armature or by using a variable power supply.


The fundamental principle of a dc motor is that whenever a current carrying conductor is subjected to a magnetic field, a torque is developed which is directly proportional to the strength of the current passing through the coil and the magnetic field. The direction of this force can be determined by Fleming’s right-hand rule which says if we stretch the middle finger, index finger, and thumb in mutually perpendicular directions in such a way that the first finger represents the direction of the current and the second finger points to the direction of the magnetic field, then the thumb, stretched in a direction perpendicular to both the current and magnetic field, will represent the direction of torque developed in the armature coil.

The commutator is made segmented to make the torque unidirectional. Each time the current reverses the magnetic field, the direction of force developed is reversed. An opposition force is required for energy conversion and this opposition force is being given by back emf, the voltage being developed in the armature conductors when they cut the magnetic field. This is the basis of working of a dc motor.

Speed Control of DC Motor Using PWM

Controlling the speed of dc motor can be done by different ways like using a potentiometer and also by a controlled current
to the armature. Apart from these techniques, pulse width modulation is the effective way to implement motor speed control. Pulse width modulation is a digital technique for coding a digital data into a pulsating signal which looks like a square wave. The applications including motor speed control, encoding messages in telecommunication systems, sound synthesis in audio amplifiers

PWM find applications including motor speed control, for encoding messages in telecommunication systems and for controlled switching in switch mode power supplies and for sound synthesis in audio amplifiers etc.

PWM uses a rectangular pulse train whose modulation results in the average value of the pulse sequence.


PWM in PIC Microcontroller

Devices in PIC family including PIC 18F2455/2550/4455/4550 have two CCP (Capture/Compare/PWM) modules and each of them contains a 16-bit register which can operate as a 16-bit capture register, a 16-bit compare register or a PWM master/slave duty cycle register which is primarily controlled by timer modules.

CCP, which stands for capture, compare and PWM, is a hardware module inside the PIC microcontroller. It helps to trigger events based on time. Capture mode allows us a duration based timing of an event. This circuit gives information regarding the current state of a register which constantly changes its value. In this case, it is the timer TMR1 register.

Compare mode compares values contained in two registers at some point. One of them is the timer TMR1 register. This circuit also allows the user to trigger an external event when a predetermined amount of time has expired. PWM module generates the rectangular pulses whose duty cycle and frequency can be varied by altering the PWM registers.


We can’t drive a dc motor directly through a microcontroller pin. It usually operates in 3.3v-5v and lower currents but DC motor normally works in 6-12v, 300mA and it has other drawbacks like the back EMF produced by the DC motor may harm the controller ports and damage them. The solution to the above problems is using motor driving circuit usually known as H-Bridges. They are basically built using FETs and many dedicated ICs are also available like L293D etc.

These are dual H-bridge motor drivers, i.e., by using one IC we can control two DC motors in both clockwise and counterclockwise directions. The L293D can provide bidirectional drive currents of up to 600mA at voltages from 4.5 V to 36 V.

The below circuit shows interfacing L293D with PIC microcontroller to control a DC motor.

L293D - DC motor circuit - Speed control of dc motor using PWM

DC motor circuit

In both ICs, drivers are enabled in pairs with drivers 1 and 2 are enabled by a high input to 1,2 EN and drivers 3 and 4 are enabled by a high input to 3,4 EN. When drivers are enabled, their outputs will be active and in phase with their inputs. When drivers are disabled, their outputs will be off and will be in the high-impedance state.



We are using the microchip’s XC8 PWM peripheral library to make the source code much simpler.

In the above circuit, we apply PWM signal to the 1-2 EN pin of the L293D which actually controls the first motor.

Here is the equation for calculating the frequency of the PWM signal:

According to the data sheet of PIC18F4550, PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)

For example, we use 20MHz clock and the o/p frequency is 2KHz;

whereas PWM period = 1/frequency (that will be 1/2000 = .0005)

.0005 = [PR2 + 1] • [1 / 20000000] • 16
PR2 + 1 = [.0005 • 20000000] / 16
PR2 + 1 = 625
PR2 = 624
PR2 = 0x270 ( 624 in hex)

/*include Header files */

#include <xc.h>
#include "config.h"     //configuration bit settings
#include <plib/timers.h>
#include <plib/pwm.h>
/*The speed of your internal(or)external oscillator*/
#define _XTAL_FREQ 20000000

void main()
 TRISD=0;  // PORTD as output
 /*set RC2 as PWM  out pin*/
 TRISCbits.RC2 = 0;
 /*configuring Timer 2 and set as clock source */
 Timer2Config = T2_PS_1_16;
 OpenTimer2(Timer2Config);/*open PWM at 2KHz*/
        /*set duty cycle*/
        SetDCPWM1("desired value");
        LATD2=1;  // motor state control pins


Spread the love, share this