There's an Arduino for that!
Written by Harry Fairhead   
Article Index
There's an Arduino for that!
Hello World of Arduino

 

The Hello World of Arduino programming is a short program that flashes the built-in LED which is connected to pin 13. 

There are 14 digital I/O pins but the first 8 have a dedicated job leaving pins 8 to 13 for general purpose tasks. Pin 13 has a surface mount LED connected to it ready for you to flash and generally use as a status indicator. You can see pins 8 to 13 at the top edge of the photo below and you can see the LED marked with L next to the Arduino symbol (just above the TX RX LEDs).

 

UNO

 

To flash the Pin 13 LED we are going to write a simple program that first sets Pin 13 to be an output - any pin can either be used as an input or an output and then we are going to write a loop that sets the pin high and then low with a suitable delay.

As all embedded hardware programs have the same structure - an initialization portion and a block of code that is repeated forever - the Arduino language has two special functions - setup and loop. The setup function is called once when the program first starts and the loop function is called repeatedly. Basically the the master Arduino program is

setup();
while(true){
 loop();
}

In other words the code you place in setup is called once right at the start and never again and the code you place in loop is called repeatedly as fast as possible.

In this case we need to put the initialization code in setup and the flashing code in loop.

If you run the Arduino editor it opens with a blank "sketch" ready for you to enter some code. The setup function is easy enough - you simply have to set pin 13 to act as an output:

void setup() {                
pinMode(13, OUTPUT);     
}

The only thing difficult about this function is knowing that there is a predefined function called pinMode which sets the direction of any pin.

The loop function is a little more difficult. You have to set the pin high and then low again to make the LED flash. This is easy as there is a function digitalWrite which can be used to set any pin to HIGH or low. However if you just write:

void loop() {
digitalWrite(13, HIGH);   
digitalWrite(13, LOW);   
}

and run the resulting program all you will see is the LED on all of the time. The reason is that the loop function is called repeatedly as fast as possible and so the LED flashes as fast as possible an in this case it is so fast you can't actually see it flash at all.

The solution is to include a delay to keep the LED on long enough and off long enough for the flashing to be obvious.

This too is easy as there is a Delay function which will pause the program for the specified number of milliseconds. You might think that :

void loop() {
digitalWrite(13, HIGH);  
delay(1000);   
digitalWrite(13, LOW);   
}

would do the job but you need to remember the loop is called repeatedly. Yes the LED is switched on then remains on for one second (i.e. 1000 milliseconds) and then it is switched off - but for how long? The answer is that the loop function is called almost immediately and the LED is switched on again. In other words the LED is on for one second but off for a very short time - again you just don't see it flash.

The correct version of the program is to make sure that the LED is on for one second and off for one second:

void loop() {
digitalWrite(13, HIGH);  
delay(1000);   
digitalWrite(13, LOW);   
delay(1000);
}

Now if you were to run the program you would see the LED flash at a reasonably slow speed.

It is this sort of reasoning about timing and what is happening in the real world that characterises this sort of programming.

The complete program is:

void setup() {                
pinMode(13, OUTPUT);     
}
void loop() {
digitalWrite(13, HIGH);  
delay(1000);   
digitalWrite(13, LOW);   
delay(1000);
}

You can also find an version of this program as part of the examples provided. If you want to try the example out then simply use the File,Open  menu command and navigate to examples/1. Basics/Blink and load the Blink file.

To run a program all you have to do is to use the menu command File,Upload to I/O Board or click the upload button:

run

Notice that there is also a Sketch.Verify/Compile command that simply checks that the program doesn't have any errors in it without downloading it and running it.

If you do click on the download and run button you will see the LED flash on for one second and off for one second. This is your first program and the rest are just as easy.

Where next?

After you have reached this point the next question is always what is the next best step?

When it comes to embedded programming the answer is complicated by the interplay between the hardware and the software. Your next step is probably to get some simple electronic components - some LEDs, a piezo sounder and some switches. If you want to be more ambitious then look at some of the add on modules - "shields" - that you can get to extend what the Arduino can do.  For programmers interested in creating "applications" the most useful are likely to be the Bluetooth, Ethernet, WiFi or XBee modules. But if you are into controlling existing equipment or building robots then the motor, servo or relay shields are likely to be what you are interested in. Remember if you can switch an LED on and off switching something bigger is just a matter of scaling up the output pin!

As far as learning more of the Arduino language is concerned the good news is that it is very simple and very like C, or any modern block-structured language you might know. There aren't many additional commands to learn to control the basic I/O on the Arduino board and these too are very similar to the commands you will find in almost any hardware control language - in other words the skills that you pick up by programming the Arduino are very general.

Finally if you want to go further - watch this space over the coming months for more Arduino-based projects and tutorials.

Of interest

Getting Started with Arduino Book Review

Practical Arduino  Book Review

Robot Xylophone - an Arduino project (Video)

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook, Digg or you can subscribe to our weekly newsletter.

 

<ASIN:B0046ANZ9S@UK>

<ASIN:B004CG4CN4@COM>

<ASIN:1430224770>

<ASIN:0596802471>



Last Updated ( Monday, 12 September 2011 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.