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
Mary Wollstonecraft Shelley