




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Encontra documentos específicos para os exames da tua universidade
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Arduino-book1
Tipologia: Notas de estudo
1 / 627
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































Getting Started
1.0 Introduction
The Arduino environment has been designed to be easy to use for beginners who have no software or electronics experience. With Arduino, you can build objects that can respond to and/or control light, sound, touch, and movement. Arduino has been used to create an amazing variety of things, including musical instruments, robots, light sculptures, games, interactive furniture, and even interactive clothing.
If you’re not a beginner, please feel free to skip ahead to recipes that interest you.
Arduino is used in many educational programs around the world, particularly by de- signers and artists who want to easily create prototypes but do not need a deep under- standing of the technical details behind their creations. Because it is designed to be used by nontechnical people, the software includes plenty of example code to demonstrate how to use the Arduino board’s various facilities.
Though it is easy to use, Arduino’s underlying hardware works at the same level of sophistication that engineers employ to build embedded devices. People already work- ing with microcontrollers are also attracted to Arduino because of its agile development capabilities and its facility for quick implementation of ideas.
Arduino is best known for its hardware, but you also need software to program that hardware. Both the hardware and the software are called “Arduino.” The combination enables you to create projects that sense and control the physical world. The software is free, open source, and cross-platform. The boards are inexpensive to buy, or you can build your own (the hardware designs are also open source). In addition, there is an active and supportive Arduino community that is accessible worldwide through the Arduino forums and the wiki (known as the Arduino Playground). The forums and the
1
An overview of Arduino boards: http://www.arduino.cc/en/Main/Hardware.
Online guides for getting started with Arduino are available at http://arduino.cc/en/ Guide/Windows for Windows, http://arduino.cc/en/Guide/MacOSX for Mac OS X, and http://www.arduino.cc/playground/Learning/Linux for Linux.
Figure 1-1. Basic board: the Arduino Uno
1.0 Introduction | 3
1.1 Installing the Integrated Development Environment (IDE)
You want to install the Arduino development environment on your computer.
The Arduino software for Windows, Mac, and Linux can be downloaded from http:// arduino.cc/en/Main/Software.
The Windows download is a ZIP file. Unzip the file to any convenient directory— Program Files/Arduino is a sensible place.
A free utility for unzipping files, called 7-Zip, can be downloaded from http://www.7-zip.org/.
Unzipping the file will create a folder named Arduino-00
Figure 1-2. Arduino splash screen (version 0019 in Windows 7)
4 | Chapter 1: Getting Started
On Windows, use the USB cable to connect your PC and the Arduino board and wait for the Found New Hardware Wizard to appear. If you are using Windows Vista or Windows 7 and are online, you can let the wizard search for drivers and they will install automatically. On Windows XP, you should specify the location of the drivers. Use the file selector to navigate to the drivers directory, located in the directory where you unzipped the Arduino files. When the driver has installed, the Found New Hardware Wizard will appear again, saying a new serial port has been found. Follow the same process as before.
It is important that you go through the sequence of steps to install the drivers two times, or the software will not be able to communicate with the board.
On the Mac, the latest Arduino boards, such as the Uno, can be used without additional drivers, but if you are using earlier boards, you will need to install driver software. There is a package named FTDIUSBSerialDriver , with a range of numbers after it, inside the disk image. Double-click this and the installer will take you through the process. You will need to know an administrator password to complete the process.
On Linux, most distributions have the driver already installed, but follow the Linux link given in this chapter’s introduction for specific information for your distribution.
If the software fails to start, check the troubleshooting section of the Arduino website, http://arduino.cc/en/Guide/Troubleshooting , for help solving installation problems.
Online guides for getting started with Arduino are available at http://arduino.cc/en/ Guide/Windows for Windows, http://arduino.cc/en/Guide/MacOSX for Mac OS X, and http://www.arduino.cc/playground/Learning/Linux for Linux.
1.2 Setting Up the Arduino Board
You want to power up a new board and verify that it is working.
Plug the board into a USB port on your computer and check that the green LED power indicator on the board illuminates. Standard Arduino boards (Uno, Duemilanove, and Mega) have a green LED power indicator located near the reset switch.
6 | Chapter 1: Getting Started
An orange LED near the center of the board (labeled “Pin 13 LED” in Figure 1-4) should flash on and off when the board is powered up (boards come from the factory preloaded with software to flash the LED as a simple check that the board is working).
Figure 1-4. Basic Arduino board (Uno and Duemilanove)
If the power LED does not illuminate when the board is connected to your computer, the board is probably not receiving power.
The flashing LED (connected to digital output pin 13) is being controlled by code running on the board (new boards are preloaded with the Blink example sketch). If the pin 13 LED is flashing, the sketch is running correctly, which means the chip on the board is working. If the green power LED is on but the pin 13 LED is not flashing, it could be that the factory code is not on the chip; follow the instructions in Rec- ipe 1.3 to load the Blink sketch onto the board to verify that the board is working. If you are not using a standard board, it may not have a built-in LED on pin 13, so check the documentation for details of your board.
Online guides for getting started with Arduino are available at http://arduino.cc/en/ Guide/Windows for Windows, http://arduino.cc/en/Guide/MacOSX for Mac OS X, and http://www.arduino.cc/playground/Learning/Linux for Linux.
A troubleshooting guide can be found at http://arduino.cc/en/Guide/Troubleshooting.
1.2 Setting Up the Arduino Board | 7
Figure 1-5. Arduino IDE
Source code for Arduino is called a sketch. The process that takes a sketch and converts it into a form that will work on the board is called compilation. The IDE uses a number of command-line tools behind the scenes to compile a sketch. For more information on this, see Recipe 17.1.
1.3 Using the Integrated Development Environment (IDE) to Prepare an Arduino Sketch | 9
Figure 1-6. IDE menu (selecting the Blink example sketch)
The final message telling you the size of the sketch indicates how much program space is needed to store the controller instructions on the board. If the size of the compiled sketch is greater than the available memory on the board, the following error message is displayed:
Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.
If this happens, you need to make your sketch smaller to be able to put it on the board, or get a board with higher capacity.
10 | Chapter 1: Getting Started
On the Mac, your board will be listed twice if it is an Uno board:
/dev/tty.usbmodem- XXXXXXX /dev/cu.usbmodem- XXXXXXX
If you have an older board, it will be listed as follows:
/dev/tty.usbserial- XXXXXXX /dev/cu.usbserial- XXXXXXX
Each board will have different values for XXXXXXX. Select either entry.
Click on the upload button (in Figure 1-5, it’s the fifth button from the left), or choose File→Upload to I/O board.
The software will compile the code, as in Recipe 1.3. After the software is compiled, it is uploaded to the board. If you look at your board, you will see the LED stop flashing, and two lights (labeled as Serial LEDs in Figure 1-4) just below the previously flashing LED should flicker for a couple of seconds as the code uploads. The original light should then start flashing again as the code runs.
For the IDE to send the compiled code to the board, the board needs to be plugged into the computer, and you need to tell the IDE which board and serial port you are using.
When an upload starts, whatever sketch is running on the board is stopped (if you were running the Blink sketch, the LED will stop flashing). The new sketch is uploaded to the board, replacing the previous sketch. The new sketch will start running when the upload has successfully completed.
Older Arduino boards and some compatibles do not automatically in- terrupt the running sketch to initiate upload. In this case, you need to press the Reset button on the board just after the software reports that it is done compiling (when you see the message about the size of the sketch). It may take a few attempts to get the timing right between the end of the compilation and pressing the Reset button.
The IDE will display an error message if the upload is not successful. Problems are usually due to the wrong board or serial port being selected or the board not being plugged in.
If you have trouble identifying the correct port on Windows, try unplugging the board and then selecting Tools→Serial Port to see which COM port is no longer on the display list. Another approach is to select the ports, one by one, until you see the lights on the board flicker to indicate that the code is uploading.
12 | Chapter 1: Getting Started
The Arduino troubleshooting page: http://www.arduino.cc/en/Guide/Troubleshooting
1.5 Creating and Saving a Sketch
You want to create a sketch and save it to your computer.
To open an editor window ready for a new sketch, launch the IDE (see Recipe 1.3), go to the File menu, and select New. Paste the following code into the Sketch Editor win- dow (it’s similar to the Blink sketch, but the blinks last twice as long):
const int ledPin = 13; // LED connected to digital pin 13
void setup() { pinMode(ledPin, OUTPUT); }
void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(2000); // wait for two seconds digitalWrite(ledPin, LOW); // set the LED off delay(2000); // wait for two seconds }
Compile the code by clicking the compile button (the top-left button with a triangle inside), or select Sketch→Verify/Compile (see Recipe 1.3).
Upload the code by clicking on the upload button, or choose File→Upload to I/O board (see Recipe 1.4). After uploading, the LED should blink, with each flash lasting two seconds.
You can save this sketch to your computer by clicking the Save button, or select File→Save.
You can save the sketch using a new name by selecting the Save As menu option. A dialog box will open where you can enter the filename.
When you save a file in the IDE, a standard dialog box for the operating system will open. It suggests that you save the sketch to a folder called Arduino in your My Docu- ments folder (or your Documents folder on a Mac). You can replace the default sketch
1.5 Creating and Saving a Sketch | 13