PWM speed control of DC motor using MSP430 Launchpad and L293D

Pinterest icon

PWM speed control of DC motor using MSP430 Launchpad and Motor Control Booster Pack

In the previous article we learned how to control a dc brushed motor using MSP430G2553 and L293D motor driver chip,the main focus in that article was about starting and controlling the direction of motors.The motors were allowed to be rotated at their maximum speed.In this article we are going to implement a simple PWM (Pulse Width Modulation) based speed control for DC brushed motors using MSP430 Launchpad.

Here i am using a Motor Control Booster Pack for MSP430G2553 Launchpad called Robot Shield

Buy MSP430 Motor Control Booster Pack from here  Buy MSP430 Launchpad Shield for Motor Control from here

The Full details of the Motor Control/RS485 communication Booster Pack for MSP430 Launchpad can be found here


link to xanthium enterprises repo on Github containing code for serial communication using CSharp and Dot net Framework 

All the C source files and Circuits used in this tutorial  can be downloaded from our GitHub Page.

Please use the codes in the " MSP430-Booster-Pack-PWM-Speed-Control " Folder in the Repository. 

If you are new to Github Check this article to download code .


Pulse Width Modulation using MSP430

PWM is a scheme for varying the power provided to the load by changing the ON/OFF time of the waveform applied to the load.In this method we will be applying a square wave to the Load (here DC) motor and by controlling the ON time (time in which waveform is high),we will able to control the average voltage applied to the motor and hence its speed.

As the ON time is increased the average voltage applied to the motor also increases hence the speed of the motor increases.When the ON time is reduced the average voltage applied to the motor also get reduced and the speed goes down.

Circuit Diagram

Here we will be using the L293D motor driver chip present in the MSP430 Motor Control Shield to control the DC motors.To control the speed of the motors connected to L293D we will be applying PWM signals to the enable pins 1&2_EN and 3&4_EN.

The pins P2.0 and P2.6 of MSP430G2553 are connected to L293D enable pins 1&2_EN and 3&4_EN respectively (highlighted in red).The connections are shown in the circuit diagram below.

MSP430 interfaced with L293D motor control

L293d circuit diagram for interfacing with motors


#include "msp430g2553.h"
void main(void)
   //------------------- Configure the Clocks -------------------//
   // Set DCO Clock = 8MHz  
   if (CALBC1_8MHZ==0xFF)   // If calibration constant erased
       while(1);            // do not load, trap CPU!!
   DCOCTL  = 0;             // Select lowest DCOx and MODx settings
   BCSCTL1 = CALBC1_8MHZ;   // Set range
   DCOCTL  = CALDCO_8MHZ;   // Set DCO step + modulation 
   // Port Configuration
   P2SEL  &= ~BIT6;                // Clear P2.6 in P2SEL  (by default Xin)
   P2SEL2 &= ~BIT6;                // Clear P2.6 in P2SEL2
   //Right Motor Control pins
   P2DIR |=  BIT0 + BIT1 + BIT2;   // P2.0,P2.1,P2.2 all output
   P2OUT &= ~BIT0 + BIT1 + BIT2;   // Clear P2.0,P2.1,P2.2  
   //Left Motor Control pins
   P2DIR |=  BIT3 + BIT4 + BIT6;   // P2.3,P2.4,P2.6 all output
   P2OUT &= ~BIT3 + BIT4 + BIT6;   // Clear P2.3,P2.4,P2.6 
   // Rotate the Right Motor clockwise
   P2OUT |=  BIT1;              // P2.1 = 1,
   P2OUT &= ~BIT2;              // P2.2 = 0
   // Rotate the Left Motor clockwise
   P2OUT |=  BIT3;             // P2.3 = 1,
   P2OUT &= ~BIT4;             // P2.4 = 0,
   // Timer 0 Configurtion
   TACTL |= MC_0;                   // Stop Timer0_A3 
   TACTL  = TASSEL_2 + TAIE;        // TAR Clock = 1MHz,1MHz/8 = 125KHz,Enable Timer Interrupt
   CCTL1  = CCIE;                   // Enable interrupt for CCR1 
   CCR1   = 6000;                // Load value 
   TACTL |= MC_2;                   // Start Timer0 in Continous Mode
   _BIS_SR(LPM0_bits + GIE);        // Go to Sleep in LPM0 with interrupts enabled   

 #pragma vector = TIMER0_A1_VECTOR
 __interrupt void TimerA(void)
        case 0x002: // CCR1 interrupt
                    P2OUT |=  BIT0;    // P2.0 = 1 ,1&2_EN = 1,Right Motor is started
                    P2OUT |=  BIT6;    // P2.6 = 1 ,3&4_EN = 1,Left  Motor is started
        case 0x004:  //CCR2 not used
        case 0x00A: //TAR overflow interrupt
                     P2OUT &= ~BIT0;     // P2.0 = 0 ,1&2_EN = 0,Right Motor is stoped
                     P2OUT &= ~BIT6;     // P2.6 = 0 ,3&4_EN = 0,Left  Motor is stoped


The MSP430G2553 is clocked at 8MHz DCO clock by loading the constants into the Basic Clock System register (BCSCTL1) and DCO control register (DCOCTL).The Port2 pins required for controlling the L293D chip is intialized and then logic levels ( 1A,2A and 3A,4A) for controlling the direction of Right (MOTOR_R) and Left (MOTOR_L) motor are set .Please note that we are implementing Software PWM here.

Timer A is then configured to operate from a 125Khz clock.The interrupt for CCR1 is enabled and Timer A is started in Continuous mode.The value in CCR1 register is used to control the width of the pulse.When Timer A counts upto the value in  CCR1,CCIFG is set and the code jumps to the Interrupt Service Routine (ISR) for CCR1 interrupt ( case 0x002).Inside the interrupt both P2.0 and P2.6 are set thus enabling the two motors.

P2.0 and P2.6 remains set till the Timer Overflows and the code jumps into the overflow section in the ISR (case 0x00A).Inside the ISR P2.0 and P2.6 are cleared thus disabling the motor.

Now the speed of the motor is controlled by adjusting the value in CCR1 register.When the value in CCR1 is increased to lets say 64,000 the duration of ON time is reduced thus reducing the effective voltage available to motor and thereby its speed.In the figure below you can see the effect of increasing CCR1 value (here CCR1 = 64000).You can also see the change in power by watching LED's D7 (LEFT) and D5 (RIGHT) on the MSP430 Motor Control Booster Pack,the LED's glow dimly.

PWM waveform for reducing the speed of the motor

Now when you reduce the CCR1 value to lets say 6000,you are increasing the ON time and thus increasing the speed of the motor as illustrated by the figure below.The Led's D7 (LEFT) and D5 (RIGHT) in this case will glow brightly signifying an increase in effective power.

PWM wave forms for increasing motor speed using msp430