Microcontroller fun

I bought an Arduino about a year ago (1 Nov 2009 according to my email) and I’ve made some LEDs blink, bought some other bits and driven LCDs, logged GPS coordinates, and displayed the temperature. The Arduino is a nice platform and makes programming the microcontroller nice and easy by abstracting away the ugly details.

The reality is you’re programming in C and linking against a main.cpp that looks like:

#include <WProgram.h>

int main(void)


	for (;;)

	return 0;

And there’s a lot more code you link against to give nice setup functions and Digital Read/Write and Analog Read/Write. All of this is hidden away, and until you start adding libraries for software serial ports or interchip protocols like I2C and SPI you wouldn’t even see a #include.

Here’s the blink example from the Arduino distribution:

int ledPin =  13;    // LED connected to digital pin 13

// The setup() method runs once, when the sketch starts
void setup()   {
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);

// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);                  // wait for a second

As a contrast, here’s basically the same thing setup to run on an ATMEL ATMEGA328P using the internal oscillator in plain C. Harder to understand than the above example if you’re not a C programmer, especially since it uses some bit shifting and bitwise operators.

#define F_CPU 1000000  // by default we have the 1MHz internal oscillator

#include <avr/io.h>      // this contains all the IO port definitions
#include <util/delay.h>

int main(void) {
  DDRB = (1 << 5);  // set PORTB5 to an output

  while (1) {
    PORTB |= 1 << 5;  // turn LED on
    PORTB &= ~(1 << 5);  // turn LED on

About the same number of lines, but actually a lot less code, because once you start digging in you find the PinMode and DigitalWrite functions do a bit of work to make everything simple. As such, you’ll find plenty of forum posts suggesting that when you outgrow Arduino the next step is just straight up programming of the microcontroller. Perhaps sound advice, and getting a better understanding of the underlying concepts is probably wise.

The personal computer space has shown when you abstract the underlying system and it becomes slow you simply make the underlying system faster. Well, why not do the same. There’s a few projects out there doing just that.

Netduino was the first I ran across. Upgrade from the Arduino’s limited IDE to VisualStudio and use the .NET Micro Framework to blink your LEDs. You get an ARM processor and can program using C#. I just found out about the FEZ products, seems to be about the same idea, they have a few more options (although a Netduino Plus with Ethernet and SD or MicroSD is real close). The FEZ line has a little faster clock speed than the Netduino though.

microBuilder.eu has some ARM Cortex M3 reference boards, a big jump in CPU power and a good library built up to support it. Writing the program to the dev board is as simple as pressing a button and then copying a file to a drive, just like a USB flash drive. I have a couple of these and hope to play with them a bit more soon.

However, all this power does have a downside. The ARM CPUs on these dev boards is surface mount, making it more difficult to build your own board for your final project. You must either embed the entire dev board or design your own. Or use the low end Atmel chips, TI MSP430s and PICs which can fit on some perfboard with through-hole components. Warm up your C skills and you can still do a lot with these chips.

Comments are closed.