Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Arduino - book1, Notas de estudo de Eletrônica

Arduino-book1

Tipologia: Notas de estudo

2017

Compartilhado em 07/11/2017

vinicius-tomaz-4
vinicius-tomaz-4 🇧🇷

4.5

(2)

14 documentos

1 / 627

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Table of Contents
Preface .................................................................... xiii
1. Getting Started ......................................................... 1
1.1 Installing the Integrated Development Environment (IDE) 4
1.2 Setting Up the Arduino Board 6
1.3 Using the Integrated Development Environment (IDE) to Prepare
an Arduino Sketch 8
1.4 Uploading and Running the Blink Sketch 11
1.5 Creating and Saving a Sketch 13
1.6 Using Arduino 15
2. Making the Sketch Do Your Bidding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.1 Structuring an Arduino Program 20
2.2 Using Simple Primitive Types (Variables) 21
2.3 Using Floating-Point Numbers 23
2.4 Working with Groups of Values 25
2.5 Using Arduino String Functionality 28
2.6 Using C Character Strings 30
2.7 Splitting Comma-Separated Text into Groups 32
2.8 Converting a Number to a String 34
2.9 Converting a String to a Number 36
2.10 Structuring Your Code into Functional Blocks 38
2.11 Returning More Than One Value from a Function 41
2.12 Taking Actions Based on Conditions 44
2.13 Repeating a Sequence of Statements 45
2.14 Repeating Statements with a Counter 47
2.15 Breaking Out of Loops 49
2.16 Taking a Variety of Actions Based on a Single Variable 50
2.17 Comparing Character and Numeric Values 52
2.18 Comparing Strings 54
2.19 Performing Logical Comparisons 55
v
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Arduino - book1 e outras Notas de estudo em PDF para Eletrônica, somente na Docsity!

Table of Contents

    1. Getting Started Preface xiii
      • 1.1 Installing the Integrated Development Environment (IDE)
      • 1.2 Setting Up the Arduino Board
        • an Arduino Sketch 1.3 Using the Integrated Development Environment (IDE) to Prepare
      • 1.4 Uploading and Running the Blink Sketch
      • 1.5 Creating and Saving a Sketch
      • 1.6 Using Arduino
    1. Making the Sketch Do Your Bidding
      • 2.1 Structuring an Arduino Program
      • 2.2 Using Simple Primitive Types (Variables)
      • 2.3 Using Floating-Point Numbers
      • 2.4 Working with Groups of Values
      • 2.5 Using Arduino String Functionality
      • 2.6 Using C Character Strings
      • 2.7 Splitting Comma-Separated Text into Groups
      • 2.8 Converting a Number to a String
      • 2.9 Converting a String to a Number
    • 2.10 Structuring Your Code into Functional Blocks
    • 2.11 Returning More Than One Value from a Function
    • 2.12 Taking Actions Based on Conditions
    • 2.13 Repeating a Sequence of Statements
    • 2.14 Repeating Statements with a Counter
    • 2.15 Breaking Out of Loops
    • 2.16 Taking a Variety of Actions Based on a Single Variable
    • 2.17 Comparing Character and Numeric Values
    • 2.18 Comparing Strings
    • 2.19 Performing Logical Comparisons
    • 2.20 Performing Bitwise Operations
    • 2.21 Combining Operations and Assignment
    1. Using Mathematical Operators
      • 3.1 Adding, Subtracting, Multiplying, and Dividing
      • 3.2 Incrementing and Decrementing Values
      • 3.3 Finding the Remainder After Dividing Two Values
      • 3.4 Determining the Absolute Value
      • 3.5 Constraining a Number to a Range of Values
      • 3.6 Finding the Minimum or Maximum of Some Values
      • 3.7 Raising a Number to a Power
      • 3.8 Taking the Square Root
      • 3.9 Rounding Floating-Point Numbers Up and Down
    • 3.10 Using Trigonometric Functions
    • 3.11 Generating Random Numbers
    • 3.12 Setting and Reading Bits
    • 3.13 Shifting Bits
    • 3.14 Extracting High and Low Bytes in an int or long
    • 3.15 Forming an int or long from High and Low Bytes
    1. Serial Communications
      • 4.1 Sending Debug Information from Arduino to Your Computer
      • 4.2 Sending Formatted Text and Numeric Data from Arduino
      • 4.3 Receiving Serial Data in Arduino
      • 4.4 Sending Multiple Text Fields from Arduino in a Single Message
      • 4.5 Receiving Multiple Text Fields in a Single Message in Arduino
      • 4.6 Sending Binary Data from Arduino
      • 4.7 Receiving Binary Data from Arduino on a Computer
      • 4.8 Sending Binary Values from Processing to Arduino
      • 4.9 Sending the Value of Multiple Arduino Pins
    • 4.10 How to Move the Mouse Cursor on a PC or Mac
    • 4.11 Controlling Google Earth Using Arduino
    • 4.12 Logging Arduino Data to a File on Your Computer
    • 4.13 Sending Data to Two Serial Devices at the Same Time
    • 4.14 Receiving Serial Data from Two Devices at the Same Time - and Receive Serial Data 4.15 Setting Up Processing on Your Computer to Send
    1. Simple Digital and Analog Input
      • 5.1 Using a Switch
      • 5.2 Using a Switch Without External Resistors
      • 5.3 Reliably Detecting the Closing of a Switch
      • 5.4 Determining How Long a Switch Is Pressed
      • 5.5 Reading a Keypad
      • 5.6 Reading Analog Values
      • 5.7 Changing the Range of Values
      • 5.8 Reading More Than Six Analog Inputs
      • 5.9 Displaying Voltages Up to 5V
    • 5.10 Responding to Changes in Voltage
    • 5.11 Measuring Voltages More Than 5V (Voltage Dividers)
    1. Getting Input from Sensors
      • 6.1 Detecting Movement
      • 6.2 Detecting Light
      • 6.3 Detecting Motion (Integrating Passive Infrared Detectors)
      • 6.4 Measuring Distance
      • 6.5 Measuring Distance Accurately
      • 6.6 Detecting Vibration
      • 6.7 Detecting Sound
      • 6.8 Measuring Temperature
      • 6.9 Reading RFID Tags
    • 6.10 Tracking the Movement of a Dial
    • 6.11 Tracking the Movement of More Than One Rotary Encoder
    • 6.12 Tracking the Movement of a Dial in a Busy Sketch
    • 6.13 Using a Mouse
    • 6.14 Getting Location from a GPS
    • 6.15 Detecting Rotation Using a Gyroscope
    • 6.16 Detecting Direction
    • 6.17 Getting Input from a Game Control Pad (PlayStation)
    • 6.18 Reading Acceleration
    1. Visual Output
      • 7.1 Connecting and Using LEDs
      • 7.2 Adjusting the Brightness of an LED
      • 7.3 Driving High-Power LEDs
      • 7.4 Adjusting the Color of an LED
      • 7.5 Sequencing Multiple LEDs: Creating a Bar Graph
        • Rider) 7.6 Sequencing Multiple LEDs: Making a Chase Sequence (Knight
      • 7.7 Controlling an LED Matrix Using Multiplexing
      • 7.8 Displaying Images on an LED Matrix
      • 7.9 Controlling a Matrix of LEDs: Charlieplexing
    • 7.10 Driving a 7-Segment LED Display
    • 7.11 Driving Multidigit, 7-Segment LED Displays: Multiplexing - Registers 7.12 Driving Multidigit, 7-Segment LED Displays Using MAX7221 Shift
      • 7.13 Controlling an Array of LEDs by Using MAX72xx Shift Registers - Chips (TLC5940) 7.14 Increasing the Number of Analog Outputs Using PWM Extender
      • 7.15 Using an Analog Panel Meter As a Display
      1. Physical Output
        • 8.1 Controlling the Position of a Servo
          • or Sensor 8.2 Controlling One or Two Servos with a Potentiometer
        • 8.3 Controlling the Speed of Continuous Rotation Servos
        • 8.4 Controlling Servos from the Serial Port
        • 8.5 Driving a Brushless Motor (Using a Hobby Speed Controller)
        • 8.6 Controlling Solenoids and Relays
        • 8.7 Making an Object Vibrate
        • 8.8 Driving a Brushed Motor Using a Transistor
          • with an H-Bridge 8.9 Controlling the Direction of a Brushed Motor
          • H-Bridge 8.10 Controlling the Direction and Speed of a Brushed Motor with an
          • Motors (L293 H-Bridge) 8.11 Using Sensors to Control the Direction and Speed of Brushed
      • 8.12 Driving a Bipolar Stepper Motor
      • 8.13 Driving a Bipolar Stepper Motor (Using the EasyDriver Board)
      • 8.14 Driving a Unipolar Stepper Motor (ULN2003A)
      1. Audio Output
        • 9.1 Playing Tones
        • 9.2 Playing a Simple Melody
        • 9.3 Generating More Than One Simultaneous Tone
        • 9.4 Generating Audio Tones and Fading an LED
        • 9.5 Playing a WAV File
        • 9.6 Controlling MIDI
        • 9.7 Making an Audio Synthesizer
    1. Remotely Controlling External Devices
      • 10.1 Responding to an Infrared Remote Control
      • 10.2 Decoding Infrared Remote Control Signals
      • 10.3 Imitating Remote Control Signals
      • 10.4 Controlling a Digital Camera
      • 10.5 Controlling AC Devices by Hacking a Remote Controlled Switch
    1. Using Displays
      • 11.1 Connecting and Using a Text LCD Display
      • 11.2 Formatting Text
      • 11.3 Turning the Cursor and Display On or Off
      • 11.4 Scrolling Text
      • 11.5 Displaying Special Symbols
      • 11.6 Creating Custom Characters
      • 11.7 Displaying Symbols Larger Than a Single Character
      • 11.8 Displaying Pixels Smaller Than a Single Character
      • 11.9 Connecting and Using a Graphical LCD Display
    • 11.10 Creating Bitmaps for Use with a Graphical Display
    • 11.11 Displaying Text on a TV
    1. Using Time and Dates
      • 12.1 Creating Delays
      • 12.2 Using millis to Determine Duration
      • 12.3 More Precisely Measuring the Duration of a Pulse
      • 12.4 Using Arduino As a Clock
      • 12.5 Creating an Alarm to Periodically Call a Function
      • 12.6 Using a Real-Time Clock
    1. Communicating Using I2C and SPI
      • 13.1 Controlling an RGB LED Using the BlinkM Module
      • 13.2 Using the Wii Nunchuck Accelerometer
      • 13.3 Interfacing to an External Real-Time Clock
      • 13.4 Adding External EEPROM Memory
      • 13.5 Reading Temperature with a Digital Thermometer
      • 13.6 Driving Four 7-Segment LEDs Using Only Two Wires
      • 13.7 Integrating an I2C Port Expander
      • 13.8 Driving Multidigit, 7-Segment Displays Using SPI
      • 13.9 Communicating Between Two or More Arduino Boards
    1. Wireless Communication
      • 14.1 Sending Messages Using Low-Cost Wireless Modules
      • 14.2 Connecting Arduino to a ZigBee or 802.15.4 Network
      • 14.3 Sending a Message to a Particular XBee
      • 14.4 Sending Sensor Data Between XBees
      • 14.5 Activating an Actuator Connected to an XBee
    1. Ethernet and Networking
      • 15.1 Setting Up the Ethernet Shield
      • 15.2 Obtaining Your IP Address Automatically
      • 15.3 Resolving Hostnames to IP Addresses (DNS)
      • 15.4 Requesting Data from a Web Server
      • 15.5 Requesting Data from a Web Server Using XML
        • 15.6 Setting Up an Arduino to Be a Web Server
        • 15.7 Handling Incoming Web Requests
        • 15.8 Handling Incoming Requests for Specific Pages
        • 15.9 Using HTML to Format Web Server Responses
      • 15.10 Serving Web Pages Using Forms (POST)
      • 15.11 Serving Web Pages Containing Large Amounts of Data
      • 15.12 Sending Twitter Messages
      • 15.13 Sending and Receiving Simple Messages (UDP)
      • 15.14 Getting the Time from an Internet Time Server
      • 15.15 Monitoring Pachube Feeds
      • 15.16 Sending Information to Pachube
    1. Using, Modifying, and Creating Libraries
      • 16.1 Using the Built-in Libraries
      • 16.2 Installing Third-Party Libraries
      • 16.3 Modifying a Library
      • 16.4 Creating Your Own Library
      • 16.5 Creating a Library That Uses Other Libraries
    1. Advanced Coding and Memory Handling
      • 17.1 Understanding the Arduino Build Process
      • 17.2 Determining the Amount of Free and Used RAM
      • 17.3 Storing and Retrieving Numeric Values in Program Memory
      • 17.4 Storing and Retrieving Strings in Program Memory
      • 17.5 Using #define and const Instead of Integers
      • 17.6 Using Conditional Compilations
    1. Using the Controller Chip Hardware
      • 18.1 Storing Data in Permanent EEPROM Memory
      • 18.2 Using Hardware Interrupts
      • 18.3 Setting Timer Duration
      • 18.4 Setting Timer Pulse Width and Duration
      • 18.5 Creating a Pulse Generator
      • 18.6 Changing a Timer’s PWM Frequency
      • 18.7 Counting Pulses
      • 18.8 Measuring Pulses More Accurately
      • 18.9 Measuring Analog Values Quickly
      • 18.10 Reducing Battery Drain
      • 18.11 Setting Digital Pins Quickly
    • A. Electronic Components
    • B. Using Schematic Diagrams and Data Sheets
      • C. Building and Connecting the Circuit
    • D. Tips on Troubleshooting Software Problems
      • E. Tips on Troubleshooting Hardware Problems
      • F. Digital and Analog Pins
    • G. ASCII and Extended Character Sets
  • Index

CHAPTER 1

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

See Also

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)

Problem

You want to install the Arduino development environment on your computer.

Solution

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 (where is the ver- sion number of the Arduino release you downloaded). The directory contains the executable file (named Arduino.exe ), along with various other files and folders. Double- click the Arduino.exe file and the splash screen should appear (see Figure 1-2), followed by the main program window (see Figure 1-3). Be patient, as it can take some time for the software to load.

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.

Discussion

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.

See Also

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

Problem

You want to power up a new board and verify that it is working.

Solution

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)

Discussion

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.

See Also

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

Discussion

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.

Discussion

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

See Also

The Arduino troubleshooting page: http://www.arduino.cc/en/Guide/Troubleshooting

1.5 Creating and Saving a Sketch

Problem

You want to create a sketch and save it to your computer.

Solution

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.

Discussion

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