Uploading our first sketch

Once you know how to connect the board to your computer and how to let the Arduino development environment know about it, the moment has come to create our first real test.

We will upload the simplest sketch to the Arduino board just to confirm that all parts are correctly set up and that you can, for sure, begin to work with it and learn how to program it.

What we will do is load one of the examples that comes with the Arduino development environment, in particular, one called Blink that makes use of the onboard integrated LED to make it do just that—blink.

To do this, go to the File menu in the menu bar and select the Examples command, navigate to 01.Basics, and select Blink.

The Arduino development environment should open a new window containing the following code for that example:

/*
  Blink
  Turns on an LED for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

We will go on with more details about the programming language and its characteristics in the coming chapters, but at the moment you just need to know that, it simply configures the Arduino board to use the onboard LED as an output in the setup function and turns it on and off infinitely, with a little delay in between, in the loop function, that is, blink an LED.

Once you have loaded your sketch, you only have to click on the Upload button on the toolbar (the one with the rightward-pointing arrow), which will cause the Arduino development environment to first compile the sketch if it wasn't already compiled and, if it finds no errors, it will upload it to the selected board through the specified serial port.

During the uploading process, you can see two other integrated LEDs labeled as TX/RX blinking, indicating that the serial communication has been established and that the Arduino board is receiving data from the computer and sending back an acknowledgment to it.

As soon as the uploading process has finished, the Arduino board will immediately begin to run the sketch and as a result, you should see the LED labeled L begin to blink at a 1 Hz frequency.

Congratulations, you have completed your first development cycle: edit the sketch (load it from the disk, in this case), compile it, upload it to the board, and run it there.

If that wasn't enough for you, perhaps you could try to simply change the two values in the delay functions from 1000 to, say, 300 and 500 and reupload it to the board to watch how the blinking frequency changes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset