DC motor speed and direction control with PIC16F877A and H-bridge

DC motor control with PIC16F877A and CCS C

The microcontroller PIC16F877A has two CCP modules CCP1 and CCP2, these modules could be used to give us two PWM signals. In this topic we are going to see how to use the two modules i order to control a DC motor speed and direction.
We can control the speed of DC motor using PWM technique as shown in the following topic:
DC Motor speed control with PIC16F877A and CCS PIC C compiler

And the direction of the DC motor can be controlled using H-Bridge circuit which allows us to reverse terminals polarity of the motor.
Related topics:
PIC16F877A ADC example with CCS PIC C compiler
PIC16F877A PWM example with CCS PIC C compiler

DC motor control with PIC16F877A circuit:
The following circuit schematic shows the full circuit of the project.
PIC16F877A DC motor speed direction control circuit
PORTB internal weak pull-ups are enabled in the software and therefore there is no need to add a pull-up resistor to pins RB0, RB1 and RB2.
There are 3 push buttons in the circuit 2 of them to choose rotation direction and the other one stops the motor. The two LEDs indicate rotation direction, if Direction 1 button is pressed the motor moves in the first direction and LED 1 on and the same thing for Direction 2 button and LED 2. The motor speed is controlled from potentiometer connected to RA0 (analog channel 0). The microcontroller PIC16F877A reads analog data from channel 0 and use the digital value to set the PWM duty cycle. If direction 1 button is pressed the microcontroller starts PWM1 (RC2 pin) and stops PWM2 (RC1 pin) and if direction 2 button is pressed the microcontroller stops PWM1 (RC2 pin) and starts PWM2 (RC1 pin), when the stop button is pressed the microcontroller stops PWM1 and PWM2 signals and the motor will stop.
The PWM (PWM1 & 2) frequency is 500Hz.
The basic elements of the H-bridge are the four MOSFETs (2 N-type and 2 P-type).
The PIC16F877A microcontroller needs to be supplied with 5V between oins VDD and VSS.

DC motor control with PIC16F877A CCS PIC C code:
PIC16F877A Timer2 is configured to generate a PWM signal with a frequency of 500Hz and the microcontroller runs with 8MHz crystal oscillator.
The microcontroller PIC16F877A reads RA0 analog value and stores the digital value on variable (i), this variable is used to set duty cycle of the active PWM (PWM1 or PWM2).

DC motor control with PIC16F877A video:
The following video shows more about this project with hardware circuit.


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top