Arduino C legacy, libraries and codebender

Arduino did an excellent job hiding the inherent complexity of compiling code for embedded microcontrollers (or lately microprocessors). This allowed anyone to easily program their boards without needing to understand how code and libraries get compiled to the blob of machine code that gets its way into your board to do ‘stuff’. Heck, you don’t even need to know what a microcontroller is anymore!

The downside is that eventually, as you realize more and more complex ideas or try to squeeze more functionality into your code, you eventually hit cryptic compiler errors or unexpected behavior that surfaces from the archaic C/C++ world living underneath. In this post I’ll try to give you some insight into what happens under the hood, without bothering you with any of the details computer science students will get to learn in their classes.

Brains! I want brains!

As you probably already know the brains of each Arduino board is a microcontroller. We like to see it as ‘brains’, but in reality it’s really… dumb. As the common saying goes, if we were to ask a microcontroller to ‘jump off a cliff’, it would. And not at all because it’s loyal or just plainly submissive; it really doesn’t know how to do anything other than what it is programmed for. A microcontroller without a program simply doesn’t do anything. The program that gets into the microcontroller manipulates electric signals in such a way that it controls the flow and results of the next operation, each one paving the way for some useful function or an output that can be understood by humans.

While this introduction is quite generic, there’s no reason to go into the details. All you need to grasp is that a microcontroller only responds to (and with) electric signals; the zeros and ones that (you already know) are called bits. We group bits into bytes that we represent by numbers and letters to create machine code that the microcontroller understands, but I bet you, doesn’t make any sense to us. Just look at the next perfectly valid program for a microcontroller:


Neat, right? This is the infamous “Blink” example, compiled to a representation that can be uploaded (flashed) to an Uno and blink a LED! And all this came out of this (pretty much) straightforward code:

void setup() {                
  pinMode(13, OUTPUT);     

void loop() {
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);

The (humanly readable) code above, tells the microcontroller to set up output 13 as digital, set it to HIGH in order to allow current to pass through a LED that is connected to it, causing it to light up. Then we tell it to wait for exactly 1000ms (or better put, a second), cut off the current flow on output 13 (shutting off the LED) and then wait another second, repeating the process ad infinitum. If a microcontroller had real brains, all you would need to tell it would be to, well, blink the led on port 13! But you have to be explicit because it really doesn’t get it. All this should make you wonder, if it isn’t smart enough to know how to blink a LED, does it really know the concept of time or how to delay a second? Not really. Despite that, you may have seen that we used delay() as if the microprocessor knew what to do with it. Let’s see how that happens.

A perfect dance

Every time you compile, a perfect dance is performed. The compiler takes your code and tries to figure out how to compile it to an intermediate object that does what the code requests. You didn’t specify the details of how delay(1000) actually causes the microprocessor to delay one second, because some person has already done all the hard work for you. The compiler can figure out that at some library somewhere, the code for delay() exists (and that’s because it exists in a library that is automatically included with your code). When everything is compiled, the linker, working en pair with the compiler, figures out how to link the parts of your code with the missing parts like delay(), by adding the implementation of delay() from the library. The end result is some compact machine code that does what you commanded.

There are some libraries that cover basic stuff and are so necessary that they are always included and compiled together with your code. These are called system libraries. This doesn’t mean they will also be linked into your machine code if they are not needed. The linker is ’smart’ enough to not bloat your machine code with unnecessary parts. System libraries include basic Arduino functions like serial output, setting of outputs, reading analog values out of pins, etc. Remember digitalWrite() before? That’s also on a system library.

Where’s my library card?

Quite often, you need to do things that are more complex than basic Arduino functions, like talking to a sensor. Fortunately in the Arduino world, everything is almost certainly already done for you in a library. You only need to include that library and manipulate the functions it provides to your benefit. In codebender you’ll find a ton of (or better put, more than 500) libraries that are already installed for you, ready to be included in your code. You don’t have to go looking, downloading, installing, figuring out incompatibilities and going through all the hassle that comes with it. Rejoice!

When you get to actually include a library in your code, some elements of that C/C++ legacy start to reappear. You probably have seen that #include statements add a library in your code. You maybe have even noticed that sometimes they are used like #include <MyLibrary.h> and others like #include "MyLibrary.h". It looks like the same thing and while for simple cases it might work the same, there are some subtle differences.

To #include <> or “”, that’s the question

The #include <> notation tells the compiler to go looking for the mentioned library outside of your code. It might find what is missing in the system libraries or in the codebender supplied libraries. The fun part is that if it doesn’t find what you specified and also no function is unknown anywhere in your code, nothing bad will happen! The compiler won’t care and the linker won’t find any missing links, allowing the compilation to succeed.

On the other hand, when you use the #include "" notation, you tell the compiler that it should look for a file with that exact name included in your sketch, together with your ino file. In codebender, if you include a file like that and it’s missing, the compiler will complain and abort the compilation because it feels that it is important to you, and it shouldn’t continue without it (despite being eventualy used or not). There’s also one more thing to remember. If you specify the name of a file that doesn’t exist in your sketch, but exists in a system or codebender library, the compiler will look there and use that one!

This is getting personal

Let’s see now what happens with another really useful feature of codebender, the ability to add personal libraries. A personal library is used in all your sketches together with system or codebender libraries. You can include it with <> as with any other library. The really useful part is that it also allows you to mask a codebender library with your own, perhaps because you want to use an older, or modified version.

Excuse me please, I have a request

And since we’re on the topic of libraries, if you’d like to see a library that you use often as part of codebender libraries, make a request for it. We thoroughly test each library before adding it to make sure it doesn’t break existing sketches. For updating a library, it means we compile each sketch with the library we already have, then with the library we’re about to add, times the number of common Arduino boards. With more than 100.000 unique sketches in codebender, that’s a lot of compiles just to make sure your sketches keep compiling! This causes us to not be as fast to clear the library request queue as we’d like, but we’re working on a new system that will make things much faster.


  1. Douglas Maughan

    05 Jan 2016 12:41:47

    I’m new to CodeBender. Is this the place to ask questions about the overall development process of sketches? I have used the Arduino Windows-based IDE and have sketches stored in DropBox. How might I get these sketches moved into a Personal Library?

    • Giannis

      25 Jan 2016 16:19:11

      Not really 🙂

      Would you like to send us an email at support?

  2. Dale Frye

    11 Feb 2016 06:46:54

    Any update on support for Arduino 1.6?

    • Fay Candiliari

      12 Feb 2016 17:00:46

      Hi Dale! We’re working on supporting Arduino 1.6 but it will take some time!

  3. jessey

    01 Dec 2016 04:57:24

    why am I loosing all my sketches when codebender said they’ll be available till after Christmas sometime? I’m chocked!!!

Comments are closed.

Stay tuned:

* we promise that we won´t spam you, never.