Vassilis K. Papanikolaou
Arduino is a must-have these days.
Arduino 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 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.