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!
What 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: https://learn.sparkfun.com/tutorials/how-to-use-a-…
Oscilloscope Use: http://eeshop.unl.edu/pdf/OscilloscopeTutorial–Be…
Step 1: Materials
DC Power Supply
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)
RJ-11 to Breadboard Adapter (DigiKey # H11394-ND)
Oscilloscope (Optional) – Allows you to see encoder signals
Oscilloscope Probes x 2 (Optional)
Multimeter (Optional) – Helpful for debugging issues
Step 2: 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.
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: http://microchip.com/downloads/en/DeviceDoc/39631E…. 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
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
JLCPCB Prototype: Only $2 for 10pcs 10×10cm, 24 hours Quick Turn, DHL Delivery in 3 days
Current Project / Post can also be found using:
- program for encoder interface with pic18f for MPLAB