top of page
Writer's pictureSanskruti Ashtikar

PWM-based DC Motor Speed Control Using PIC Microcontroller

Updated: Nov 30

Introduction


Controlling the speed of a DC motor is a common requirement in various industrial and hobbyist applications. Pulse Width Modulation (PWM) is an effective technique to control motor speed by varying the duty cycle of a square wave signal. This article explores how to implement PWM-based DC motor speed control using a PIC microcontroller, providing precise control over motor speed for various applications.





Overview of the Project


The project focuses on controlling the speed of a DC motor using the PWM technique. The PIC microcontroller generates PWM signals, which are fed to a motor driver circuit to control the motor's speed. By varying the duty cycle of the PWM signal, the average voltage applied to the motor changes, thereby controlling the speed.


Components Required


  • PIC Microcontroller (PIC16F877A): The core controller used to generate PWM signals and manage speed control.

  • DC Motor: The motor whose speed is to be controlled.

  • Motor Driver (L293D or L298N): An H-bridge motor driver to interface the PIC microcontroller with the DC motor.

  • Potentiometer: To provide a variable analog input for controlling the PWM duty cycle.

  • Oscillator (Crystal and Capacitors): Provides the necessary clock signal to the microcontroller.

  • Power Supply: To power the microcontroller and other components.

  • Resistors, Capacitors, and Diodes: Additional components for circuit stability and protection.

  • Connecting Wires: For making connections between different components.

  • Breadboard/PCB: For assembling the circuit.


Circuit Diagram


Below is a basic circuit diagram for PWM-based DC motor speed control:


Potentiometer  -->  PIC16F877A  -->  Motor Driver (L293D)  -->  DC Motor
                     |
               Oscillator (XTAL)
                     |
                Power Supply
  1. Potentiometer to PIC Microcontroller:

  2. The wiper (middle pin) of the potentiometer is connected to an analog input pin of the PIC microcontroller (e.g., AN0). This provides a variable voltage based on the potentiometer's position.

  3. The other two pins of the potentiometer are connected to Vcc and GND.

  4. PIC Microcontroller to Motor Driver:

  5. The PWM output pin of the PIC microcontroller is connected to the input pin of the motor driver.

  6. The motor driver’s output pins are connected to the DC motor, and the enable pin of the driver is controlled by the microcontroller.

  7. Power Supply:

  8. The PIC microcontroller, motor driver, and DC motor are powered by a suitable DC power supply, typically 5V for the microcontroller and a higher voltage (e.g., 12V) for the motor.


Working Principle


  1. Analog Input Reading:

  2. The potentiometer provides a variable voltage to the analog input of the PIC microcontroller. The microcontroller reads this analog value using its ADC (Analog-to-Digital Converter).

  3. PWM Generation:

  4. The microcontroller processes the analog input and generates a PWM signal with a duty cycle proportional to the input value. A higher input voltage results in a higher duty cycle, which increases the motor speed.

  5. Motor Speed Control:

  6. The PWM signal is fed to the motor driver, which controls the DC motor. By adjusting the duty cycle, the average voltage applied to the motor changes, thus controlling its speed.





Programming the PIC Microcontroller


The microcontroller is programmed using Embedded C or Assembly language. Below is an example of a basic code snippet in Embedded C for this project:


#include <pic16f877a.h>
#define POT_CHANNEL 0 // AN0
#define PWM_PIN RC2  // CCP1 output pin
// Function prototypes
void init_system();
unsigned int read_adc(unsigned char channel);
void set_pwm_duty_cycle(unsigned int duty_cycle);
void main() {
    unsigned int adc_value = 0;
    unsigned int duty_cycle = 0;
    
    init_system();
    
    while(1) {
        adc_value = read_adc(POT_CHANNEL);
        duty_cycle = adc_value >> 2; // Convert 10-bit ADC value to 8-bit duty cycle (0-255)
        set_pwm_duty_cycle(duty_cycle);
    }
}
void init_system() {
    TRISC2 = 0; // Set RC2 as output for PWM
    PR2 = 255;  // Set PWM period
    CCP1CON = 0x0C; // Configure CCP1 module for PWM mode
    T2CON = 0x07; // Timer2 ON, prescaler 1:16
    TMR2ON = 1; // Start Timer2
    ADCON1 = 0x80; // Configure ADC
    ADCON0 = 0x01; // Enable ADC
}
unsigned int read_adc(unsigned char channel) {
    ADCON0 &= 0xC7; // Clear previous channel selection
    ADCON0 |= (channel << 3); // Select ADC channel
    ADCON0 |= 0x02; // Start conversion
    while(ADCON0 & 0x02); // Wait for conversion to complete
    return (ADRESH << 8) | ADRESL; // Return 10-bit ADC value
}
void set_pwm_duty_cycle(unsigned int duty_cycle) {
    CCPR1L = duty_cycle >> 2; // Set the higher 8 bits of duty cycle
    CCP1CON = (CCP1CON & 0xCF) | ((duty_cycle & 0x03) << 4); // Set the lower 2 bits
}




Explanation of the Code


  • TRISC2: Configures RC2 as the output pin for the PWM signal.

  • PR2: Sets the PWM period. In this case, a value of 255 is used for an 8-bit resolution.

  • CCP1CON: Configures the CCP1 module for PWM mode.

  • T2CON: Configures Timer2, which is used for generating the PWM signal.

  • read_adc(): Reads the analog value from the specified ADC channel.

  • set_pwm_duty_cycle(): Sets the duty cycle of the PWM signal based on the input value.


Advantages of PWM-based Speed Control


  • Energy Efficiency: PWM is more energy-efficient compared to other methods of speed control, such as variable resistors, as it reduces power loss.

  • Smooth Operation: PWM allows for smooth and precise control of motor speed, reducing mechanical wear and tear.

  • Scalability: The system can be easily scaled or modified to control multiple motors or integrate with other control systems.


Conclusion


PWM-based DC motor speed control using a PIC microcontroller is a versatile and efficient method for controlling motor speed in various applications. By following the steps outlined in this article, you can build your own motor control system that provides precise and energy-efficient speed regulation. This project demonstrates the practical application of microcontrollers in motor control and offers a foundation for more advanced automation and control systems.


Want us to guide you through your project or make the project for you ?






Create Various Projects

Check out our Free Arduino Projects Playlist - Arduino Projects 

Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects 

Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects 

Check out our Free IoT Projects Playlist - IoT Projects 

Check out our Free Home Automation Projects Playlist - Home Automation Projects 

Check out our Free NodeMCu Projects Playlist - NodeMCu Projects 



16 views0 comments

Related Posts

See All

Comentarios