The popularity of Arduino IDE is undisputable. This success was a great incentive for many other Arduino IDE clones. Introduced in 2011 by Microchip and Digilent and complemented by the initial release of two Arduino compatible PIC32 boards, MPIDE is a fork of the original Arduino 0023 IDE, with support for PIC32 microcontrollers. Since then MPIDE evolved to the current mpide-0150-20150812 release, and also the range of supported boards increased.
Most of the MPIDE-compatible boards used USB-to-serial FTDI chips, similar to the Arduino boards. The latest series of MPIDE bootloaders allows for direct USB connectivity, and an increased number of development boards are now able to run MPIDE. Such example is the Clicker2 for PIC32MX from MikroElektronika.If we take a look at the Clicker2 for PIC32MX, we find it’s very close to the Cerebot 32MX4ck board, later renamed as Diligent chipKIT Pro MX4 board. The only major difference is that the original chipKIT board uses a FDTI USB-to-serial, while the Clicker2 has a native USB connection. Unfortunately, the bootloader that comes with the Clicker2 for PIC32MX is not compatible with the MPIDE.
So, to make this board run in MPIDE one must replace the original bootloader with the chipKIT version.Here we are lucky: there is a USB compatible bootloader available for download. So, all we have to do is to download and extract the zip file, go to BootloadersCurrent-hex and pick the Cerebot-32MX4-USB.hex file. Use a PICkit 3 or similar programmer to burn the new bootloader.
At this moment when you connect the board to the PC it’s detected as Stk500v2. Of course, you need drivers for this. For WIN7 I found the following download link for the drivers.
Compatibility with Arduino IDE
Now comes the really funny part: what does work and what doesn’t. The first thing is to find the correspondence between Arduino pin definitions and the actual pins on the Clicker 2 for PIC32MX. Here’s what I found to be working:
With the above notations most Arduino code will run on the Clicker2 for PIC32MX. Don’t forget to set the board as “chipKIT Pro MX4” in MPIDE.
The only thing I couldn’t make work is the serialprint function, which tries to use the hardware serial port, and not the USB connection. Some care is also needed when using third-party Arduino libraries as some of those libraries are written using 8-bit Atmel C code and will not work with PIC32’s.
Another issue I found is that the board needs a hardware reset to reestablish the communication with the PC. So, every time when you wish to reprogram the board you need to press the reset button before uploading the sketch.
I also found that a LED connected to microcontroller pin RB10 (Arduino pin 64) can be used to provide some diagnostics in bootloader mode.
A simple MPIDE sketch
The following code is a modified example from MPIDE that turns off LED2 if Button #1 is pressed:
/* Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 4. This example code is in the public domain. Based on the original code http://www.arduino.cc/en/Tutorial/Button */ // constants won't change. They're used here to // set pin numbers: const int buttonPin = 4; // the number of the pushbutton pin const int ledPin = 69; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
Programming is very much the same as in Arduino IDE. For the board I selected “chipKIT Pro MX4”, then I pressed the reset button before uploading. All went smooth, and the code was running just as it would do on any Arduino board.
Some final thoughts
Does it worth all this hassle to change the bootloader? I think so, as I can benefit from a free IDE, and of all the available Arduino code.
Are there downsides? Yes!
The major downside is that when the power is turned off or the reset button is pressed the loaded program is lost. The board just gets into Stk500v2 mode, and waits for a new program to be loaded. I’m not sure is this is the intended behavior, or it’s a flaw in the code. I’m still investigating this issue…
Second, there is no debugger. Besides, some things don’t work for the moment (particularly I miss the serialprint, which is widely used for debugging). Maybe I will try using another serial-to-USB board to see if I can do some limited debugging using the native serial communication.
Update: I found a way to make it work using another bootloader and a USB-UART click.