Archive for December, 2010

Why you don’t need your Arduino and how to use alternatives

First of all, don’t interpret this as some kind of anti-Arduino rant. I think the Arduino is a handy tool and, I think it’s great for introducing hoards of people to something they enjoy. I am not writing this because I don’t like the Arduino; I am writing this because there is no solution for every problem. Speed, flexibility, and cost would be the primary reasons not to use Arduino. There are times when you will need more than 8-bit microcontroller or a different peripheral set. If you are looking at production, even if the ATmega series does meet your needs in those areas, you may want something cheaper. The ATmega has its high points but as far as 8-bit micros go, price is not one of them.

I think that most Arduino users realize that the hardware is nothing particularly special. The part of Arduino that is usually lauded is the software environment. I have heard comments such as “It’s a lot like C but easier” or “It’s an easy to use language based on C”. Well, I have news for you. All of you have been HOODWINKED! It is not like C; it is not similar to C; IT IS C. Well, it’s actually even harder than C; it’s C++, which is a bit odd for the 8-bit space. The Arduino IDE is not even a preprocessor. When you compile an Arduino program, your code is taken completely unmodified and compiled directly with AVR-GCC (the open source AVR compiler). I know what you’re thinking; it’s like the Twilight Zone. Here you’ve been, shaking in your Nikes at the thought of the vast and scary world of big-boy microcontrollers, not knowing that the whole time you were already in it. You have already been using the standard open-source AVR tool suite, you just displaced AVR studio with the Arduino IDE. There is a tutorial about how to compile your project outside if the Arduino IDE on their website here.

So why does anyone care about Arduino? The magic of Arduino is in the library that comes with it. It is a set of source files that is included with your project when it is compiled. You are free to look at those files, they can be found inside the Arduino directory under hardware\arduino\cores\arduino. Essentially, these files contain a set of functions that hide special function register access from you. SFRs are locations in memory that do things in addition to just storing data. For example, the SFR register assigned to an I/O port would make the pins go high or low when you wrote to it. That’s it; writing to registers is everything that separates you from programming for any microcontroller you want.

Guess what else? The Arduino library, being high level and all, could just as easily be written for any other microcontroller. That means, assuming you didn’t access registers directly, that all of your existing Arduino code can be used as-is on any microcontroller you want. All you have to do is write the functions you used for the new platform. For example, on a PIC, the digitalWrite function might look something like this:

void digitalWrite(unsigned char pin, unsigned char val)
{
   if (pin < 8)
   {
     if (val)
         LATA |= (1 << pin);
     else
        LATA &= ~(1 << pin);
    }
}

There are times when you should think about accessing the SFRs yourself, even if you don’t need a different platform. Take, for example, this line of code that sets pin 1 of ATmega port D high:

PORTD |= 2;

Literally, that line includes at least three instructions: read the value of PORTD, do a logical OR with the result and the value 2 (10 in binary), then write that result back to the port. However, AVR-GCC knows what you want and it will compile that into a single instruction that takes two clock cycles to execute. You can learn something about that here. Now, let’s consider the Arduino alternative:

digitalWrite(1,1);

Assuming AVR-GCC does not automatically inline the digitalWrite function (it is not declared inline), that will take about 16 cycles. The Arduino library was written for usability and development time, not for performance. In this case, accessing the register directly results in a 8x performance improvement.

If you are already using Arduino but you decide you want to be able to program microcontrollers in C, you will need to learn two things, neither of which should take you very long. The first thing is some additional semantics of C. You’ll need some basic knowledge of includes, defines, etc. I think this tutorial is very good but there are many. The second thing is how to handle bit manipulation in C. For example, you’ll want to know how to set the fourth bit of a byte to 1. You can find a tutorial on that here.

Don’t throw out your Arduino. It’s a good thing to have. On the other hand, if you haven’t branched out, you are like a tourist who travels all the way to China just to see the airport. Sure, the airport is great but now that you’ve come all that way, you might as well step outside.