Arduino Tool Chain Overview
Free Arduino Course
Today we’re going to talk about how the Arduino tool chain converts instructions you type into the text editor into machine language the microcontroller can understand.
A tool chain is simply a set of software tools that work together to complete a task. for example, if we were going to hang a picture, the tool chain might include a hammer, maybe a tape measure and some nails.
Arduino Hardware/Software Interface
When programming the Arduino (or anything else), it is possible to write some pretty complex instructions and get the Arduino to do some really cool things.
The problem is that a microcontroller (like the ATmega328 on the Uno) can only execute simple, low-level instructions. These simple instructions include things like add variable a to variable b or take variable b and put it in register x.
And, to complicate matters, microcontrollers only speak in binary. For those who don’t know, binary numbers are long strings of 1s and 0s. If you need to brush up on binary numbers, see A Bit of Fun with Binary Number Basics.
Figure 1: Arduino and other microcontrollers only understand binary.
The statement here may make total sense to you if you’ve been working with Arduino for a while. Or, if not, it may not make any sense at all. Either way, just go with me here for a minute. This is just an illustration which does not require complete understanding of the code.
Int Temp = analogRead(sensorPin); If (Temp > 500) { //more complex instructions go here }
How does a complex statement like this one actually run on a microcontroller that only knows basic arithmetic, logic, moving and shifting? How do these statements translate into the ones and zeros that the microcontroller can actually understand?
Glad you asked. No, really, I am. This is what we’re going to answer now.
Microcontrollers are electronic devices, so to speak to one you need to send electronic signals, as in figure 1. That’s where binary numbers come into play. A zero represents low or off, usually 0 V or close to it, while a one represents high or on, usually either about 5 V or 3.3 V, depending on the processor.
Become the Maker you were born to be. Try Arduino Academy for FREE!
A good way to think of binary numbers and digital signals is like a single pole light switch. The light is either on or its off, there is nothing in between. Zero is off and one is on.
Figure 2: a good way to think about digital signals.
Going from complex instructions to simple ones the microcontroller understands requires several layers of software that translate the high-level operations into simpler instructions.
How the Arduino Compiler Works
Enter the compiler. Compiling a program in Arduino is referred to as verifying. The terms mean the same thing, so throughout these tutorials we’ll use them interchangeably.
The compiler first transforms the code you write into assembly language. The name of the compiler we’ll be using on our Uno is AVR-GCC. If you’re new to this, that may sound kind of weird but try not to obsess over it. It’s just a name.
The assembler, which come with the IDE with the compiler, then translates the assembly language program into machine language. It then creates object files, which combine machine language, data, and information it needs to place instructions properly in memory. Often, the assembler creates multiple files which will eventually be put together.
This is where the linker — another part of the compiler software package — shines. The linker will take all the independently assembled machine language programs and object files and put them together. This produces a .hex file that the microprocessor can understand and run.
The two figures below, though they apply to C/C++ programming in general, are a good illustration of this process.
Figure 3: Arduino code compilation process. The figure below is another way to view it.
Figure 4: another way to visualize the Arduino code compilation process.
Another piece of software, called AVR Dude (for Downloader UploaDEr) starts when we press the upload button. This software sends the .hex file to the ATMega328 on the Arduino board. On the chip resides the bootloader. This bootloader was put there on purpose by the folks at Arduino and works with AVR Dude to get the .hex into the flash memory on the chip.
All of this happens very quickly and seamlessly behind the scenes of the Arduino IDE.
Figure 5: how the compiled code transfers to the Arduino board.
Programming in C vs Arduino
A few words are in order on this subject due to the enormous popularity of Arduino boards and the C/C++ language in general.
Some of you use stand-alone or naked microcontrollers for your projects. After all, this is how things are actually made and mass-produced in the real world. And it’s cheaper. We talked about naked or stand-alone microcontrollers vs Arduino in an earlier tutorial.
Others use platforms or ecosystems such as the Arduino almost (or entirely) exclusively.
Finally, some of you may use both depending on your goals and background.
For hobbyists, the number of people who use platforms like Arduino has exceeded those who only use naked microcontrollers.
When we talk about programming the Arduino, we’ll talk about the C/C++ languages. The truth is, sketches are written in a language similar to C, though a sketch itself is not completely compatible with C.
In Arduino, the main() function is hidden from view and added for you when you compile or “verify” your sketch. Also, there are two functions which the Arduino ecosystem absolutely requires: setup() and loop(). The only function C requires is main().
C also lacks built-in functions for using microcontroller I/O such as digitalWrite().
To make learning simple, the Arduino IDE designers hide a lot of detail and functionality behind layers of abstraction, many of which come in the form of libraries. Note that the C programming language also uses libraries. The linker adds them during the linking process.
Though there are a few slight differences, if you can become a competent C programmer, you’ll also master the Arduino IDE and crush it on your Arduino projects. And if you desire to port the code from Arduino to a naked micro it will be a cinch.
Now go create something!
Become the Maker you were born to be. Try Arduino Academy for FREE!
Thanks for the info. I am building a 3 tier robot and plan on using arduino. I am a qbasic and assembly programmer and electronic tech. In the process of learning early Arduino programming.
That’s awesome Mark! Love to hear more about your project as you progress!
Arduino is one of my favorite open source hardware hacking platform for hobbyists, designers, and artists? Arduino gonna be a big thing in the future. CircuitCrush I want a little favor from your end I am looking for best arduino tutorials online over the web. I try to find it but I am little confused which one is good for professional level. Can you help me out. Link is in the website coloum. Do reply and please help me.
You’ve found them!