Follow PeterJFrancis on Twitter

Monday 22 November 2010

Arduino Tutorial - 01 Hello World

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.



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.

Monday 13 September 2010

Arduino 'Cheat' sheet



I came across this cheat sheet a couple of months ago and have used it ever since, its being updated regularly.
Print it out at A3 size and pin it up somewhere convenient and it will become your new best friend !

Arduino Cheat Sheet

Friday 3 September 2010

Monday 30 August 2010

Map of Arduino users

I came across this Arduino User map , here's the link Map why not add yourself !

Thursday 26 August 2010

Online Course: Processing and Arduino

Online Course: Processing and Arduino in Tandem Creating Your Own Digital Art Tools http://oreil.ly/a4Zea5

Wednesday 25 August 2010

New Toys !


I've spent the last few days setting up 2 new MCAD (Mechanical CAD) PC's , and after a few licencing problems they are finally ready to use. One machine is for use by our Production dept and the other is for me , its a Dell Precision M6500 laptop ( http://www.dell.com/us/en/business/notebooks/precision-m6500/pd.aspx?refid=precision-m6500&cs=04&s=bsd )


I've also got some new software to use which I'm rather excited about , its a Computational Fluid Dynamics (see below for link to definition of CFD ) package called FloEFD.Pro by Mentor Graphics.
This will be used to analyse our new designs from both a fluid flow and thermal viewpoint, we will also be offering this facility as a service to our customers. I hope to be able to show you some of the work I'm doing with this over the coming months.

I've just run the first analysis on an assembly consisting of 2 PCBs stacked in a small plastic enclosure. I wanted to look at the thermal performance as the a couple of components have a fairly high heat dissipation. The results are correlating quite well with the physical testing we have been doing on the prototypes. I'll post some more detailed images when I've finished post processing the results, in the mean time here is an image to keep you going.



Links


Dell Precision M6500 laptop

http://www.dell.com/us/en/business/notebooks/precision-m6500/pd.aspx?refid=precision-m6500&cs=04&s=bsd

PTC Pro/Engineer Wildfire 4

http://www.ptc.com/products/proengineer/

Mentor Graphics FloEFD.Pro
http://www.mentor.com/products/mechanical/products/floefd/

Definition of CFD
http://en.wikipedia.org/wiki/Computational_fluid_dynamics

Tuesday 24 August 2010

1st Post of many...

Welcome to the first of many posts .... I will be posting on a variety of subjects , stay tuned !