Honey, I Shrunk The Arduino Core

High-level programming languages do a great job of making a programmer’s job easier, but these languages often leave a lot of efficiency on the table as a compromise. While a common thought is to move into a lower-level language like assembly to improve on a program’s speed or memory use, there’s often a lot that can be done at the high level before resorting to such extremes. This, of course, is true of the Arduino platform as well, as [NerdRalph] demonstrates by shrinking the size of the Arduino core itself.

[NerdRalph] had noticed that the “blink” example program actually includes over 1 kB of extraneous code, and that more complicated programs include even more cruft. To combat this issue, he created ArduinoShrink, which seeks to make included libraries more modular and self-contained. It modifies some of the default registers and counters to use less memory and improve speed, and is also designed to improve interrupt latency as well by changing when the Arduino would otherwise disable interrupts.

While there are some limits to ArduinoShrink, such as needing to know specifics about the pins at compile time, for anyone writing programs for Arduinos that are memory-intensive or need improvements in timing, this could be a powerful new tool. If you’d prefer to go in the opposite direction to avoid ever having to learn C or assembly, though, you can always stick with running Python on your embedded devices.

69 thoughts on “Honey, I Shrunk The Arduino Core

    1. Most of all it isn’t the size reduction in Flash *but* a lower byte count for the code *is* also a lower runtime (in uSec).
      Lots of time its just that the “lower runtime” makes it possible (timing constraints) to do you project with that micro-controller.

  1. Arduino whether it is the Library, the text editor (no it’s not an IDE) or the AVR-based boards is just pure plain evil:
    – Badly written library code. Even if you despise modern languages like Rust and even Zig where embedded thingies are a first-class citizen, still C++’s template metaprograming can be used to make Wire.write generate 1-2 instructions instead of whole procedure spanning tens of instructions.
    – No debugger. Anyone who’s worked with MCUs professionally will tell you that not having a debugger is an utter joke.
    – No proper build system.
    – No jumping through symbols, no ‘find usages’, no renaming symbols through a project, no auto-formatting.

    1. The library issue is true, but I view the arduino ecosystem as “for beginners, by beginners”.

      And by that I mean it’s implemented in a way that makes people getting their debut with c++ not feel utterly overwhelmed by unessesary details more than c++ already does.

      That said it’s not something I’d recommend to anyone aspiring to learn c++ for real, but rather if coding is for them or they’re just gonna quit after 5 minutes, or if they wanna crank out projects without becoming a greybeard by reading each mcu’s, sensors or support IC’s datasheets so they repeat them verbatim.

      1. There’s a downside to that. Whenever I get to tutor students who come from an Arduino background, they seem to have no concept of what is a datasheet. If there’s not a tutorial online on how to use some specific part – with an Arduino – they just don’t know how to approach it, as if it’s dark magic. You can put the printout of the datasheet on the table and ask them a question, and they won’t pick it up. They google for a make magazine article or an instructable, and come back with answers from that.

        I think it’s learned helplessness. If it’s not provided for you, it doesn’t exist. The people who are naive to the subject tend to be better.

        1. “The Maker Movement” (ignoring the people who have always done things but jump on the bandwagon) seems to be about results. “I don’t want to understand things, I just want flashing LEDs for my bike so I can call it art”. So a lot of it is about making things bite size, and it’s not about beginnings going places, it’s a perpetual state of simplicity.

          I remember a post somewhere “wiring an LED is the hardware equivalent of ‘Hello.World'”, but that was a prelude to solderless LEDs. It forgot that neither is so useful in itself, but a starting point.

          We create this illusion that everyone should be doing “technology”, is doing it, but that hasn’t raised people up, it’s lowered standards.

          1. Guys, I do not share you opinion on Arduino. I think it is awesome because it is simple. I remember reading datasheets for 8051, 8255 and other parts in early 90-s. It just takes forever. With Arduino I can make some big project in an hour because someone already read the datasheets and wrote the libraries for me, which I highly appreciate.
            Also, I was teaching highschoolers. It works better if they can make something meaningful quick and see the value. That motivates them to dig dipper. When they see nothing but datasheets, only a few can stay motivated long.

          2. Yes, but we’re talking about what happens after that. People get stuck at a perpetual state of “beginner” because they are being SOLD pre-digested pre-thought projects, ideas, and solutions.

            The fundamental point of Arduino is to sell Arduino and all the paraphernelia, not to teach you electronics, programming, engineering, design… People are motivated, they are making stuff, but they are reduced to trivial “lego engineering” by “helpful” people who pander them into helplessness.

          3. I like to think about it like a young person who uses crayons to make art. They really enjoy it and decide to take an oil painting class. They likely know nothing about mixing theory, or brush types, or whatever. Maybe oil painting isn’t for them. Maybe they just want to draw. That doesn’t mean that standards are lowered for oil painting. There’s a place for people to do things at different levels.

          4. Sounds like the failure is not in Arduino, but in educators to fail to provide a path from Arduino to a more standard microcontroller ecosystem.
            It’s like someone has done you the favour of bringing you a case of beer, but you’re complaining that they’re not going to stay and pour it into glasses for you.

          5. Take 100 complete beginners and put the Arduino IDE and Adafruit walkthroughs in front of them, and take a 100 complete beginners and put a datasheet in front of them. Come back in five years and see who has progressed.

            I have a feeling you’re frustrated that you come across more people who stop at the surface level, because there are far more people at all levels now than otherwise.

          6. Mind, it’s not that there is some nefarious plan to keep people stupid. It’s just that those who fail at educating people and expanding their skillsets may succeed in attracting the same people back as customers, which is what keeps the company in the market and their competitors out of it. You make more money by being a little bit TOO helpful.

            Arduino is one of those things that, despite being understood as an educational tool, actually straddles the market between education and a “poor man’s PLC” for hobbyist and research use. It has a conflict of interest, where the company succeeds the more people are UNABLE to use anything else.

          7. > take a 100 complete beginners and put a datasheet in front of them

            I’m not talking about complete beginners, but people who already understand some concepts and have already tried some things. The more stuff they’ve done with Arduinos, the more they tend to stick with that, and the less they try to look beyond and figure out by themselves what they could be doing.

            To give a particular example, take controlling a servomotor. A person who hasn’t used Arduino much will approach the problem by looking up how a servomotor works. A person who HAS used Arduino extensively will first look up how to connect a servomotor to an Arduino. This results in a curious effect: the naive person actually leaps ahead of the Arduino person in understanding the task, rather than simply finding a particular solution to it. I’ve seen this happen, where the person who was supposed to be ahead because they already knew Arduino stuff actually made gross errors, like thinking that you can update the servomotor position faster than once per 20 ms, because they never bothered to read up about the servomotor itself.

            The Arduino established person orients themselves according to whether they can find a ready solution that accomplishes the task, not according to learning anything from it, like a gamer who first googles for a walk-through and a cheat-sheet than trying to play the game. They simply trust that someone else has done the thinking for them, which is why they may look at an example with a tiny micro-servo connected directly to an Arduino Uno, and then come asking why it doesn’t work when they try to connect a big power servo the same way. Well, you didn’t read the data sheet, so you didn’t notice that it needs more than an amp of current and now your Uno is toast.

          8. In other words, the question “How to control a servomotor” tends to get translated to “How to control a servomotor with an Arduino”. By giving the easy solution first: nail, meet hammer.

            It’s also why you teach arithmetic to children not by handing them a pocket calculator, but making them do sums with their fingers and on paper.

          9. >I have a feeling you’re frustrated that you come across more people who stop at the surface level

            This shouldn’t be the case for people at bachelor level who are trying to get a degree on the subject. There is something wrong if the people think they don’t need to go any further.

          10. >educators to fail to provide a path from Arduino to a more standard microcontroller ecosystem.

            Educators are being sold (or they misunderstand) Arduino as such. The more standard stuff would require more resources and teaching time in circuit design, PCB manufacturing, etc. so the Arduino is seen as a handy substitute for the basics – without realizing that it skips the basics.

            So you get people at a level where they should be able to whip up a basic strip board with an AVR/PIC to blink LEDs and drive stepper motors, buy a pile of Arduinos instead. They may have taken some courses in circuit theory etc. but they have no idea how to use a transistor, since they’ve never actually done it. All they’ve done is Arduino, with a bunch of plug-in kits and shields, wires pre-cut and terminated with connectors, no soldering, no theory, just connect the lines and color by numbers.

          11. It used to be fairly common for high schools(California) to have courses on electronics. Where they used to cover datasheets on logic chips but usually only some students got into programming pic chips. Sad they lost all that curriculum and resources. I went to the last meeting of the organization I was just a kid but it was sad they had only 5 members and all of them where at retirement age and many schools where not going to continue the classes. Then in 2003-4 some schools tried to create some new classes but it was mostly build this kit or program this specific robot and the teacher was only an interested party usually not their primary subject of knowledge.

          12. I joined a makerspace many years ago. I hated the culture of if its not on a PCB I dont know how to use it. It was so bad that some people where using 10’s of Arduino’s to control their projects instead of controlling there many inputs/outputs through MUX’s and when I suggested this being shunned off as a anti Arduino person. While its true I come from a PIC background I still agree Arduinos are great devices but a deeper understanding of electronics is a must.

        2. It depends entirely on the task you’re giving them.

          Are you asking them to make an LED blink? Then why go to the datasheet when there’s almost certainly a simple high level solution?

          Asking them to make a certain pin show a certain pattern on an oscilloscope? Then I could definitely see some utility in checking out the datasheet.

          1. Well, at least they should check the forward voltage and the rated current, and calculate the appropriate resistor value so they don’t break the LED or the MCU – that is, if they want to blink any other LED than what’s already on-board the Arduino. Otherwise it’s just an exercise in pressing the “upload” button in the editor, since the code example is written for you as well.

        3. The Arduino ecosystem seems to be almost defiantly arranged to prevent learning. We have an employee who spent years enthusiastically doing Arduino projects. I was shocked to learn that he had no idea how 2’s complement math works and how you can use rollover to your advantage when dealing with polar coordinates. If you want to look at the libraries to see how they work, they’re hidden nine folder levels down from the root of the dev system. If you want to know what overloaded versions of common functions are available, there is zero documentation about anything except the most basic 1 character methods. I had to guess that there must be a version of the TCP send function for the ESP32 that took a string instead of a char; it was faster for me to just write it and see if the compiler accepted it. (Every single example turned up by da goog used char. It was 1000x faster passing the string.) If you do find the libraries nothing is commented and many of them make use of arcane C++ techniques that are very hard to follow if you don’t use them all the time. And if you make the mistake of asking on the forums you get ridiculed for not being an expert and not reading the nonexistent docs.

          1. LocalRoger hit the nail on the head: (couldn’t resist)

            “There is zero documentation.”

            Look at the IBM FORTRAN manual from the early 70’s. It gave detailed information on even the most trivial of functions. At that time, FORTRAN couldn’t pass arrays of strings to a subroutine, but having this detailed information, how data are stored in memory and how they are addressed, I was able to do this. (yeah, I’m really eff’in’ old.) Another cool thing (but a nightmare to debug), was self modifying code. The point here is that the documentation was there.

            Also, in my day, we wrote a detailed description of what we were trying to achieve, how we were going to achieve it, then the documentation and the last thing we wrote was the code to match the docs. Yes, there were times when there were better ways to code something, but we went back and made the docs match.

            Now we get shit apps that are buggy as all get out and the only place to turn is forums because most authors have abandoned the code for other cool projects.

            We had a picture of a roll of toilet paper with the caption, “The job’s not done until the paperwork is complete.”

            Kids these days! They have no pride in their work. Just a bunch of hacks (def “cut with rough or heavy blows.”).

      2. >I view the arduino ecosystem as “for beginners, by beginners”.

        With the coding style and code quality from a beginner… That’s a reason why sometimes I have to specifically exclude Arduino search result from google.

      1. Not so sure about that, the success of PlatformIO suggests there is definitely interest in better tools. Even the Arduino folks are working on moving the IDE to be based on VSCode, which includes adding debugging.

    2. To be fair, Arduino was just a hijack of Wiring. It has all the same bugs a lifetime later.

      I know open source is meant to be forked, but I don’t get the ethics of a professor passing off his student’s work as his own.

    3. The cringe factor of looking at the official Arduino core code, knowing it’s probably being used by millions of people. was part of my motivation for writing something slightly better. There’s a bit of enjoyment from the retro factor of working with 8-bit MCUs like the AVR, but most of the time I prefer a low-end Cortex-M.

    4. This is old and not accurate:
      – Bad libraries: In any ecosystem library quality varies. Even in so called “pro” there are bugs. It’s an open source ecosystem, there is good and bad code, it’s the nature of open source. We also released arduino-lint to help catch common issues in arduino code. You can always release good libraries to replace the bad ones if the ones that are available don’t suit you :)
      – There is a debugger, Arduino IDE 2.0, and it works quite well. we’re still smoothing out a few issues but the tool is good.
      – We released a long time ago the arduino-cli which is a quite sophisticated tool which deals with the build process, it can be customised in a lot of different ways. It’s used by a lot of people even to run continuous integration on libraries etc. you should check it out.
      – Jumping through symbols etc. Again with Arduino 2.0 you get all of that. (BTW the Arduino IDE 1.0 has autoformatting)

      It’s ok to criticise Arduino but at least let’s look at the present not at what we did years ago.

      BTW The Arduino IDE 1.0 is downloaded 40 million times every year… you can say whatever you want but it’s the most successful embedded development tool ever made and helped millions of people to learn how to program microcontrollers.. It deserves a little bit of respect just for that :)

      1. This shows a surprising lack of understanding of software. ‘bad’ is not just a measure of bugs. Code size, complexity (by whatever metric), execution time, maintainability… All contribute to the bad.

        Saying ‘it works so it cannot be bad’ is incredibly naive.

      2. I will check out Arduino IDE 2.0. This sounds awesome. I started using Visual Studio 2019 with the Arduino extensions, and it was good, but something a little smaller is just the trick. I had been trying to get into embedded for years and years, and Arduino was the spark I needed. Having all the ugly stuff bootstrapped away was what did it, especially a pre-built toolchain and enough libraries / examples to do interesting work. I use more highfalutin’ stuff now (ARM, J-Link, etc.) but Arduino has its place for me, especially when doing “poor man’s PLC” as was mentioned above.

      3. Best solution would be to kill the Arduino IDE and switch to Eclipse. It provides all that Arduino 2.0 promises and more, and has done it for many years. It is the de facto standard, it is used by STM, TI, NXP and whatever so its good for young people that will use it in their career.
        No, it is not hard to use, it is easier because it does not hide things.
        Only thing that would be missed: a reference to Arduino during a job interview is a big red flag, many time it is associated with many bad programming habits and it is better to stay with someone who chose a proper IDE.
        And the look of surprise that turns to fury when you show step by step execution to people trying to learn a language on Arduino. And when you say them it is nothing new, it has been with us from the 80s and they are the victim of a sad joke.

      4. I will say the one thing Arduino gets right — possibly the only thing — is that it automates the installation process for the development toolchain, particularly if you are on Windows where it is less natural than on Linux. This is the reason I am using the fucking thing for my ESP32 projects. I would have favored NodeMCU Lua, but I had a commercial project to complete and it was still in beta. And being compiled, once you do figure it out Arduino gives you a lot more power.

        But to use this thing for programming actual Arduinos, with 8-bit processors? Fuck that.

      5. It has not helped people to learn how to program microcontrollers though, it has taught people how to program arduinos, in their various shapes and forms.

        They can hook up sensors with various protocols and not have to understand a single one of them, to them wires and digital signals are all just a black box, if you asked them how uart or i2c or spi works chances are they couldn’t tell you and wouldn’t even recognise them even though they have used them. All they are taught is you plug it in a certain way, you use a library and magically the Arduino talks to the sensor.

        Same with things like stepper motors and such, all they need to learn is that you hook up the wires and use a library, they probably don’t think about how the stepper motor works, why it has 4 wires, why it needs a driver, everything they know about it comes from an online tutorial that handed them the diagram and the code and it just works, if they want to modify it to do something else then they just look up another tutorial. If you asked them why it has to be those pins to connect that sensor they probably don’t have an answer other than the tutorial said to use those ones and they would get confused if two tutorials for two different sensors used the same pins and wouldn’t know what to do.

        Arduino is only useful for generating interest in people to learn about microcontrollers but it also traps people into only following tutorials and only using Arduino or Arduino IDE compatible boards. Arduinos themselves aren’t all that great anyway, they have very low specs and you can pick up more powerful boards cheaper.

        It is also very annoying looking for development boards and seeing lots with an Arduino Uno compatible pinout, because the people that buy these other boards probably don’t want to use an Arduino shield or anything from Arduino but it is such a common thing, and the people that use Arduino shields will only know how to use them with arduinos libraries anyway.

        Arduino isn’t all bad it just should be moved on from very quickly.

  2. It’s good for what it is and at its price, but it’s true, no serious programmer doing non-trivial things can live without a debug and trace system. The PIC is still where I go for this – not perfect, not a true ICE, but the price is right. I’m not really a very good programmer, but with a good debugger and the knowledge of how to use it, I can make almost anything work :)

  3. I do think our reliance on high level languages makes us indifferent to efficiency – execution time and size. But, the reality is that HLLs make it easier and faster to create programs – we trade off machine inefficiency for human efficiency.

    And, when your program needs more memory or faster execution, you can just move up to a faster processor with more memory. In the Arduino cosmos, you can go from a 16 MHz AVR to a 600 MHZ Teensy with just a recompile. Well, mostly just a recompile but it is usually a very small amount of effort. Less than $20.

    So, while I think it is important to be efficient with machine resources, it really doesn’t matter much with Arduinos.

    1. This is very true. There’s a time and place for efficiency with machine resources. Most people making electronics projects for fun will never need to worry about that. Many people online will constantly reiterate “Arduino is not for professionals!”, well yeah that’s obvious. No one ever claimed that. It’s just an easy way for people to get started, the people who are interested in going deeper will use better tools and learn more. Works out well for everyone. Making things more accessible isn’t a bad thing

      1. as you said it’s a quick accessible resource for the masses as a introduction and i’ve seen two main types in that eco system, those that will break out of the arduino ide and delve deeper into the build system and eventually lead elsewhere onto other chips and languages and those that dont need to,

  4. As someone who started out hardcoding machine language from the processor datasheets, and I’m talking about hexadecimal bytes typed into a debugger/editor into ram to be executed and later burned into EPROM, I welcome the fact that I don’t have to do that to blink a light or move a stepper motot.
    You can still do that if you want to, and maybe some should try that. Knowledge of registers and stacks and ports can teach you a lot about the inner-workings of processors. But you don’t have to. Most of my time programming is spent on the structure of the program, ease of changing parameters, and the user interface. All those thnigs are more important than knowing how to write to a port and make only one pin go high or low.

  5. The Arduino environment does exactly what it was intended to do. Make using microcontrollers easy. It has become a huge success because it meets a need. If you don’t like it or it doesn’t work for you…chose something else. It’s a big world with something for everyone.

    1. Not in the general sense. It makes using Arduinos easy, to the extent that they are easy to use. For many tasks involving microcontrolling, they actually stand in the way of doing it.

      1. The question is if removing the Arduino community would help people with tasks involving microcontrolling, or if they would never even start a project in the first place.

        1. My impression is that the arduino isn’t about programming, but doing away with solder.

          People don’t want to learn, they want results. No messy soldering with software. So there’s all this hardware off the shelf to make them do what you want. And reuse software to do it.

          When I was a kid, technical hobbies weren’t mainstream. We knew who was there because we weren’t common. If we were interested, we learned, and there was a limit on how much it could be simplified.

          Now we want everyone to be able to participate, but what does that give us?

          “Tech” has become something to use, rather than something to explore.

          1. So ? Different people have different goals. If someone likes tinkering in their garden, and uses an arduino for watering plants, they may not understand the details of microcontrollers, but that was never their goal. They are still achieving their goal of growing plants.

  6. Amazed at how jaded people can be argumenting from a professional standpoint about a learning tool for kids.
    Arduino *might* just not be your ecosystem? Can you stop invading it with your resentment? Yikes…

    1. I started with Arduino but quickly learned to use more tools, debuggers, ide’s or make my own build environment.
      basically I grew up :)

      Often I just don’t get what people are complaining about.
      Sometimes people want to tinker without all the hassle I can understand that and sometimes people want to really know what they are doing people are different and have different needs Arduino just caters to a need.

    2. My great, great grandmother Henrietta faced racism in 1853. She didn’t need the bar lowered, she needed racism to be eliminated. Probably sexism too.

      Lots of people have been left out because of race. Lack of money is also a factor.

      I can’t help but think some of this mantra about how everyone should be able to do things is skewing because some have long been left out. But then it becomes patronizing, Natives or Blacks or women need the entry lowered, as if they were incapable of some things?

      Some people aren’t destined for some things. I have zero interest or skill in sports. Why should those people who hated me get easy access to a technical hobby? I didn’t want to play sports in school, I wanted out of it.

      Making things easy just dumbs things down, it doesn’t raise things up.

    1. Yeah, I noticed STM32duino has even more cruft than the AVR core. Blink on the STM32F030 takes about 8kB, so half the flash is gone on a F4P6. I have thought about writing a small STM32 core, using LibOpenCM3 as the HAL…

  7. For The most part I think you missed the point of arduino. It’s not for the hardcore coder , it’s for the hobbiest. Being mode mechanical engineer I never learn how to code except in Fortran. But I’m using one now to control a multi speed transmission. Is it the best use of a microprocessor probably not but it’s cheap and easy. I can read data sheet and figure out the rest of the circuitry So for me it’s perfect.

  8. Semantics. Learning to program an MCU from scratch or assembler is about “as useful as doing log base 10 in your brain. Fun in concept, a waste of intelligence in reality”. And that was a quote of my math Prof. In the end the result is the same and no one will be able to tell. If you need the extra performance so much you are free to do whatever you want, but it is irrelevant for the youth. Coding in a high level language is a more useful skill than being an assembler wizard.

  9. I know, right? It’s not fair! I’ve spent years and years moving bits around one operation at a time to make LEDs blink, and now a kid in kindergarten can do it in about three minutes.

Leave a Reply to Michael BlackCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.