logo
background
 Home and Links
 Your PC and Security
 Server NAS
 Wargames
 Astronomy
 PhotoStory
 DVD making
 Raspberry Pi
 PIC projects
 Other projects
 Next >>

The PIC (and PIC type micro-controllers)

PIC basics

What's a PIC ?

The Microchip PIC (Programmable Interface Controller) is an unbelievably cheap programmable device containing a CPU, a small amount of Flash memory for your program code and a (very limited) amount of RAM. In package sizes from 8 pins upward, all have i/o pins that can be programmed as inputs or outputs and most have additional on-board circuits (timers, serial comms, PWM, analogue inputs, DAC etc.)

The base-line PIC

The 'low end' (10F/12F, 16F5x) or 'baseline' devices can cost less than 20p each (in qty. 10pcs, eBay - or less then 40c in qty. 5,000 from Microchip itself - UK on-line stores (RS Components, CPC, Element14 etc) charge at least double this price although CPC occasionally 'remainders' PIC chips in it's 'Bargain Corner'.

Surface mount packages are typically only a few pence cheaper than the DIP, however you then have to allow the extra cost of a TSSOP-DIP 'converter' which may be only a few pence but then wipes out any saving
 
There is one big advantage of using surface mount - this is that the 'converter' can act as a 'mini-PCB' for your entire circuit .. so (for example) a 28 pin TSSOP 'converter' used with a 4 to 20 pin TSSOP PIC leaves at least 8 'free' positions for other devices (diodes, capacitors etc) to be added
 
The baseline PIC's contain an 8bit CPU (supporting 33/35 instructions), up to 1kb of 12bit Flash program memory, a few dozen bytes of RAM and at least 4 or 6 i/o pins and run at 1-5 MIPS.
 
Note that some basic types (such as 16F5x) have no internal oscillator circuits (i.e. they require an external clock / crystal for full speed operation (typ. 20MHz) or a R-C (limited to 4MHz)).

Mid-range PIC

Mid range (and extended mid-range) devices (16Fxxx, 18F) typically cost 50p or more. The main difference between 'low' and 'mid' range (in addition to the extended 14bit, 35 instruction set) is their support for (at least one) interrupt(s) and a deeper 'Return' stack.

Unlike the low-end, the surface mount package can be significantly cheaper than the DIP = for example I managed to find the PIC 16F690 in SOIC 20pin package for only £1.01 each whilst the DIP version can't be had for less than £3.
 
Of course you then need an 'adapter' mounting PCB. You can get 10x SOP20 PCB's for about £1 (or 50x for £3.30). For extra space, there's 20x 'TSSOP/SOP 24' for £2.10 (or 50x for £4.40), however I purchased 20x 'TSSOP28 to DIP28 Adapter Converter PCB Board 0.65/1.27mm' for less than £1.80 (so 9p each) which gave me lots of space for additional components (see later)
 
All mid range devices have internal oscillators with the option of using your own external oscillator source (although some can run 2x faster using their internal osc.). Some devices incorporate dual internal oscillators (the second is usually 32kHz for 2^n timing).
 
At the top of the 'mid range' are the 48MHz capable devices that incorporate support circuits for USB operation.

The 'high end' 16bit / 'top end' 32bit devices

The 24F and dsPIC high-end devices typically cost less then £5, contain a 16bit CPU with multi-kb's of Flash (up to about 192kb), RAM (up to about 8kb), and a 16bit CPU with an instruction set 'optimised' for running C code. They typically run at 32-64MHz (the dsPIC use osc/2 CLK rather than the 'normal' osc/4)

The high-end instruction set is so complex that it just isn't worth while trying to learn it and program these devices in 'assembler'.
 
Further, by the time you have added in the cost of components needed to support a typical Project, you might as well be using a Raspberry Pi Zero.

Finally there are the 'top-end' devices (32F) that incorporate a 32bit CPU with lots of program space and (a few) kb's of RAM. Shopping around these can sometimes be had for just over £5

These are totally outclassed in price/performance by the Raspberry Pi Zero / ZeroW, so are no longer a viable choice for the hobbyist DIY user (especially as most are only available in 'hard to use' square format surface mount packages)

Common elements

All PIC's have programmable i/o pins, many of which can be set for analogue input. All PIC's come with some 'built in' support circuits = such as a timers/counters, analogue comparators, 8/10bit ADC. Starting with the mid-range you get more complex things like I2C, UART and (at the top of the mid-range) even USB (1.1) ! Many PIC's, even the lowish end ones, also contain some user (software) programmable EEPROM. The bottom end PIC's can achieve 1 MIPS, the top-end devices exceed 16 MIPS. Some have dual OSC that allows part of the chip to be run at RTC speed (32,768 Hz)

Note 1. The hobbyist is essentially limited to those devices available in *DIP packages ('PDIP' = Plastic DIP or 'SPDIP' = 'Skinny' PDIP), and whilst DIPs are limited to 40 pins, this still means you can get up to 36 i/o pins (see, for example, PIC16F877 / 887 both available for less than £2 ea. (eBay))
 
Many PIC's offered in the DIP package are also available in a surface mount ('SOIC' or TSSOP) package, which can be significantly cheaper (eg  PIC16F690 in TSSOP can be had for about £1, whilst in a 20pin DIP it's almost £4), even after adding the cost of an mini-PCB TSSOP-DIP adapter (cost depends on pin count - eg. "20Pcs SOP8 SO8 SOIC8 TSSOP8 MSOP8 to DIP8" typically 99p, so 5p each, or the more useful "20Pcs SOP28 SSOP28 TSSOP28 to DIP28" at £1.80 = 9p ea. Note, the double sided versions are the most useful as other components can be added)
 
The more 'higher end' the PIC is, the more i/o pins it has - and those with really high i/o pin counts (32F etc) only come in 'square' type surface mount packages (TQFP) which means a TQFP 'surface mount' PCB is needed. Such devices are virtually impossible to 'breadboard', although socket/adapters can be found for some 'J' lead packages if you search hard enough and are prepared to pay enough.

If you shop around on eBay long enough you will discover sellers inviting bids on lots of 10pcs 12F675 in the 8pin DIP package. These often go for just over £2 (so 20p each) ... or 'Buy it now' for just under £3 (so 30p each). Don't waste your time buying one or two chips from any UK seller - they just buy lots of 10 or 20pcs from China and resell them at outrageous mark-ups (although I did find one non-UK seller asking just under £18 for 50pcs in the surface mount package == which is still overpriced at 36p each !)

One huge annoyance to most hobbyists must be the way Microchip 'marketing' likes to 'advertise' (i.e. lie) about the ship specification. On all the low end devices, the instruction speed is 1/4 the clock speed. So, for example, a 16MHz device runs at 4MIPS.
 
In particular, the 'program space' is given in bytes, rather than in instructions.
 
So the PIC16F690 (a mid-range 14bit instruction device) is advertised as having '7kb program space'. Whilst that may well be the size in bytes, each instruction uses 14 bits, not 8bits. So the truth is that in 7kb you only have room for 4k instructions.
 
Since a look-up-table 'costs' one instruction location per byte stored, if you thought the '7kb' PIC16F690 would have plenty of room for a 4k LUT plus the code needed to use it, you would be out of luck (there is no way to 'pack' bytes into words as the instruction space is totally separate from the data space = see Harvard Architecture)

What other Micro-controllers exist ?

The most common alternatives to the PIC is the Atmel AVR family, which is the device behind the Arduino board (a 'pre-built' mini-system similar in many respects to the PIC based Basic STAMP / PICAXE or codeBUG = see below)

Arguably, the AVR has better instruction mnemonics (names) than the PIC, which makes it rather easier to program in 'assembler' (for sure it's hard to imagine anything that could be worse than the illogical PIC instruction 'names', although you can use Macro's to 'alias' the names to something easier to understand (even Microchip themselves do this :-)) - see my 'new PIC 33 instruction set macros' later.
 
In terms of functionality, there is little to choose between the two device ranges (at least in the 8 bit versions), other than the lack of any USB support in any 'low-medium end' AVR devices. When comparing, note that AVR's typically run at 1 MIPS per MHz whilst the PIC is 1/4 MIPS per MHz (i.e the PIC 'raw' OSC is divided by 4 to get the CPU speed).
 
In fact, Microchip (the makers of the PIC) now own Atmel, so being easier to program and having a better clock to CPU cycle ratio is plainly no indicator of success in the marketplace.
 
I suspect that it was the low end 'mass market' that determined the 'winner'. The 'lowest end' AVR was a little more expensive than the 'lowest end' PIC, and having (a lot) more instruction memory that the PIC didn't help (more instruction flash = bigger die size = lower yield) - indeed AVR devices also (generally) have a lot more RAM (registers) than the PIC (again, bigger die, lower yield)
 
When it comes to the 'higher end' devices, programmed in 'C', one drawback for the AVR devices is that Atmel charges stupid amounts of money for their C complier. Worse, this is 'locked' to existing devices and requires you to pay an annual 'support' fee if you ever want to program any future released devices. No doubt this 'put off' some potential users, even with the availability of good Open Source alternatives.
 
In contrast, the Microchip 'non-optimised' C compiler is free of charge for private/student use = and many users report minimal difference between the (free) 'non-optimised' and (charged for) 'optimised' compiled code (chances are you will want to code any bits that are vitally 'time-sensitive' in Assembler anyway)
 
Of course, now that MicroChip have 'won', all the above may change.

If you want USB support in a 'lost cost' device (and who doesn't ?), you might as well 'bite the bullet' and learn how to program the PIC (but don't expect the 'low end USB PIC' to have much RAM / Flash / CPU cycles 'left over' to do anything 'clever' (or, indeed, almost anything at all :-) )

Microchip has a huge range of 8 bit PIC devices, from the (more or less obsolete) 4MHz low-end through 20MHz (5 MIPS) mid-range all the up to 64MHz (16 MIPS) at the top end. So you can always find one 'to do exactly what you want' at the lowest possible price.
 
Note also that the ancient 4MHz (and 10MHz) designs are now being fabricated on modern manufacturing lines and (despite 'no change' in the Spec. sheets) these will almost always run quite happily at 20 MHz.
 
Much the same applies to the older 20 MHz designs (they are being fabricated on the '40MHz' line and are said to achieve 33MHz), whilst the older 40/48MHz parts are being manufactured on the same line that makes the 64MHz parts.

What's a BASIC Stamp ?

This is an ancient 3rd party (Parallax) 24-28 pin .6" (wide) DIP package containing a (surface mount) PIC chip with support components. It has a micoBASIC Interpreter built in. In it's time it was ground-breaking, however these days the BASIC Stamp is regarded as (much) too slow and (much) too expensive (all 3rd parties have to make their profit some-how :-) )

Whilst now only of historical interest, it's of note that the very first version (1994) was built using the PIC 16C56 ! (see here for how it worked).

The 'Stamp', whilst still being sold, is just way too expensive for any new DIY project = for the same price you can get the (Atmel AVR ATmega8 based) Arduino SBC (or even a couple of Raspberry Pi Zero's :-) )

What's' a PICAXE ?

A successor to the BASIC Stamp, it's a PIC pre-programmed with 'bootstrap' code to allow '3 wire in-circuit programming' which is double talk for 'using a Serial link' (something that a modern computers don't have = so you require an (expensive) USB to serial converter cable that runs as a 'virtual COM port') aimed at the 'educational' market. Or you can purchase a cheap USB to Serial cable from eBay for about 1/10th the cost and invert the Rx and Tx signals (or just invert the Rx/Tx lines on a PC Serial out. NB. the PICAXE uses 4800 baud).

Instead of an 'assembler' or 'compiler' (C language) support, you get a basic implementation of BASIC (interpreter ?) that turns the PIC into a bit of a kids toy.
 
Each pre-programmed PICAXE chip costs about 3 or 4 times the cost of the 'bare' PIC = for example the 'top end' PICAXE-40x2 (4x4k 'memory capacity', 1280 RAM / 56 'byte variables') at £5.39 is based on the 18F45K22 (a 32kb program space, 1.5kb RAM, 64MHz (16MIPS) device, that can be found for £1.55 (surface mount pkg, 10 off price, Farnell Element14)). In recent years the PICAXE has made a bit of a comeback, with prices dropping to about 2 times the cost of the 'bare' PIC.  For example, the low end PICAXE-20M2, which is based on the PIC16F1829 can be had for less than £3 if you shop around
 
The software,consists of a BASIC editor with a built-in SPICE simulation (for testing before programming), is free.
 
The basic 'PICAXE-28/40 Proto Board' (consisting of little more than a bare board with two empty chip sockets and a 'Phono' socket) will cost you about £15 and this will allow you to 'program' their 'bootstrap enabled' PICAXE chips, using their £12 custom USB-serial conversion cable (extra), for a total of about £27.
 
On the other hand, you could pay 5x less for 'bare' PIC chips and purchase an "expensive conventional PIC programmer" (their words) for less than £5 on eBay (the "K150" which supports almost every DIP package PIC and comes with a 4 wire 'In Circuit' programming cable for those that don't fit it's socket). It connects to your PC using a standard USB cable (available at your local £1 store), although if you want the branded "Microchip" standard PIC programmer ('PIC Kit 3') you can expect to pay £15, post free (USB cable included, ZIF socket programmer not included).
 
In contrast, the Raspberry Pi Zero will cost you £7.91 (mid 2019 prices, £4.66 + £3.25 postage, after having launched at £4 + £2.50 postage) and is still limited to 'one per customer order'. The Pi Zero operating system needs at least a £1 4Gb SDHC 'program store' which can be loaded using nothing more complex than your PC's 'multi-media' card slot.
 
The Pi Zero can be plugged straight into a USB socket on your PC (using an "OTG" cable, available from the £1 store) and both powered and controlled from there, so for less than £10 you have a complete 1.2GHz 512Mb RAM, 4Gb program store device that beats any PICAXE based system hands down.

In short, whist the PICAXE is somewhat more powerful than the 'Stamp' it's still way too expensive for any but school kids leaning BASIC. Although obsolete, you can sometimes find these devices being 'remaindered', so if you already have the expensive support components (prototyping PCB, programmer, cable) they can still be worth using (I suspect that the built in code will have been 'kraked' by now, so you can always program up your own)

What's' a codeBUG ?

The codeBUG is the final evolution of the BASIC Stamp, PICAXE line. It is based on the PIC18(L)F25K50 (12MIPS RISC CPU, 32kb program space, 2kb RAM) on a small PCB and specifically designed as a learning aid "for kids" = which is a way of saying it's a mega-overpriced 'educational' toy.

For £12.50 (double the Pi Zero, about the same as the Zero W (after adding postage)), you get a 5x5 LED display, 2 pushbutton switches, USB, 4 i/o and (via an expansion socket) I2C, SPI and UART.

Kids 'program' it using MS Blockly (a GUI based 'language' where you drag and drop 'Lego like' software 'modules' that 'bolt together' to form a 'program'), however direct programming in Python is also supported.

You can plug the codeBUG into your computer using a micro-to-standard USB cable and it will appear as a USB 'thumb drive' (so 'programs' can just be 'dragged and dropped')
 
Interestingly, the CodeBUG 0.1" 6pin expansion header socket allows it to be plugged straight into the Raspberry Pi i/o header pins :-)

The codeBUG is the end of the Stamp, PICAXE line and again just another expensive toy

What's a BBC micro:bit ?

The latest version of the 'BASIC stamp' concept, this time by the BBC and based on an ARM Cortex M0 CPU (13 MIPS, 256kb program, 16Kb RAM). It has an 'edge connector' strip along one side that supports between 6 and 17 i/o lines. In addition to MS 'Blocky' (a modular programming 'language'), it supports Java-Script and Python. The 'price is right' for schools (free) but it's priced rather higher for parents (the 'base unit' on it's own can be found for about £13).

The small size and edge connector does make it 'usable' by the DIY project builder, however it's hopelessly outperformed by the Pi Zero (which is about 1,000x more powerful). Price wise, it's a couple of £'s more expensive than the Raspberry Pi Zero, although about the same cost as the Pi ZeroW (which also incorporates Bluetooth, WiFi and a USB socket) at £9.60 + compulsory P&P of £2.50 + £1 SDHC = £13.10.
 
However price is the only specification that is comparable with the Pi ZeroW - every other specification compared to the Pi W (except ease of kiddie programming perhaps) shows what a joke the micro:bit is. Indeed, it's hard to see how the micro:bit would appeal to anyone beyond the parents of school kids.
 
Indeed, if anything, the micro:bit should be compared to the (obsolete) codeBUG (you can find a comparison of codeBUG and micro:bit here

In conclusion, the codeBUG and micro:bit are just (very) expensive ways for kids to create "toy" apps. If you need a single chip controller, learn assembler (or C) and use a 'raw' PIC chip. If you can't cope with assembler (or C) then by all means stick to Python = and use the Raspberry Pi Zero / ZeroW

What's a Pi Zero ?

Yes, the Raspberry Pi line is indeed based on a programmable 'PIC like' device = in the case of the Zero, that's the Broadcom BCM2835 !

The Zero has the original Pi BCM2835 chip on a tiny 'minimalist' circuit board. Released at $5 (£4), the 'headline' price has since (mid 2019) increased to £4.66 whilst postage has gone up to £3.25. Demand is still so high that distributors are still restricting customers to 'one per order', so the actual 'real world price' is £7.91.

However for that you get a 1GHz CPU, 24MFLOPS GPU, 512Mb RAM, 1x USB2, HDMI output and a micro-SDHC socket for your program/data storage (up to 64Gb), plus a set of PCB 'holes' for the 40 pin .1" i/o header (that has a decent number of i/o's, including UART, I2C etc) and even an option socket for the Pi Camera !

Even after the price hike, the Pi Zero is a complete 'game changer' in the 'embedded system' market. It wipes out most ATmel AVR's at a stroke and puts paid to all the 'higher end' PIC's (and all the mini-pcb 'kiddie toys')
 
The only reason for using a PIC now is to get sub-£1 pricing or because your application is battery powered = the PIC can run off anything for 3v to 5v, whilst the Pi needs 'exact' 5v (if you want USB, or exact 3v3 if not) and whilst the the Pi Zero only consumes 140mA (at 5v) a 'sleeping' PIC only consumes uA (i.e. essentially zero).

The Pi Zero makes every other Pi (including the new ZeroW) look very over priced.

The Pi ZeroW only adds Bluetooth and on-board WiFi .. and costs exactly double the plain 'original' Zero (and about the same as the micro:bit which also has Bluetooth built-in).

The additional functions of the ZeroW can only be adding a few pence to the manufacturing cost. However the 'margin' on the Pi Zero can't be more than 10% or so, whilst the Pi ZeroW 'margin' is undoubtedly higher than 50%. Distributors have to make their profits somehow.

Next up from the Zero is the Pi A+, which is still being sold at about 4x the price of the Zero for the single 'extra' of a Pi Display option socket = but with ONLY HALF THE RAM !

Allowing for the cost of adding one socket - but removing half the RAM - a 'fair price' for the A+ would be no more than that of the basic Pi Zero (i.e. about £4), however (as of end 2016) most vendors are still listing the A+ at £18 (which suggests a 'margin' of 80%+ !!)

The Pi B+ / B2 (both of which, as of 2017, are obsolete and no longer generally available) also has the Pi Display option socket as the A+, plus they came with an on-board USB/Ethernet hub, however these were even more overpriced compared to the Zero.

Only the Pi B3 is significantly different from the Zero to justify it's price. With a new Quad core CPU and 1Gb RAM it can run applications that the Zero never will, but for the typical price of the B3 (just over £32) you could (in theory) get 6 Pi Zeros !

When (and why) is a PIC better than a Pi ?

Battery power

The Pi's inability to run directly (i.e. without a voltage regulator) from either 'primary cell' or 'secondary' (rechargeable) batteries is it's 'Achilles heel'.

The PIC will run from 3-5v, so two primary cells at 1.5v each, but not two NiMH 1.2v, although 3 at 1.2 = 3.6 would be just fine. Three primary cells of 1.5v each gives 4.5v nominal, which is also fine for a PIC but not good for the Pi which needs at least 4.75 (although you can often 'get away' with using 3x '1.5v' because primary cells will be above 1.5v for about half their life). 4x1.2 NiMH rechargeable = 4.8v and this is the only combination that will work with both Pi and PIC. You can't use 4 x 1.5 = 6v, which is way too high for the Pi (which will burn-out) and risky with a PIC (spec is 6.5v absolute max. but primary zinc carbon / alkalies may start above this). So, to run the Pi on batteries, you almost always have to add the cost of a DC-DC converter**
 
**DC-DC converters typically come in 2 types, 'step up' and 'step down'. Both types have a 'dead band', which is typically 2v. To get 5v you either use a 'step up' with 3v (or lower) batteries OR a 'step down' with 7v (or higher). Neither approach is very 'convienient' = 2x 1.5 batteries will start at more than 3v plus to get 5v out from 3v in means drawing double the current. The other choice is at least 5x 1.5v = 7.5v (which will soon stop working as the batteries discharge, so you need 6x 1.5 = 9v 'to be sure').
 
However the availability of the cheap LiPO based PowerBank is starting to 'change the game' = even the basic (single cell) PowerBank will keep a Pi Zero running for hours and I have recently found these in the £1 stores !

The PIC will run from 2v to 6.5v without falling over (or burning out) and a PICs programmed Flash is more or less immune to sudden power loss or 'resets' (unlike the Pi, where sudden power loss or an unexpected reset can corrupt the SDHC card)

THe PIC also consumes a lot less power - indeed it can be run 'for years' from a 3v 'button cell' (so long as you let it 'go to sleep' and only 'wake up' when it needs to 'do something')

The PIC has the advantage over the Pi (including the Zero) when your application has to be run from battery/solar cell for days (rather than hours).

Analogue inputs

The Raspberry Pi lacks analogue inputs (no ADC, no 'comparator' functionality) and it's (single) PWM output is dedicated to stereo audio output (and it's single DAC is wired to the TV (AVI) output port). Further, the Pi is running an Operating System - that means your code is subject to 'variable' timing. This is a problem when you want to time something to 'fractions of a second' accuracy.

PIC's above the most basic contain an 8 or 10bit Analogue to Digital Converter (ADC) plus a number of analogue 'comparators', so these can be programmed to handle a range of basic 'analogue' tasks - for example, you can even build a basic 'Oscilloscope' using a PIC or two (something that is only possible with the Pi by adding an external A-D chip, as the Pi lacks internal A-D converter circuits). Most PIC's have an internal Vref (comes with the comparator) and many have PWM support circuits (although you can program PWM in software almost as easily).

Even basic PIC devices can be programmed to do a multitude of 'counting' type tasks (position sensing, basic motor control and 'timer' applications) - and even support some basic video functions (LED clock, dot matrix 'marching display' and even an OSD (On-Screen-Display) / VTI (video Time Inserter) sometimes even without the need for an external oscillator crystal !

Cost

A basic Pi Zero system will cost at least £10. Using a basic PIC and surface mount components on a small 'converter PCB', a PIC solution can (just !) come in at £1 i.e. 10x less

The low cost makes the PIC an ideal way to build 'intelligent sensors' = however this only applies to 'simple' detectors (temperature, movement etc). As soon as you start to consider image sensing you have to use the Pi.

The PIC 'wins' when it comes to simple 'position sensing' type applications, but for anythuing more complex you need the Pi

Performance

The Pi is at least 10x faster than the most powerful PIC - or more like 100x when you include the GPU. The Pi has HDMI output and USB2 'built in' plus it runs the Linux operating system. The Pi also comes with up to 1Gb RAM (more than 10,000x any PIC) and directly supports a plug-in SD chip that can provide 64Gb (or more) of program and data space (about 1,000,000x more than a typical PIC)

In short, the Pi (with it's GPU) can play HD movies whilst a PIC struggles to display 40x24 lines of 5x7 font B&W text on an ancient analogue TV screen. Indeed, the PIC and the 'kiddie toys' (codeBug, BBC micro:bit) are only suitable for use in projects that don't need to do anything more complex than "blink LED's" faster than a few times a second, whist the Pi is suitable for use in a project involving multi-media or web applications.
 
To get the most out of the PIC  you have to program it in Assembler - the Pi can be programmed in anything from Python to C++, even 'command line scripts' that make use of pre-assembled utility programs.

In many cases, the 'best' approach will be use a mix of PIC's and Pi's - see, for example, my 'CCTV Intruder Alarm system' or my 'DIY Telescope PushTo / GoTo' projects

Next page :- Using a PIC - (the basics)

[top]