Learning to Program the PIC Microcontroller

September 30, 2014

I've been using Arduino for a while now (2 years on and off) and they are great development boards that are easy to learn. The only problem is that it gets expensive to include full boards in each project so I've been looking at cheaper alternatives.

I recently learned about the Microchip PIC range of micro-controllers from a friend at my local makerspace. These chips are easy to use on their own with practically no external components so are much easier to set up in a custom circuit compared to the ATMEGA328P (the micro-controller used in the Arduino Uno). The chips have their own internal oscillator so an external crystal isn't needed for low-frequency projects.

An external programmer is required to upload code onto the chip so I bought a PICkit 3 for just under $50 (this a low cost alternative to the $200 ICD3). Here's a photo of my first circuit using a PIC. I'm using the PIC12F683 which is a mid-range 8 pin PIC costing less than $2. The only external components I needed were a voltage regulator to provide a stable 5V and 0.1uF capacitor between the power rails.


After installing the MPLAB X IDE and the XC8 compiler (both free), I was able to upload the following code to blink an LED connected to GPIO5 (pin 2). I've also made this code available on github (https://github.com/andygrove/blink_led_pic12f683).

#define _XTAL_FREQ 8000000

#include <xc.h>
#include <pic12f683.h>

// select internal oscillator
#pragma config FOSC = INTOSCIO

int main()
  TRISIO5 = 0; // equivalent of pinMode(5, OUTPUT)
  while(1) {
    GP5 = 1; // equivalent of digitalWrite(5, HIGH)
    GP5 = 0; // equivalent of digitalWrite(5, LOW) 
  return 0;

This is a small first step in my PIC adventure but I don't think I've ever been so excited about seeing an LED blink. If I can blink an LED I can do anything! Being able to do this for a total cost of around $2 is an order of magnitude cheaper than using an Arduino so this is really going to open up more possibilities for me with this hobby.

I should point out that the learning curve of PIC is a bit steeper than that of the Arduino because the PIC just doesn't have the rich ecosystem of books, blogs, and tutorials that the Arduino has. Also, the source code is lower level and changes depending on which PIC chip you are using. I wouldn't recommend PIC for everyone.

However, if you don't mind reading data sheets and you are comfortable with low level C / Assembler coding and dealing with bit-level logic then these are great micro-controllers to use. I actually like the fact that I'm dealing directly with the hardware rather than going through the abstraction layer provided by the Arduino/Wiring libraries. It is going to result in more efficient code for sure and I will learn much more this way.