The Arduino Inventor's Guide (8 page)

BOOK: The Arduino Inventor's Guide
8.28Mb size Format: txt, pdf, ePub
ads

NOTE

If you plugged in your board before installing the IDE and drivers, you may need to restart your computer.

FIGURE 1-14:
The LED labeled 13 blinks when you power a new board.

Choosing Your Board in the IDE

Depending on your computer or operating system, it may take a little bit for the computer to identify the new hardware you just plugged in and associate it with the drivers you installed. After your computer recognizes the new device, click
Tools
and mouse over the
Board
option, as in
Figure 1-15
.

FIGURE 1-15:
The Board selection list in the Tools menu

A list of pre-supported Arduino boards should appear. If you’re using a standard Arduino Uno or the SparkFun RedBoard, select the option
Arduino/Genuino Uno
. If you end up using a board other than an Uno or RedBoard in the future, select the correct Arduino based on your board’s documentation—this book assumes that you’re using the Uno or an Uno derivative.

Selecting the Communication Port

Each device that’s plugged into your computer has a unique communication port identifier. You need to configure the IDE so that it knows which port your Arduino is connected to. To do this, first select
Tools

Port
to see the communication port options for your device. You’ll see different options depending on your operating system.

On Windows

If you’re using a Windows PC, you may see COM3, COM4, or another numbered COM port, as shown in
Figure 1-16
. Select this option. If no options show up, see “
Basic Arduino Troubleshooting
” on page
27
.

FIGURE 1-16:
Selecting the communication port on Windows

On OS X and Linux

On Mac or Linux machines, the communication port should be listed as
/dev/cu.usbserial-A
, where the

is a string of random characters unique to your Arduino. Select this option. You may see more than one port listed, as in
Figure 1-17
, but only the one with this unique ID string will map to your Arduino. If no options show up, see “
Basic Arduino Troubleshooting
” on page
27
.

FIGURE 1-17:
Selecting the communication port on OS X

AN ARDUINO “HELLO, WORLD!”

“Hello, world!” is the classic first program that many beginning programmers write. In most other programming languages, this program displays
Hello, world!
to the screen. Because the Arduino doesn’t have a screen, its version of “Hello, world!” is a blinking LED.

For your first sketch, we’ll show you how to use an example that comes with the Arduino IDE. With your board connected to your computer, click the
File
drop-down menu and select
Examples

01.Basics

Blink
as shown in
Figure 1-18
to open a sketch called
Blink
.

A new IDE window containing the Blink sketch should open. In this window, click
Sketch

Upload
or click the
Upload
icon. The IDE will turn this relatively human-readable code into 1s and 0s that the Arduino understands (a process called
compiling
) and then upload the sketch to your board.

FIGURE 1-18:
Finding the Blink sketch

After you click Upload, watch the alert area for status messages. It should say
Compiling sketch…
and show a progress bar. After the compiling is complete, your computer will start to upload the sketch to your Arduino. The RX (receive) and TX (transmit) LEDs on your Arduino board should blink rapidly, indicating that the sketch is being transmitted to the Arduino board. The TX light blinks because you’re transmitting something to the Arduino, and the RX light blinks because as the Arduino receives the sketch, it responds to your computer to confirm receipt. When the upload process is done, the status area on the IDE should say
Upload complete
, and the LED labeled 13 on your board should blink, as in
Figure 1-19
.

FIGURE 1-19:
Turning on LED 13

If you get any error messages, your Arduino might not be talking to your computer. Read the next section,

Basic Arduino Troubleshooting
,” for some common problems to look out for, and then try uploading the sketch again.

BASIC ARDUINO TROUBLESHOOTING

The Arduino, like any other programmable piece of electronics, is temperamental at times. Here are a few troubleshooting tips for when you have trouble programming your Arduino.

1. Make sure that your Arduino is plugged into your USB cable and that the cable is plugged into your computer all the way. It’s easy to have the cable only partially plugged into the board. You can also try unplugging it and plugging it back in.

2. Always confirm the board selected in the Board menu is the board plugged into your computer. For our examples, we will have
Arduino/Genuino Uno
selected.

3. Confirm that the correct communication port is selected in the Tools

Port menu; it should have a checkmark or dot next to it. If you’re not sure which port goes with your Arduino, unplug your USB cable from your computer, refresh the communication port listing, and watch to see which port disappears.

4. Make sure you didn’t accidentally type some stray characters into your example sketch. The code will not compile if there are any extra characters.

5. On Windows, check your computer’s Device Manager. Make sure that there isn’t an exclamation mark next to the device. If there is, you need to reinstall the drivers manually.

6. If you’re still getting error messages, reinstall the drivers for your board. We have additional instructions available at
www.sparkfun.com/ftdi/
.

These six tips are solutions to common speed bumps for anyone new to Arduino, so start here. If none of these suggestions solves the issue, just stay calm, be patient, and remember that you’re not the first one to have a problem. If you get completely stuck, look for solutions on the official Arduino forum at
http://forum.arduino.cc/
.

ANATOMY OF AN ARDUINO SKETCH

In this section, we’ll walk you through the Blink sketch that you uploaded to the Arduino in ‘An Arduino “Hello, World!”’ on page
25
. First,
Listing 1-1
gives the sketch itself, in all its blinky glory.

LISTING 1-1:
The Blink example sketch


/*
    
Blink
    
Turns an LED on for one second, then off for one second,
      
repeatedly.
    
Most Arduinos have an onboard LED you can control. On the
      
UNO, MEGA, and ZERO, it is attached to digital pin 13;
      
on the MKR1000 it's on pin 6. LED_BUILTIN is set to the
      
correct LED pin independent of which board is used.
    
If you want to know which pin the onboard LED is connected
      
to on your Arduino model, check the Technical Specs of
      
your board at https://www.arduino.cc/en/Main/Products
    
This example code is in the public domain.
    
modified 8 May 2014
    
by Scott Fitzgerald
    
modified 2 Sep 2016
    
by Arturo Guadalupi
    
modified 8 Sep 2016
    
by Colby Newman
  
*/
  
//the setup function runs once when you press reset or
  
//power the board

void
setup
() {
    
//initialize digital pin LED_BUILTIN as an output
    
pinMode
(
LED_BUILTIN
,
OUTPUT
);
  }
  
//the loop function runs over and over again forever

void
loop
() {
      
digitalWrite
(
LED_BUILTIN
,
HIGH
);
//turn the LED on
                                       
//(voltage level is HIGH)
      
delay
(1000);                     
//wait for a second
      
digitalWrite
(
LED_BUILTIN
,
LOW
);  
//turn the LED off
                                       
//(voltage level is LOW)
      
delay
(1000);                     
//wait for a second
  }

BOOK: The Arduino Inventor's Guide
8.28Mb size Format: txt, pdf, ePub
ads

Other books

Unbelievable by Lori Foster
Outer Dark by Cormac McCarthy
A Word with the Bachelor by Teresa Southwick
Sugar Skulls by Lisa Mantchev, Glenn Dallas
The Sins of Lady Dacey by Marion Chesney
InkintheBlood by Chandra Ryan
The Closer by Rhonda Nelson