Skip to content


MCU Programming Intro Series #1: SOS on the Arduino

mcu_intro_1_1

In this article the Wiretrip Hardhack Team does a take on the getting started with Microcontroller projects theme started in this first article. This article focuses on the Arduino Duemilanove. Future articles will focus on other platforms. The Wiretrip Hardhack Team provide instructions and example code breakdowns to show how easy development is with this platform.

During our amateur travels with electronics, we thought it would be interesting to document some our introductory-level experiments on new MCU (microcontroller) platforms. Whenever we get our hands on a new platform, we take the time to do some rudimentary level programming just to get a feel for the platform and the associated development environment. This is the first in a multi-part series that are going to explore different hardware MCU platforms and programming environments.

The programming goal for all of the platforms is a slightly more complex twist on the usual ‘hello world’ hardware hacking project: blinking a LED. In our case, we want to specifically blink out an SOS pattern(i.e. three short blinks, three long blinks, three short blinks, pause, repeat). We like the idea of the SOS pattern, because it requires an explicit control of the LED based on different timing characteristics. Also, while there are tons of ‘blink the LED’ tutorials for most platforms on the Internet, there are few ‘blink the LED in SOS pattern’ tutorials…so it’s not a matter of a simple copy/paste job from someone else’s prior work. And there is a certain useful utility to blinking out an SOS: you can use it in your own code to indicate (in overly fancy fashion) when your board experiences a fatal error. Anyway, on to the walk through.

mcu_intro_1_1 Our first platform is the Arduino Duemilanove, a highly popular and easy to use microcontroller development platform based on the ATmega168. Despite being based on an ATmega MCU, Arduino uses a special C-esque programming language (based on the Wiring language) rather than the usual AVR programming language. The Arduino also uses a programming IDE based on Processing, rather than the usual suite of AVR tools. The main Arduino home site is at www.arduino.cc. There are many Arduino derivatives and offshoots, such as the Freeduino, Lilypad, etc. Overall, Arduino is an open platform that is easy to adapt and extend into many forms, while keeping a few common key characteristics (supporting the Arduino programming language, utilizing USB connectivity for programming, etc.).

Interfacing with an Arduino Duemilanove is relatively simple: you plug it into your computer via a USB cable, install the USB driver, and it shows up as a serial (COM) port on your system. The Arduino programming IDE interfaces with the Arduino via the COM port. Arduino calls the firmware programs you write a ‘sketch'; a sketch is typically composed of some setup code (in a function called setup()) and the main application code which repeats in an infinite loop (contained in a function called loop()). For those familiar with C, the implied main() code (which you do not see/manipulate) is:

void main( void ) {
	setup();
	while(1)
	loop();
}

MCUs often come equipped with GPIO (general purpose input/output) pins that are used to control electronic circuits. For output-based digital GPIO pins, the MCU generates either a high or low electric current on the pin (i.e. a digital one or digital zero). You can connect and control an appropriate LED via GPIO; setting the GPIO output to high causes the LED to light, while setting the GPIO output to low causes the LED to turn off. In an Arduino, you can control the output of a GPIO pin with the digitalWrite() function.

The standard Arduino Duemilanove comes equipped with a LED connected to GPIO pin 13; thus a basic Arduino has all of the hardware necessary for our SOS blinking needs. All we need to do is create our custom sketch (software) to blink the LED in the right pattern.mcu_intro_1_2 

If you haven’t done it already, connect your Arduino Duemilanove to your computer with the USB cable. The Arduino uses power from the USB port to power itself, so no external power supplies are required (although you can optionally use one if necessary). Windows will want to install a USB device driver for the Arduino the first time. The driver is included in the free Arduino IDE download, in the ‘drivers/’ folder (be sure to unzip the downloaded .zip file to some suitable location first). When prompted by the driver installation dialog, select the ‘specify an exact location’ option and browse to this folder. Windows will find the right drivers and install a USB-based virtual serial (COM) port. Detailed steps of this process are available at http://arduino.cc/en/Guide/Windows.

Now start the Arduino IDE (just double-click the arduino.exe file in the directory you previously unzipped). The Arduino IDE is fairly minimal, but that also makes it easy to navigate and use. You must make sure you configure the IDE to the right COM port and Arduino device you are using. You’ll find these configuration options via the Tools -> Serial Port and the Tools -> Board menu options. Note: if you have an Arduino Duemilanove and don’t see it listed in the IDE’s board selection list, select the Arduino Diecimila board model.

The IDE ships with many example sketches for use—and it includes a basic ‘blink the LED’ sketch. Let’s go ahead and load that sketch by going to File -> Sketchbook -> Examples -> Digital -> Blink.

mcu_intro_1_3 

You will see some basic code in the setup() and loop() functions. First we see that a variable ‘ledPin’ is created and assigned the value 13, which is the GPIO pin number that contains the LED. This variable is created purely for convenience and to enhance readability of the code (i.e. it is easier to know what ‘ledPin’ means than the plain number ’13’). In the setup() function, the pinMode() function is used to specify that GPIO pin 13 is used in output mode. Then the loop() function uses digitalWrite() to set GPIO pin 13 to a high state (i.e. send electricity on the pin and thus turn on the LED), wait for a little bit via the delay() function, then use digitalWrite() again to set GPIO pin 13 to a low state (i.e. stop sending electricity and make the LED turn off). This process is repeated in an indefinite loop.

Just for fun, let’s go ahead and run this example sketch as-is. Click the ‘verify’ button (mcu_intro_1_4 ) to compile and check that the code is valid, and then click the ‘upload’ button (mcu_intro_1_5 ) to have it uploaded to the Arduino and executed. Within moments, you should see the LED blinking. So far, so good.

Now we need to adapt the code to blink the specific SOS pattern. Basically we want three short blinks, followed by three long blinks, followed by another set of three short blinks, and then a noticeable pause. We could code this up as a sequential series of 18 calls to digitalWrite() with associated delay()s, but that seems excessive. Let’s use a simple loop, like so:

for(int i=0; i<3; i++){			// do the following sequence three times
	digitalWrite(ledPin, HIGH);	// turn on the LED
	delay(300);			// leave LED on for.3 seconds – short blink
	digitalWrite(ledPin, LOW);	// turn off the LED
	delay(200);			// leave LED off for .2 seconds
}
for(int i=0; i<3; i++){			// do the following sequence three times
	digitalWrite(ledPin, HIGH);	// turn on LED
	delay(700);			// leave LED on for .7 seconds – long blink
	digitalWrite(ledPin, LOW);	// turn off the LED
	delay(200);			// leave LED off for .2 seconds
}
for(int i=0; i<3; i++){			// do the following sequence three times
	digitalWrite(ledPin, HIGH);	// turn on the LED
	delay(300);			// leave the LED on for .3 seconds – short blink
	digitalWrite(ledPin, LOW);	// turn off the LED
	delay(200);			// leave the LED off for .2 seconds
}

This is a little better, but is still pretty redundant because each loop is still doing essentially the same thing (turning the LED on for a period of time, then turning it off for a period of time). We can further reduce the size of the code by making a custom ‘tripleBlink()’ function, which lets you specify how long the LED should be on and how long it should be off:

void tripleBlink(int time_on, int time_off){
	for(int i=0; i<3; i++){			// do the following sequence three times
		digitalWrite(ledPin, HIGH);	// turn on the LED
		delay(time_on);			// leave the LED on for time_on seconds
		digitalWrite(ledPin, LOW);	// turn off the LED
		delay(time_off);		// leave the LED off for time_off seconds
	}
}

Now our main code can just call the tripleBlink() function like so:

tripleBlink(300, 200);	// blink three blinks at .3 seconds each – short blinks
tripleBlink(700,200);	// blink three blinks at .7 seconds each – long blinks
tripleBlink(300,200);	// blink three blinks at .3 seconds each – short blinks
delay(500);		// wait .5 seconds before repeating the process

This will reproduce our SOS blink pattern. The final complete sketch looks like:

int ledPin = 13;
void setup() {
        pinMode(ledPin, OUTPUT);
}
void tripleBlink(int time_on, int time_off){
	for(int i=0; i<3; i++){
		digitalWrite(ledPin, HIGH);
		delay(time_on);
		digitalWrite(ledPin, LOW);
		delay(time_off);
	}
}
void loop(){
	tripleBlink(300, 200);
	tripleBlink(700, 200);
	tripleBlink(300, 200);
	delay(500);
}

Go ahead and run this sketch; you should see your Arduino blink out a desperate SOS plea. Success! If you are interested in seeing what other functions the Arduino programming language supports natively, see the full reference at http://arduino.cc/en/Reference/HomePage. The Arduino web site also has some good introductory descriptions of basic concepts related to MCUs at http://arduino.cc/en/Tutorial/Foundations.

In the next article of our series, we will walk through getting an MSP430 MCU to blink out an SOS using the IAR Kickstart IDE.

- Wiretrip Hardhack Team

Posted in Arduino, Development Tools, Discovering, Microcontroller, Projects.


6 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Shawn says

    Nice Article! Well thought out and worded. Being a newbie this was a great find to learn from.

  2. follower says

    Just a pedantic note, the Arduino doesn’t really use a “special C-esque programming language” it’s actually standard C/C++ but only a subset is documented in order to simplify getting started.

    It also features a post-processor that removes the need for certain boilerplate, like function prototype declarations.

    –Phil.

  3. Cail says

    @follower: that’s a good distinction; we’ll try to not be so loose with our wording/descriptions in future articles…but I can’t make any guarantees because we’re just amateurs and not experts. :)

    Thanks,
    – Cail, Hardhack

  4. Kent Fenwick says

    For all those who love awesome languages like Ruby, checkout RAD http://rad.rubyforge.org/.

    It let’s your program Arduino using Ruby :)

Continuing the Discussion

  1. The Split Brain linked to this post on February 5, 2009

    Programming the Arduino…

    uC Hobby has an excellent article on programming on the Arduino platform. I have a particular interest in this since I own and program one of this suckers for fun.
    Check it out if you’re interested in learning about microcontroller programming! T…

  2. AVR Dragon: Getting Started | uC Hobby linked to this post on March 2, 2009

    [...] MCU Programming Intro Series #1: SOS on the Arduino [...]