DC Motor Control With Rotary Encoder and PIC MCU - Mikroc Projects

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Simple Projects

Simple Electrical and Electronics Projects

Home PIC Projects  Arduino Projects ESP8266 Projects MikroC Projects CCS C Projects MPLAB Projects PCBWay

PCBONLINE Contact

October 21, 2018 ⁄ Simple Projects

DC Motor control with rotary encoder and


PIC MCU | mikroC Projects
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. ACCEPT

This post shows how to control DC motor speed and direction of rotation using
PIC16F887 microcontroller, rotary encoder and L293D motor driver chip.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.

Last time I made a simple controller circuit that controls speed and direction of
Labels:
rotation of DC motor, project link is below:
7-SEGMENT 74HC595 ADC
DC Motor control with PIC microcontroller | mikroC Projects
BLDC MOTOR BME280 BMP280

DAC DC MOTOR DHT11 DHT22


In this example I used the rotary encoder shown in the following image:
DS18B20 DS1307 DS1621

DS1631 DS3231 GPS HC-SR04

ILI9341 TFT INTERRUPT JOYSTICK

L293D L6234 LCD LED LM35

LM335 LM4040 MMC/SD CARD

NOKIA 5110 PWM

REMOTE CONTROL ROTARY ENCODER

RTOS SSD1306 OLED ST7735 TFT

ST7789 TFT STEPPER MOTOR

THYRISTOR TRIAC UART

ULN2003 USB VGA

Archives
The rotary encoder has 5 pins: GND, + (+5V or 3.3V), SW (push button), DT (pin B) and
March 2020
CLK (pin A).
February 2020
As an addition to the rotary encoder there is a push button and three pull up
January 2020
resistors for pins SW, DT and CLK of 10k ohm each. With the three pull-up resistors,
December 2019
the normal state of each terminal is logic high. November 2019
The rotary encoder generates (when rotating) two square waves on pins A (CLK) and October 2019
B (DT) with 90° out of phase as shown in the figure below: September 2019
August 2019
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
As shown in the figure, the rotary encoder has 4 phases: January 2019

phase 1: A = 0, B = 0 December 2018


November 2018
phase 2: A = 1, B = 0
October 2018
phase 3: A = 1, B = 1
September 2018
phase 4: A = 0, B = 1
August 2018
July 2018
Hardware Required: June 2018
May 2018
PIC16F887 microcontroller  —->  datasheet April 2018
L293D driver                     —->  datasheet March 2018
Rotary encoder February 2018
12V DC motor January 2018
Breadboard December 2017
12V source November 2017

5V source September 2017


August 2017
Jumper wires
July 2017
June 2017
DC Motor control with rotary encoder and PIC16F887 circuit: November 2016
Project circuit schematic diagram is shown in the following image. October 2016
September 2016
August 2016
July 2016
May 2016
April 2016
March 2016
February 2016

Facebook Page:

Recent Posts

Interfacing Arduino with


LM335 Temperature Sensor
(All grounded terminals are connected together)
ESP8266 NodeMCU with
BME280 Sensor & ST7789 TFT-
The circuit is supplied with two power sources, one with voltage of 5V which supplies
Weather Station
the PIC16F887 microcontroller, the rotary encoder board and the L293D driver IC ESP8266 NodeMCU Interface
(VCC1), and the second source with voltage of 12V which supplies the L293D chip with BMP280 Sensor and
(VCC2) (therefore the DC motor). The DC motor nominal voltage is 12V. ST7789 TFT

The L293D driver has 2 VCCs: VCC1 is +5V and VCC2 is +12V (same as motor nominal
Sponsored Links:
voltage). IN1 and IN2 are the control pins where:

IN1 IN2 Function

L H Direction 1

H L Direction 2

L L Fast motor stop

H H Fast motor stop

The PIC16F887 generates a PWM signal on pin RC2 (#17) using CCP1 module (CCP:
Capture/Compare/PWM), this pin is connected to EN1,2 pin of the L293D chip. IN1
and IN2 pins are connected to RD0 and RD1 respectively (they can be reversed).

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

In this project the PIC16F887 microcontroller uses its internal oscillator @ 8 MHz,
MCLR pin is configured as an input pin.

DC Motor control with rotary encoder and PIC16F887 C code:


The following C code is for mikroC PRO for PIC compiler, it was tested with version
7.2.0.

Rotary encoder pushbutton (connected to RB2) and the control lines (connected to
RD0 and RD1) are defined in the code as shown below:

C
1 // define rotary encoder button pin connection
2 #define SW          RB2_bit
3  
4 // define motor control pins
5 #define IN1         RD1_bit
6 #define IN2         RD0_bit

Internal weak pull-up is enabled for pin RB2, the following lines are used for that:

C
1 // enable RB1 internal pull up
2 NOT_RBPU_bit = 0;      // clear RBPU bit (OPTION_REG.7)
3 WPUB         = 0x04;   // WPUB register = 0b00000100

PORTB interrupt-on-change is enabled for pins RB0 and RB1 which are respectively
connected to CLK and DT pins of the rotary encoder. This interrupt detects falling
and rising of the two lines:

C
1 INTCON   = 0xC8;   // enable global, peripheral and PORTB change interrupt
2 IOCB     = 0x03;   // enable RB0 & RB1 pin change interrupt

Full mikroC code:


Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700

C
1 /***********************************************************************
2  
3   DC Motor control with PIC16F887 MCU and rotary encoder.
4   C Code for mikroC PRO for PIC compiler
5   Internal oscillator used @ 8MHz
6   Configuration words: CONFIG1 = 0x2CD4
7                        CONFIG2 = 0x0700
8   This is a free software with NO WARRANTY.
9   https://simple-circuit.com/
10  
11 ************************************************************************
12  
13 // define rotary encoder button pin connection
14 #define SW          RB2_bit
15  
16 // define motor control pins
17 #define IN1         RD1_bit
18 #define IN2         RD0_bit
19  
20  
21 bit direction;
22 char last_read;
23 short quad = 0, change;
24 int   motor_speed = 0;
25  
26 void Interrupt()
27 {

The following video shows a similar hardware circuit where PIC16F877A


microcontroller is used:

Share this:

  

DC MOTOR ROTARY ENCODER

« DC Motor control with PIC microcontroller | mikroC Projects


Interfacing Arduino with Nokia 5110 LCD »

Leave a Reply

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

Simple Projects - 2021 Home Contact

You might also like