Sketch code size of nano VS native 'C' code size for a smaller MCU

Hi,

I use arduino nano (with sketch) for prototyping.
However the final target chip may be one of the smaller attiny or even PIC which does meet the h/w requirements.

However, the software size (flash size) needed is not clear. How much does the code footprint reduce when a program compiled for Nano using sketch is converted to non-sketch native 'C' code using a smaller MCU like attiny2313 or something similar?

Thanks and Regards,
WI

First of all, code size will probably not reduce when switching to a smaller chip. That is, when switching to another 8-bit. Heck, it might even grow because of the less capable instruction set of the smaller chip. If's a different story if you switch to a more powerful chip but that one probably comes with more flash anyway. And when you switch architecture everything can happen.

But what do you mean by "native 'C' code"? Arduino is normal C/C++ code only it uses a pretty user friendly HAL. Do you mean without HAL and just write to all registers yourself?

Two things about that:

  • First, that would give you non-portable code so it makes no sense whatsoever to prototype with a Nano (or a very different chip than the target chip that is).
  • Second, it depends which HAL functions you mimic in your code and how good your implementation is compared to the Arduino HAL. And remember, the linker is pretty good in not including stuff you don't use. So if you don't use parts of the HAL it's not assembled into your code. But granted, before setup() is called the HAL already does some basic setup for the chip which makes it include that.

Overhead for Arduino sketch is a few hundred bytes (to implement things like the timers for millis() and initialize the timers to work with analogWrite()), and a hundred or so each for digitalRead/digitalWrite/pinMode if you use those in your code (for the arduino pin to port and bit lookup). As noted above, the compiled size of your code itself may grow a bit moving from a mega to a tiny, because the tinys have a slightly different (and more limited) instruction set (no hardware multiply instruction).

Counter intuitive, but good. Thanks.

Worth noting two more things:

  1. you can program ATTiny parts with Arduino (using my ATTinyCore for classic AVRs, megaTinyCore for the new ATTiny parts - both are on the board manager json file listed in my sig.

  2. The new megaAVR attiny's might be a good thing to look at if you're productizing something and so need it to be cheap and/or small - the bare chips are really cheap for what you get. The peripherals are different, but there is remarkable consistency across the product line (even moreso than the classic AVRs). As of recently, you can also use them all with the Arduino IDE via my megaTinyCore. The megaAVR attiny parts also have the full instruction set too, complete with hardware multiply.

If you port to another AVR and install the needed core, you can test this yourself.