Transferring Arduino Libraries to other MCU's

Hey guys, I play a lot with Arduino and have made multiple projects with them but I am now trying to transition to other MCU's, specifically PIC. I wanted to start buy taking a few of my Arduino projects and rewriting them to work on PIC but I am not sure how to strip down a Arduino library to work on PIC. Is this possible and if it is, do any of you have any suggestions on how I can go about this. Thank you.

Hi,
Start by learning a LOT about the actual hardware and software.

Read and get to understand the detailed data sheets and underlying architecture of the microcontrollers.

Maybe start here about Arduino: ArduinoBoardDetails - ArduinoInfo

Read stuff on Wikipedia, like: ATmega328 - Wikipedia

Here’s the ATMEL Datasheet: https://arduinoinfo.mywikis.net/w/images/3/35/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf

The short answer is, it won’t work…
In most cases you’d have to re-write the libraries and most of your other your code and/or figure-put how to use the PIC libraries. Different processors (or different processor families) have different internal architecture and use different machine languages.

C and C++ are “portable” languages which means a program that uses the standard language library can be complied and run for a different computer processor as long as you have a C/C++ compiler.

But… A lot things in the Arduino library are NOT standard C/C++. For example, there is no analogWrite() or digitalWrite() in standard C/C++.

And, all of the standard C/C++ keyboard input, display-output, and file storage functions don’t run on the Arduino because it doesn’t have a keyboard, display, or file storage. You can add a keyboard or display to the Arduino the there is no universal-standard way of interfacing with it and no operating system to help with that…

…And in fact, standard C & C++ doesn’t include any mouse, color display, or graphics functions. Probably 90% of a Windows program written in C++ is NOT standard C++. That’s why most “real programs” are not available for Windows, Mac, and Linux. You can’t simply re-compile then for different platforms… It’s basically 3 times as much work to write a program for all 3 platforms and it requires 3 times the knowledge.

A powerful concept of portable software is a Hardware Abstraction Layer (HAL), that provides a common interface to different hardwares. Examples are the already mentioned digitalRead() and other standard functions, which can be ported easily to other architectures. Timer and other low level functions, like analogWrite(), instead are often hard to translate.

For implementation details see the "hardware\arduino..." folder in your Arduino installation. And note that not all Arduino "common" functions can have counterparts on other (PIC, ARM...) architectures. Many libraries add their own support for specific hardware, like for Uno, Mega, Tiny etc., in addition to the standard HAL functions. Then it's required to implement this set of specific functions and constants also for every new target.

Which PIC? Do you have a C++ compiler for it, or do you need to translate to plain C as well?
Do you already know about the ChipKit MPIDE Core

There is a chipKIT hardware package for the Arduino IDE:

This provides a standard Arduino core API that will allow many libraries to work fine for the supported PIC32 chips without any changes at all (this is the whole idea behind the standard API). In other cases, the library will contain architecture-specific code that needs to be ported to PIC. Even then, it will often only be specific sections of the library that need to be modified.

OLIMEX also has an Arduino hardware package for their PIC32 boards.

I believe there is at least one other Arduino hardware package for PIC, but I don't remember the name.

TL;DR: Arduino function for PIC32 is easy - a package already exists that supports several of the chips.
PIC24 and PIC33 would be a lot more difficult, but at least they are supported by gcc.
PIC16 and PIC18... Good luck with that!

(that said, implementing the core Arduino functions in a "similar" fashion on any microcontroller shouldn't be THAT hard. You start with a background timer, digitialWrite, digitalRead, pinMode, and Serial, then add other functionality "as needed." Peripheral support (SPI, I2C) will be significantly different.)

Well thank you guys for the info, I will do my best with this project and try to update on my progress.

I started with PICs and now use both PICs and Arduino. There's advantages to both, which you prefer is up to you. For PIC I use MPLAB X with MPLAB XC compilers. I also use a PICKit 3 but this has been superseded by PICKit 4 so if you are starting out with PIC probably get one of those. There are other development tools available but I've not tried them.

As others have commented, the thing you are going to have to learn is how to drive the hardware. Pretty much everything you need to know to drive a serial port or a pin is done for you with the Arduino IDE. With PIC you will have to study the data sheet to see how to drive the hardware you want to use. If you want something like the serial monitor you'll have to write your own. Tip: I always include 3 LEDs on my projects, I turn them on and off in the code I am having problems with to give me an indication of whether the code has got to a particular place or not. Another tip: if you want to use print in any form don't buy a PIC with 8k of flash memory; print uses almost all of it leaving little or no space for anything else.

I suggest you buy one of the 18F PICs, probably doesn't matter too much which one (I happen to like 18F26K22) and play with it. If you can get some LEDs flashing and read some buttons you are well on your way. There is a huge range of PICs, they differ in the built in peripherals they have, they are similar in that the basic architecture is always the same, once you learn how to use one you will find the others easy.

Any C code you have that doesn't interface to hardware will probably transfer easily. However, any code that does anything at all to the hardware will have to be re-written to drive the hardware in a PIC.

Have fun!

PerryBebbington:
I started with PICs and now use both PICs and Arduino. There's advantages to both, which you prefer is up to you. For PIC I use MPLAB X with MPLAB XC compilers. I also use a PICKit 3 but this has been superseded by PICKit 4 so if you are starting out with PIC probably get one of those. There are other development tools available but I've not tried them.

As others have commented, the thing you are going to have to learn is how to drive the hardware. Pretty much everything you need to know to drive a serial port or a pin is done for you with the Arduino IDE. With PIC you will have to study the data sheet to see how to drive the hardware you want to use. If you want something like the serial monitor you'll have to write your own. Tip: I always include 3 LEDs on my projects, I turn them on and off in the code I am having problems with to give me an indication of whether the code has got to a particular place or not. Another tip: if you want to use print in any form don't buy a PIC with 8k of flash memory; print uses almost all of it leaving little or no space for anything else.

I suggest you buy one of the 18F PICs, probably doesn't matter too much which one (I happen to like 18F26K22) and play with it. If you can get some LEDs flashing and read some buttons you are well on your way. There is a huge range of PICs, they differ in the built in peripherals they have, they are similar in that the basic architecture is always the same, once you learn how to use one you will find the others easy.

Any C code you have that doesn't interface to hardware will probably transfer easily. However, any code that does anything at all to the hardware will have to be re-written to drive the hardware in a PIC.

Have fun!

I bought a few PIC 16F18877's and a PICKit 4. I've been playing with those but havent started with creating hardware drivers. Still having trouble creating a delay function using a timer and interrupt. PIC makes me feel so dumb lol. Reading the datasheet shows me how much I dont know about MCU's.

castroaldrickDev:
I bought a few PIC 16F18877's and a PICKit 4. I've been playing with those but haven't started with creating hardware drivers. Still having trouble creating a delay function using a timer and interrupt.

If you have not created any hardware drivers (by which I mean anything at all that interfaces to the outside world, including the digital ports) how are you getting any kind of output to know what is happening?

castroaldrickDev:
PIC makes me feel so dumb lol. Reading the datasheet shows me how much I don't know about MCU's.

Don't be hard on yourself, we all had to start somewhere!

PerryBebbington:
If you have not created any hardware drivers (by which I mean anything at all that interfaces to the outside world, including the digital ports) how are you getting any kind of output to know what is happening?

Don't be hard on yourself, we all had to start somewhere!

Oh xD I meant I havent made anything that does communication in the form of UART, SPI, I2C without the use of a library.