1
votes

I'm working on a Mega 2560 board. I plan to move my project to a custom PCB using a bare ATmega2560 microcontroller (same as the one on Arduino). From what I have researched, there is no clear answer to my question.

Can I program the Arduino with the Arduino IDE and later port all this code to my own PCB? I see that the Arduino IDE creates .cpp files corresponding to all the sketches.

I understand that I would have to keep all referenced Arduino pins and peripherals from the Eagle schematics.

EDIT: As pointed out in the answers below, you can use Arduino code on your own PCB, provided you don't use any third party libraries. You can use the hex files the Arduino IDE generates to program the microcontroller on the PCB via Atmel Studio or using Arduino as ISP.

3

3 Answers

1
votes

Keep several things such as used GPIO, crystal, EEPROM (if present) and maybe service LEDs on your PCB like they were done in arduino and everything will be working. But I recommend to move to your own libraries and some free ones. You'll be able to detach from Arduino platform and use other Atmel microcontollers (for bigger or smaller purposes).

Answer: It will work if you move some valuable hardware (GPIO, SPI, crystal oscillator) to your PCB.

1
votes

It is definitely possible to flash Arduino code into your own creations. I have done this several times. However you need an ISP to flash the target.

Depending on the hardware of your PCB some libraries may not work or not deliver the desired results.

Everything that relies on exact timing requires a crystal oscillator. (However the newer Arduinos only have a resonator, thus this might not be an issue for you).

Everything that needs to run at 16 Mhz requires a crystall as the internal PLL only supports up to 8 Mhz.

Everything that requires serial IO must be compiled with matching frequencies.

--> Unless you know what you do it is recommended to have a 16 Mhz crystall oscilator.

The serial to USB converter is purely optional but if you ommit it you will not be able to use the IDE's serial monitor or the boot loader option.

In all cases you have to ensure that you set the fuses properly. If you flash a bootloader with the IDE the IDE will automatically set the fuses properly. Since you need an ISP anyway you might want to consider to remove the bootloader afterwards. But this is mostly a matter of personal preferences.

0
votes

If you dont use Arduino librairies and you stay on the same atmega model then the general code should work. You might have to adapt a couple of things tho depending on what your pcb has. The important is that you keep it on Atmega and you dont use Arduino's librairies. But it might be possible to export those? I'm not sure about that.