Arduino 1: Writing Sketches and Printing to Screen

Connect the USB cable between the Arduino and the computer, then start the Arduino software. Once the software opens, the first thing to do is to tell the software on which port the Arduino is connected. In the top menu click on Tools>Serial Port, and generally you choose the last COM port on the list (the latest version of the software, Arduino 17, will do this automatically.) Also set up which Arduino board you have by going to Tools>Board, and clicking on the proper one (as of 12/2009, probably the Duemilanova – the name is stamped on your Arduino board.)

Hello world – getting Arduino to print to the laptop screen

Unlike traditional programming, programs written in Arduino (called “sketches”) are usually intended to loop infinitely. While that’s generally a bug in a typical program, you want a microcontroller running a project to keep going until you turn it off, so infinite loops are OK. Let’s start by printing a message to the laptop to make sure everything is connected. Since the program will keep looping, let’s have it alternate printing two words, with a one-second delay between them. Type in the following program, watching the capitalization and punctuation:

void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(“hello”);
delay(1000);
Serial.println(“world.”);
delay(1000);
}

Once it’s typed in, you upload it to the Arduino board by clicking the sixth icon in the icon row at the top of the screen, the right-pointing arrow. You will get the message “Uploading to I/O board”, and lights should flash on the Arduino. If you typed something incorrectly, you will get a message to that effect at the bottom of the screen in red– make your corrections and upload again.  If instead you get the message “Done uploading”, it worked. But in order to see output from the Arduino you have to click the last icon in the row at the top of the screen, the Serial Monitor.  Then a new window will display information sent back from the Arduino. If you see gibberish, check the little drop-down box in the bottom right of the window, which is the communication speed – it should read 9600. If not, change it to 9600. You ought to see a continuing sequence of  “hello” and “world.” go by indefinitely. To stop it running, click the second icon in the row, the square. This just stops the printout – the program is still running on the Arduino board.

Arduino programs usually are made up of two functions, called setup() and loop(). You start a function with the command “void”, and names of functions end in a pair of parentheses (). The setup function runs just once, and takes care of initializing values in variables or defining whether pins on the Arduino are input or output. The loop function holds all the code that is meant to loop indefinitely, which is the body of your program. Notice that all the commands in a function are contained in curly braces, {}, and that all command lines end in a semicolon.

In this program, the command

Serial.begin(9600);

opens up a line of communication between the Arduino and the computer at 9600 baud, or bits per second (specifically use 9600, not any old number). You must do this in setup if you want to have Arduino print to the computer screen. The command

Serial.println(“text”);

prints whatever text you put in quotes to the screen, then moves the cursor to the next line. If you put a variable in the parentheses with no quotes, it would print the value stored in the variable. The command

delay(n);

makes the program pause n milliseconds before continuing (n should be a positive integer).

At this point, it would be a good idea to check out the language reference page at the Arduino website. You can also get there from within the Arduino software via the top menu, at Help>Reference.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s