Introduction
This is the first in a series of Arduino tutorials that I intend to write. I hope you will find them useful and it will get you started with this fantastic tool.
This tutorial will cover the usual first program most people write, its called "Hello World" (flashing an LED on and off) but I will also include writing to the serial port as I feel this is so useful for debugging code.
I always add this to any code I write as it's a tidy way of confirming that the code is doing what you think it is, as well as confirming that the values you are generating are as expected.
IDE
Lets download the IDE (Integrated Development Environment) a big name for the program thats lets you write and transfer your code into an Arduino. Go to http://arduino.cc/en/Main/Software and select the version for your operating system, once its downloaded install it following the usual procedure for your machine.
Hardware
Now lets get the Arduino hardware ready to use, I always stick some rubber feet on the bottom of my Arduinos , this stops them damaging your worksurface and prevents them skidding around. I also usually use a solderless breadboard so i put a small bit of Bluetak or similar on each foot which is enough to hold it in place on the breadboard (see photo below) just don't press it down too hard or you'll fill the breadboard contacts with Bluetak.
Plug it in!
Right you're ready to go so plug in your Arduino to your PC using the usb lead (you did buy one didn't you ?). You should see one of the LEDs come on , this is a good sign as it proves your board is alive and functioning.
Downoad some code
Next we'll load the sample code for this tutorial, copy the code from here http://dl.dropbox.com/u/2326568/_1_Hello_World.pde
and put the file in your documents folder <DocumentsFolder>/Arduino/_1_Hello_World/ creating the folder if necessary.
Open the file
To open the file select FILE then SKETCHBOOK from the toolbar and then select the sketch _1_Hello_World.
Alternatively select FILE then OPEN then navigate to the folder <DocumentsFolder>/Arduino/_1_Hello_World/_1_Hello_World.pde. )
You should then see something like the following.
and put the file in your documents folder <DocumentsFolder>/Arduino/_1_Hello_World/ creating the folder if necessary.
Open the file
To open the file select FILE then SKETCHBOOK from the toolbar and then select the sketch _1_Hello_World.
You should then see something like the following.
Verify the code
You should now see the program loaded in the Arduino IDE. Lets check that its loaded OK and there are no errors, to do this we'll Verify the code. This is done with the button that looks like a DVD Play button. If you look at the bottom of the IDE you'll see the words 'Done Compiling' (see the screenshot above) if there was a problem the IDE would give you details of what and where the problem is, but we'll look at fault finding in a later tutorial.
Upload the code
Time to upload our code into the Arduino and see what it does. So press the right facing arrow to upload the code , this may take a few seconds, if you look at the Arduino you'll see a couple of LED's flashing these are Tx and Rx LED's and they show when the data is being written to the board. When the upload is complete the Arduino will start running the program, you'll see a small LED on the board flashing about once a second - Your first program is running!
Serial monitor
Time to see what else our Arduino can do , we are going to look at what what its sending out of its serial port. Open the Serial Monitor (its the far right icon) select 9600 baud on the bottom of the window that opens. And you should see the word "LED is ON" and "LED is OFF" in sync with the LED on the Arduino - clever eh ?
NOTE: Occasionally the IDE might not pick the correct serial port automatically so you may not see anything in the monitor window. If this happens just close the window and pick TOOLS then SERIAL PORT from the toolbar and select one of the other ports available the re-open the Serial Monitor
Look at the code
Lets look at the code line by line and see what it does, you'll see these comments in the actual code
All Arduino programs start with the setup section which is run only once when the program first runs. Its used for all those initialisation jobs and is contained in curly braces like these { }
void setup() {
}
The setup for our program is nice and simple
pinMode(13, OUTPUT); // sets the built-in LED pin as an OUTPUT
Serial.begin(9600); // Open serial port and set to 9600 baud
That's it , all setup and ready to run.
The next command that is in all Arduino programs is Loop, this is the main part of the program and it runs continuously, again its contained in curly braces.
void loop() {
}
// this loop runs continuously
digitalWrite(13, HIGH); // set the LED connected to Pin 13 HIGH ( or On )
Serial.println("LED is ON"); // sends the text LED is ON to the serial port
delay(1000); // wait for a second measured in 1000ths of a second
digitalWrite(13, LOW); // set the LED connected to Pin 13 LOW ( or Off )
Serial.println("LED is OFF"); // sends the text LED is OFF to the serial port
delay(1000); // wait for a second
// this then returns to the top of the loop and starts all over again
}
That's all there is to it, simple eh?
What next ?
Why don't you have a go at changing the program ?
Change the delays to speed up the flashing or change the text that is printed out.