Saturday, August 04, 2012

Arduino : thank you and goodbye

Arduino is a must-have these days.

 

It's a great microcontroller-based prototyping platform, coming into many flavors, with tons of open source projects, tutorials, forums etc. for anyone to start playing with embedded hardware. Using a simple IDE and C++ based code, a USB cable and a few passive components, it is possible to blink a LED in seconds or exchanging messages with a PC (well, a Mac too) in a few minutes, without any serious prior knowledge in electronics.

It's undoubtedly an excellent starter, but how far can you go with an Arduino ? well, pretty far, but up to a point because (as generally in life) there's always a tradeoff between simplicity and performance. It's up to the engineer/maker to decide if and when.

Personally, I decided to completely quit after some months climbing up the learning curve. Actually, Arduino itself pushed me into this decision. Below are the reasons, but credits first :

What is great

  • The Arduino IDE is based on AVRGCC. Learning Arduino eventually helps you to learn C++. If you dislike a specific higher-level Arduino command or library, you can always substitute it with a C++ equivalent (well, not always).
  • You can power, program and communicate with Arduino via a single USB cable (or an FTDI cable for some clones).
  • You can do something simple (and slow) in minutes with standard libraries, without worrying what lies beneath. Slow means reading buttons, displaying serial or LCD messages and rotating motors.
  • Serial and SPI communication is excellent and out-of-the-box.


What is awful

  • The Arduino IDE. It is the worst and most unusable editor after notepad.exe. In one day, you always switch to a decent external editor, however you still have to leave the IDE open in order to program the device.
  • The bootloader. In order to finalize an Arduino-based product, you have to manually burn it in every raw ATmega chip. This decreases the available program flash size by 2 KB no matter what.
  • Few choices : if you follow the official product line, you can choose between 32 30 and 254 KB of program size. What happens if your code is, say 42 KB ? The only choice is the semi-compatible Sanguino clone.
  • There's no easy way to change the clock frequency, is it ? The 3.3V/8 MHz model can go (safely) up to 12 MHz by the way.
  • digitalWrite() takes 56 cycles to execute (well, I measured 400). At least it is easy to find out why and switch to direct port access (the second thing to change, after the IDE). Generally, Arduino is not very friendly for time-efficient coding.
  • You cannot (at least, easily) disable the default hardware serial library, in order to take over the TX and RX interrupts, no matter if it has been started or not !
  • There's a overflow timer ISR firing every 16K clock cycles in the background. This is for servicing millis() and micros() functions, even when they're not used at all !
  • An empty Arduino project takes 466 bytes on an Arduino UNO and 666 bytes on an Arduino Mega 2560. I dislike overheads a lot. I also dislike seeing warnings during compiling of the above.
  • Last but not least - the Arduino environment undoubtedly 'hides' important aspects of microcontroller architecture : registers, interrupts and timers. LEARN THEM.
 
Now, is it time to switch to pure AVRGCC ? 
Well, there are pros and cons again :

The difficulties :

  • Need to rewrite some things in C++, most important of which is serial communications (maybe the best part of Arduino). Fortunately, there are lots of how-to's for that. Writing other protocols is much easier, e.g. SPI.
  • Learn in which libraries the non-compiling commands are included, and include these libraries in your cpp's. Most common are : avr/io.h, avr/interrupt.h, util/delay.h, stdio.h and string.h.
  • byte and boolean should become uint8_t and bool, byte numbers as B00010001 should become 0b00010001 and so forth. You'll find the rest changes, they are few.
  • Get more information also here, and don't give up.

The gains :

  • Need another processor for more space or power ? you get one (from the same series, i.e. megaAVR) and just recompile ! you may need to change some register and interrupt names, the makefile and fuses.
  • Need a different clock speed ? change the crystal or the resonator, or even use the internal oscillator by only changing the fuses and the makefile !
  • You can get an excellent or a turbo-charged IDE for your code. Read more in the next post.
  • You have total control on your code. What you write, it executes. Nothing's hidden, nothing happens without your consent. You have full power and responsibility. You write faster and more efficient code. You learn more.
      

    When you feel ready, go for it.

P.S. Get a good logic analyzer. It is the eyes of the blind.


UPDATE : Here are two sites with really excellent tutorials in order to implement the missing parts when upgrading from Arduino to pure AVRGCC :

9 comments:

  1. I like the article! Finally someone made the tests and got proof of what I've always suspected. Arduino is actually good for small prototying tests, but if you want to go for high performance and complex programs you must learn the raw stuff. That's why I've never got into -uino stuff. Take care!

    ReplyDelete
  2. Couldn't agree more. 8-bit microcontrollers are not a sensible place to run this sort of middleware.

    ReplyDelete
  3. It is hard to disagree with what you have stated above. However, the problem with Arduino is its projection by vendors and perception by users like us. What Arduino actually is , is a 'Software Platform'. Arduino is not a hardware platform as is always projected with that standard board layout. Arduino is just an IDE optimised for ATMega.

    However I do not foresee a Good Bye for Arduino in near future, as the program space on entry level micro-controllers is being ramped up at a pretty past pace. Earlier having 16K Pgm space was a luxury, now it is essential. So as with any market driven trends, I see the Arduino pushing the manufacturers to offer much more beefy MCU at entry level.

    ReplyDelete
  4. I totally agree with you about the 'software platform' label. However, I think that manufacturers already offer a vast range of MCU options for developers. So it's up to Arduino team to support more of them; why not ATtiny2313 and ATmega644 ? The only rumor to date is for an ARM-based 'Arduino Due'. But the latter is surely not entry level !

    ReplyDelete
  5. I had much the same gripes about the Arduino runtime a couple of years ago, and have been continuing to develop a feature rich, efficient runtime library that is capable of supporting most AVRs (various tidbits of info are pulled from AVR LibC, so the porting effort to a new microcontroller is simply populating a few tables in a script and regenerating the headers).

    In the latest refactoring, we now use template parameters to pass compile-time constants to classes (instead of runtime variables), which makes for extremely compact code.

    You can find out more at http://www.makehackvoid.com/project/MHVLib

    ReplyDelete
  6. if you like microcontrollers and everything related to it see this link

    http://all-about-embedded.blogspot.com/

    ReplyDelete
  7. I use following method to utilize the 2K flash space
    1. Coding & Compile with Arduino IDE
    2. Program the Hex file with AVRISP MKii + AVR Studio (Bootloader will be erased)

    To increase the IO speed, I use Port Registers
    http://www.arduino.cc/en/Reference/PortManipulation

    ReplyDelete
  8. If you want the option to get down to the bits and full control, mansos.net supports arduino.

    ReplyDelete
  9. Well, yesterday I installed Eclipse to get more power and a better IDE. Frankly, I was overwhelmed and I began to appreciate the simplification of the Arduino system. Yes, it has its problems, but I have been able to build, robots, audio thermometer, midi laser harp,.. I have been able to connect it to my TV, a ps2 keyboard, SD card, and much more. It may not be for everyone but it fills a large segment of hobbyists. Yes, there are a segment of projects that would benefit from a more efficient coding. However, engineering is about solving problems efficiently. Sometimes inefficient code that works is more efficient than spending time optimizing it. However if it is necessary you can write the ports directly even within the arduino environment. Of course, if you are serious about engineering, than you should go deeper and move on. Good luck.

    ReplyDelete