Getting Started - Programming Arduino Getting Started with Sketches (2012)

Programming Arduino Getting Started with Sketches (2012)

2
Getting Started

Having introduced the Arduino, and learnt a little about what it is that we are programming, it is time to learn how install the software that we will need on our computer and to start working on some code.

Powering Up

When you buy an Arduino board, it is usually preinstalled with a sample Blink program that will make the little built-in light-emitting diode (LED) flash.

The LED marked L is wired up to one of the digital input output sockets on the board. It is connected to digital pin 13. This limits pin 13 to being the one used as an output. However, the LED uses only a small amount of current, so you can still connect other things to that connector.

All you need to do to get your Arduino up and running is supply it with some power. The easiest way to do this is to plug in it into the USB port on your computer. You will need a type-A-to-type-B USB lead. This is the same type of lead that is normally used to connect a computer to a printer.

If everything is working OK, the LED should blink. New Arduino boards come with this Blink sketch already installed so that you can verify that the board works.

Installing the Software

To be able to install new sketches onto your Arduino board, you need to do more than supply power to it over the USB. You need to install the Arduino software (Figure 2-1).

Full and comprehensive instructions for installing this software on Windows, Linux, and Mac computers can be found at the Arduino website (www.arduino.cc).

Once you have successfully installed the Arduino software and, depending on your platform, USB drivers, you should now be able to upload a program to the Arduino board.

Image

Figure 2-1 The Arduino application

Uploading Your First Sketch

The blinking LED is the Arduino equivalent to the “Hello World” program used in other languages as the traditional first program to run when learning a new language. Let’s test out the environment by installing this program on your Arduino board and then modifying it.

When you start the Arduino application on your computer, it opens with an empty sketch. Fortunately, the application ships with a wide range of useful examples. So from the File menu, open the Blink example as shown in Figure 2-2.

Image

Figure 2-2 The Blink sketch

You now need to transfer or upload that sketch to your Arduino board. So plug your Arduino board into your computer using the USB lead. You should see the green “On” LED on the Arduino light up. The Arduino board will probably already be flashing, as the boards are generally shipped with the Blink sketch already installed. But let’s install it again and then modify it.

When you plug the board in, if you are using a Mac, you may get the message, “A new network interface has been detected.” Just click Cancel; your Mac is confused and thinks that the Uno is a USB modem.

Before you can upload a sketch, you must tell the Arduino application what type of board you are using and which serial port you are connected to. Figures 2-3 and 2-4 show how you do this from the Tools menu.

Image

Figure 2-3 Selecting the board type

Image

Figure 2-4 Selecting the serial port (in Windows)

On a Windows machine, the serial port is always COM3. On Macs and Linux machines, you will see a much longer list of serial devices (see Figure 2-5). The device will normally be the top selection in the list, with a name similar to /dev/tty.usbmodem621.

Now click on the Upload icon in the toolbar. This is shown highlighted in Figure 2-6.

After you click the button, there is a short pause while the sketch is compiled and then the transfer begins. If it is working, then there will be some furious blinking of LEDs as the sketch is transferred, after which you should see the message “Done Uploading” at the bottom of the Arduino application window and a further message similar to “Binary sketch size: 1018 bytes (of a 14336 byte maximum).”

Image

Figure 2-5 Selecting the serial port (on a Mac)

Once uploaded, the board automatically starts running the sketch and you will see the LED start to blink.

If this did not work, then check your serial and board type settings.

Now let’s modify the sketch to make the LED blink faster. To do this, let’s alter the two places in the sketch where there is a delay for 1,000 milliseconds so that the delay is 500 milliseconds. Figure 2-7 shows the modified sketch with the changes highlighted.

Image

Figure 2-6 Uploading the sketch

Click on the Upload button again. Then, once the sketch has uploaded, you should see your LED start to blink twice as fast as it did before.

Congratulations, you are now ready to start programming your Arduino. First, though, let’s take a mini-tour of the Arduino application.

Image

Figure 2-7 Modifying the Blink sketch

The Arduino Application

Sketches in Arduino are like documents in a word processor. You can open them and copy parts from one to another. So you see options to Open, Save, and Save As in the File menu. You will not normally use Open because the Arduino application has the concept of a Sketchbook where all your sketches are kept carefully organized into folders. You gain access to the Sketchbook from the File menu. As you have just installed the Arduino application for the first time, your Sketchbook will be empty until you create some sketches.

As you have seen, the Arduino application comes with a selection of example sketches that can be very useful. Having modified the Blink example sketch, if you try and save it, you get a message that says, “Some files are marked read-only so you will need to save this sketch in a different location.”

Try this now. Accept the default location, but change the filename to MyBlink, as shown in Figure 2-8.

Now if you go to the File menu and then click on Sketches, you will see MyBlink as one of the sketches listed. If you look at your computer’s file system, on a PC, you will find that the sketch has been written into My Documents\Arduino, and on Mac or Linux, they are in Documents/Arduino.

All of the sketches used in this book can be downloaded as a zip file (Programming_Arduino.zip) from www.arduinobook.com. I suggest that now is the time to download this file and unzip it into the Arduino folder that contains the sketches. In other words, when you have unzipped the folder, there should be two folders in your Arduino folder: one for the newly saved MyBlink and one called Programming Arduino (see Figure 2-9). The Programming Arduino folder will contain all the sketches, numbered according to chapter, so that sketch 03-01, for example, is sketch 1 of Chapter 3.

Image

Figure 2-8 Saving a copy of Blink

These sketches will not appear in your Sketchbook menu until you quit the Arduino application and restart it. Do so now. Then your Sketchbook menu should look similar to that shown in Figure 2-10.

Image

Figure 2-9 Installing the sketches from the book

Image

Figure 2-10 Sketchbook with the book’s sketches installed

Conclusion

Your environment is all set up and ready to go.

In the next chapter, we will look at some of the basic principles of the C language that the Arduino uses and start writing some code.