Programming PICs using the PICKit2 and pk2cmd
I have used these neat little micro controllers from Microchip's PIC family for various projects throughout the years using different compilers, assemblers and programmers. What I have recently used is the PICKit2. The best thing is that Microchip has released Linux drivers for it! Here is how to get it all running. The first step is to obtain the release from www.microchip.com. I won't even try to place a direct link here since they seam to change their website structure on a regular basis just to make it harder to find what you are looking for. However locate the downloads for the PICKit2 and scroll down a bit. The Linux and MacOS drivers should be there. Note that the PICKit3 does NOT have drivers for anything but Windows so make sure you get a PICKit2. The latest release when I am writing this is 1.20. Microchip is kind and releases pre-compiled binaries for those who do not want to build from source but I will go through the building steps here to get it all covered. The release archive name is pk2cmdv1.20LinuxMacSource.tar.gz and the procedure is quite straight forward. Unpack, Compile. Install. There is no configuration step prior to compilation but the development libraries for usb-dev is required for the compile. Please note that I have only tested this under Debian Squeeze. EDIT 2014-06-19: Works fine under Debian Wheezy as well. Install required library (you might have this installed already) # apt-get install libusb-dev Unpack and compile # tar zxvf pk2cmdv1.20LinuxMacSource.tar.gz # cd pk2cmdv1.20LinuxMacSource # make linux Three files are required for everything to run and they have to be copied to somewhere in your PATH. # cp pk2cmd /usr/local/bin # cp PK2DeviceFile.dat /usr/local/bin/ # cp PK2V023200.hex /usr/local/bin/ Verify (as root!) that you can reach your PICKit2 dongle. # pk2cmd -?V Executable Version: 1.20.00 Device File Version: 1.55.00 OS Firmware Version: 2.32.00 Operation Succeeded If you get something like "OS Firmware Version: PICkit 2 not found" then make sure that the PICKit2 is connected and that you are running as root. # lsusb | grep Microchip Bus 003 Device 030: ID 04d8:0033 Microchip Technology, Inc. PICkit2 It is a bit ugly to be required to program your chips as root. This can be fixed by adding a udev rule. Create a new file /etc/udev/rules.d/99-pickit2.rules containg the following: SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device",SYSFS{idVendor}=="04d8" , SYSFS{idProduct}=="0033", MODE="0666" It is important that you restart udev and after that reconnect the PICKit2 for the new rule to apply. # cat /etc/udev/rules.d/99-pickit2.rules SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device",SYSFS{idVendor}=="04d8" , SYSFS{idProduct}=="0033", MODE="0666" # /etc/init.d/udev restart Now you should be able to run the PICKit2 as a regular user. Programming was a bit of trial and error for me but I will present the procedure I found was working. The pk2cmd application is used for all PICKit2 activities and it has quite a bit of arguments that can be issued. One of the most important arguments is "-P". It tells pk2cmd which kind of PIC we are actually programming. It can also be used to auto-detect the connected PIC. I think the best way of describing pk2cmd is by a set of examples. Run auto-detection $ pk2cmd -P Auto-Detect: Found part PIC16F630. Operation Succeeded Now we know which PIC we have connected (odds are that we knew this already). Erase the PIC $ pk2cmd -P PIC16F630 -X -E Erasing Device... Operation Succeeded Note that the device name from the auto-detection is now placed after the -P argument. Two new arguments is supplied now. -X tells the PICKit2 to "Use VPP first Program Entry Method". I get read/write errors if I do not supply this no matter how I connect my PIC's but your experience may be different. -E tells pk2cmd to erase the connected PIC. Program a HEX file to the PIC $ pk2cmd -P PIC16F630 -X -M -F code.hex PICkit 2 Program Report 16-8-2012, 10:21:05 Device Type: PIC16F630 Program Succeeded. Operation Succeeded Two new arguments again. -M tells pk2cmd to actually program the PIC. -F tells pk2cmd which file to use. The -M argument can actually also be either -MP, -ME, -MI or -MC to program only Program memory, EEPROM, ID memory or Configuration memory respectively but for most cases you will program the entire PIC using -M. Verify a program $ pk2cmd -P PIC16F630 -Y -F code.hex PICkit 2 Verify Report 16-8-2012, 10:28:59 Device Type: PIC16F630 Verify Succeeded. Operation Succeeded The argument -Y tells pk2cmd to verify the PIC's memory with the HEX file given by -F. Again the -Y argument can address different areas just as the -M argument by setting either -YP, -YE, -YI or -YC to verify a specific region only. The PICKit2 can be used to power a connected device. For example the "PICKit2 Low Pin Count Demo Board". The argument for turning on power is -T. Power ON $ pk2cmd -P PIC16F630 -T Operation Succeeded Power OFF $ pk2cmd -P PIC16F630 Operation Succeeded This was quick demonstration of the PICKit2 under Linux. Make sure to check out all the other arguments for pk2cmd.
by saminda konkaduwa 2013-09-14 13:33 UTC
Excellent. It worked. I have CentOS 6 64 bit. I used locally built PICKIT2 clone. Many thanks again for good post. saminda konkaduwa ( sri lanka )


by Daniel 2014-01-24 08:50 UTC
Good work Saminda. Good to know this guide is applicable to other Linux distributions as well. Thank you for sharing your results!


by JF Duhamel 2014-10-11 19:12 UTC
Thank You for this detailed procedure of installation, it works fine under linux Mint 17 64 bits (cinnamon edition) Best regards


by Ed 2015-01-06 10:22 UTC
I read something about a dongle, what is that ?


by Daniel 2015-01-07 10:06 UTC
Hi Ed, A dongle is a small piece of hardware that connects to a computer or laptop. In this case it refers to the PICKit2 module.


by Pravin 2015-08-03 20:36 UTC
Hi.. Thank you for the information. It helped a lot.. Cheers !!!


by JF Duhamel 2015-08-25 21:36 UTC
Good afternoon I installed already several months ago this way and it was working fine on my laptop under linux Mint 17. BUT I have changed my Laptop anf it is an HP 240G3 having USB3 ports with automatic switching USB3/USB3 switching. All other USB devices work fine, except the pickit2 which starts working once connected, but after a few commands executed, PK2CMD reports NO PICKIT2 (it looks non connected). The LSUSB command reports the PicKit2 as present, and I have to disconnect and reconnect the equipment after a few seconds to have again access to it, perform i.e an erase command end a blak check, the the pickit2 vanishes again! libusb 0.1.12 is istalled as well as libusb 1.0 (in standard on the system), same results happen wether I compile pk2cmd or use a binary pre compiled, the version used is 1.20.00 and device def file 1.55 as classic. I use a USB presto Asix programmer under Wine without any problems or also my old picstart plus on USB-RS232 adapter Could it be a problem of compatibility with the USB hardware of my laptop ? Pickit2 hardware is not to be involved as it is working on a netbook under Win 7 and the Microchip software... Best regards JF Duhamel


by Daniel 2015-08-29 11:23 UTC
Hi JF! Hard to guess what may be the problem here. Perhaps a power supply issue? Try running a "dmesg" right after the device disappears to see if the log provides some clue. Also try connecting a powered USB hub in between the laptop and the pickit device to see if that changes anything.


by Charles Baudouin 2015-12-08 16:31 UTC
Exactly the same problem on a ASUS labtop runing linux. Seems the first read for the pickit2 version number times out. Repowering the pickit solves the problem.


by biterror 2018-08-01 11:56 UTC
Same problem. On one laptop, one USB port pickit 2 works ok, but on all other machines and USB ports it works only once per connection. :-(( (xubuntu 16.x, 18.04, mint etc)


by spostma 2019-10-24 13:02 UTC
an improved PK2 boot loader code that solves the 'USB device not recognised' when pluggging in the PK2 programmer may help. See the last paragraph on: https://www.voti.nl/pickit2faq/index_1.html


by Arun 2022-02-24 08:46 UTC
Thanks for your guide. This helps to program a critical old hex to PIC18 Series. Your articles works like a charm. I am going to save this procedure for future use.


by knitter 2023-11-07 13:50 UTC
Good tutorial. This works also with the updated pk2cmd from here: http://kair.us/projects/pickitminus/index.html Even with a PICkit3!


Write a comment

Name or handle

E-mail (optional and not visible to others)

Comment


Code from above