DC Motor speed/direction control using PIC16F877A and rotary encoder

This example shows how to use a rotary encoder with PIC16F877A microcontroller in order to control the speed and direction of rotation of a DC motor.

Previously I posted an example shows how to control a DC motor speed and direction using PIC16F877A and a potentiometer, example link is below:
DC motor control with PIC16F877A and L293D – Proteus simulation

In this example I used the rotary encoder shown in the image below:

rotary encoder

The rotary encoder has 5 pins: GND, + (+5V or 3.3V), SW (push button), DT (pin B) and CLK (pin A).
As an addition to the rotary encoder there is a push button and three pull up resistors for pins SW, DT and CLK of 10K ohm. With the three pull-up resistors, the normal state of each terminal is logic high.
The rotary encoder generates (when rotating) two square waves on pins A (CLK) and B (DT) with 90° out of phase as shown in the figure below:

rotary encoder output waveform

As shown in the figure, the rotary encoder has 4 phases:
phase 1: A = 0, B = 0
phase 2: A = 1, B = 0
phase 3: A = 1, B = 1
phase 4: A = 0, B = 1

Hardware Required:

  • PIC16F877A microcontroller
  • Rotary encoder
  • DC motor
  • L293D motor driver
  • 20MHz crystal oscillator
  • 2 x 22pF ceramic capacitors
  • 10k ohm resistor
  • Breadboard
  • 12V source
  • 5V source
  • Jumper wires

DC Motor speed and direction control using PIC16F877A and rotary encoder circuit:
Example circuit schematic diagram is shown below.

PIC16F877A rotary encoder DC motor controller circuit

All grounded terminals are connected.

The L293D quadruple half-H drivers chip allows us to drive 2 motors in both directions. With two PWM outputs from the PIC16F877A microcontroller we can easily control the speed as well as the direction of rotation of one DC motor. (PWM: Pulse Width Modulation).

The L293D is supplied with 2 different sources, the first one (VCC1) is +5V, and the 2nd one (VCC2) is +12V which is the same as motor nominal voltage. Pin IN1 and pin IN2 are the control pins where:

LHDirection 1
HLDirection 2
LLFast motor stop
HHFast motor stop

The PIC16F877A microcontroller has two CCP modules, CCP1 and CCP2 (CCP: Capture/Compare/PWM). By setting both modules in PWM mode we get 2 PWM signals with the same frequency on pins RC2 (PWM1) and RC1 (PWM2), at any time there is only 1 active PWM, this allows us to control the direction as well as the speed by varying the duty cycle of the active PWM signal. The active PWM pin decides the motor direction of rotation (one at a time, the other output is logic 0).

The rotary encoder push button terminal is connected to pin RB3, with this button we can change the direction of rotation of the motor. CLK and DT pins are connected to RB4 and RB5 respectively.

The Code:
The C code below is for CCS C compiler!

PORTB interrupt on change (IOC) is used to detect the movement of the rotary encoder.

The video below shows how this example works:

CCS C compiler rotary encoder example (ex_sk_rotary_encoder_isr.c)

1 thought on “DC Motor speed/direction control using PIC16F877A and rotary encoder”

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