How to Program a PIC Microcontroller & Read an Encoder

Introduction: From electronic toys & games to microwave ovens, microcontrollers can be found just about anywhere these days! But how can we make these small computers work for our own electronics projects? That’s where this tutorial comes in – with detailed instructions showing you what to do every step of the way. At the end of this project, your microcontroller will even be able to track the speed of a motor!
How to Program a PIC Microcontroller & Read an EncoderWhat to know: Before we get started though, there’s some prior knowledge that will not be covered in this guide. If you’d like to brush up your knowledge on any of those topics (listed below), feel free to check out the links.


Voltage Measurement:…


Oscilloscope Use:–Be…

Step 1: Materials

Major Components in Project

Note: Materials listed with an asterisk can be substituted depending on what you may have available. The components used for this tutorial that fall into that category are listed in parentheses. Please note that substitutions may render portions of this guide irrelevant (i.e. a portion of code may not work if a different microcontroller is used).


MPLAB X IDE (Free from:…
MPLAB XC8 (Free from:…


DC Power Supply
Banana Cables
PIC Programmer/Debugger* (MPLAB ICD 3)
RJ11 Connector [Included with MPLAB ICD 3]
USB 2.0 Cable [Included with MPLAB ICD 3]
PIC Microcontroller* (PIC18F4520) – Be sure to use a DIP microcontroller so that it can be put into a breadboard.
Device with a rotary encoder* (Pololu Item # 2269)
10Ohm Resistor
RJ-11 to Breadboard Adapter (DigiKey # H11394-ND)

READ  Interfacing Stepper Motor with PIC Microcontroller

Oscilloscope (Optional) – Allows you to see encoder signals
Oscilloscope Probes x 2 (Optional)
Multimeter (Optional) – Helpful for debugging issues

Step 2: Power Supply Setup

 Power Supply Setup

After acquiring the necessary equipment, our next task is to setup the breadboard & get power to the PIC microcontroller (MCU). That means it’s time to hook up the DC power supply.

Power Supply: As you’ll see in the picture of the Acopian DC power supply pictured in this step, each power supply channel has three colored connectors to which banana cables can be connected. For the purposes of this tutorial, we’ll need +5V and +0V (ground) from the power supply. In order to supply +5V and +0V, connect the negative terminal and grounded terminal of the power supply together (the green and black terminals in the picture provided). Then attach a banana cable to both the positive (red) terminal and the grounded (green) terminal that will be connected to the breadboard. These terminals, found on the breadboard, are shown with connected banana cables in step 3.

Step 3: Breadboard Setup

Breadboard: It’s time to setup the breadboard. After many wiring disasters of my own, I would suggest that you keep wire colors consistent. In this tutorial you’ll see +5V wires in red and +0V wires in green with the exception of the wires in the RJ-11 to breadboard connector (which were wired by a company). Please also note that it is helpful to setup +5V and +0V on the breadboard’sterminal strips.

Note: The pictures provided above show the same connections mentioned below and help in interpreting the instructions below.

READ  PIC18F2550 Project Board using pic microcontroller

Pin Numbers: Figuring out where to connect +5V and +0V (Ground) is as easy as opening up this link (containing the datasheet) and scrolling down to the fourth page:…. There you’ll find that the following connections need to be made:

Pin 11 to +5V
Pin 12 to +0V
Pin 31 to +0V
Pin 32 to +5V
Pin 1 to one side of the 10Ohm Resistor (not mentioned in datasheet)

Additionally, the following connections need to be made with the RJ-11 to breadboard adapter (hereafter referred to as RJ-11 Adapter). The picture of the adapter associated with this step also shows what to connect.

RJ-11 Adapter Yellow Wire: To +5V
RJ-11 Adapter Blue Wire: To the other side of the 10Ohm Resistor
RJ-11 Adapter: Green Wire: To +0V
RJ-11 Adapter Red Wire: To Pin 40
RJ-11 Adapter Black Wire: To Pin 39
RJ-11 Adapter White Wire is NOT Connected

As a rule of thumb you can remember that integrated circuit chips start with pin 1 at the top left hand corner and finish by moving down the left side, then back up the highest numbered pin (top right corner). To tell the top from the bottom there is usually an indent or dot on the top side of the chip.

Also note that VDD = +5V and VSS = +0V (ground)


For more detail: How to Program a PIC Microcontroller & Read an Encoder

Current Project / Post can also be found using:

  • encoder how to program\
  • reading an encoder with a picaxe microconr\troller
READ  Almost No Parts' 12/24hrs LED Clock using pic microcontroller

Leave a Comment

= 5 + 2

Read previous post:
Square Function Generator
10MHz DDS Sine/Square Function Generator based on the AD9835 using PIC16F628

An extremely simple and low cost Sine/Square wave generator based on the Analog Devices AD9835 Direct Digital Synthesis (DDS) Generator...

Scroll to top