Arduino Past Paper PDF (ASIA 413) Module 01
Document Details
Uploaded by ClearedEarthArt
Don Honorio Ventura State University
null
Brian Evans
Tags
Summary
This document is a module from Don Honorio Ventura State University. It is an introduction to Arduino, a flexible programmable hardware platform designed for artists and makers. The document covers the basics of Arduino hardware, software, and the Arduino platform.
Full Transcript
DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARC...
DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 Arduino is for Makers Arduino is a flexible programmable hardware platform designed for artists, designers, tinkerers, and the makers of things. Arduino’s little, blue circuit board, mythically taking its name from a local pub in Italy, has in a very short time motivated a new generation of DIYers of all ages to make all manner of wild projects found anywhere from the hallowed grounds of our universities to the scorching desert sands of a particularly infamous yearly arts festival and just about everywhere in between. Usually these Arduinobased projects require little to no programming skills or knowledge of electronics theory, and more often than not, this handiness is simply picked up along the way. Central to the Arduino interface board, shown in Figure 1-1, is an onboard microcontroller—think of it as a little computer on a chip. This microcontroller comes from a company called Atmel and the chip is known as an AVR. It is slow in modern terms, running at only 16Mhz with an 8-bit core, and has a very limited amount of available memory, with 32 kilobytes of storage and 2 kilobytes of random access memory. The interface board is known for its rather quirky design—just ask the die-hards about standardized pin spacing—but it also epitomizes the minimalist mantra of only making things as complicated as they absolutely need to be. Its design is not entirely new or revolutionary, beginning with a curious merger of two, off-the-shelf reference designs, one for an inexpensive microcontroller and the other for a USB-to-serial converter, with a handful of other useful components all wrapped up in a single board. Its predecessors include the venerable BASIC Stamp, which got its start as early as 1992, as well as the OOPic, Basic ATOM, BASICX24, and the PICAXE. Where all of these precursors are generally closed proprietary products and often require a single computer platform to use, the Arduino development environment is free for all to use _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 and will run on just about any kind of computer that supports Java. The actual hardware board costs a mere USD $30 or EUR €22 and needs nothing more complex than a USB cable to get up and running. This affordable price, nearly half that of its closest competitor, and the board’s durable design have led to numerous Arduino-like boards being stitched into embroidery embedded in pumpkins to be launched through the air by trebuchets, and even sent into outer space in weather balloons. This at least tells us a little bit about what kind of person the Arduino was originally designed for and about the hardware used in its design, but these things alone do not begin to account for the huge degree of success enjoyed by the Arduino as a whole. To get a sense for this popularity, we need to look at the larger Arduino ecosystem and how some fairly divergent parts came together to create a movement. In this chapter, we will learn about the different components on the Arduino board. We will study the Arduino UNO board because it is the most popular board in the Arduino board family. In addition, it is the best board to get started with electronics and coding. Some boards look a bit different from the one given below, but most Arduinos have majority of these components in common. 1.Power USB = Arduino board can be powered by using the USB cable from your computer. All you need to do is connect the USB cable to the USB connection (1). _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 2.Power (Barrel Jack) = Arduino boards can be powered directly from the AC mains power supply by connecting it to the Barrel Jack (2). 3.Voltage Regulator = The function of the voltage regulator is to control the voltage given to the Arduino board and stabilize the DC voltages used by the processor and other elements. 4.Crystal Oscillator = The crystal oscillator helps Arduino in dealing with time issues. How does Arduino calculate time? The answer is, by using the crystal oscillator. The number printed on top of the Arduino crystal is 16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16 MHz. 5.Arduino Reset = You can reset your Arduino board, i.e., start your program from the beginning. You can reset the UNO board in two ways. First, by using the reset button (17) on the board. Second, you can connect an external reset button to the Arduino pin labelled RESET (5). 6.Pins (3.3, 5, GND, Vin) = 3.3V (6) − Supply 3.3 output volt 7.5V (7) − Supply 5 output volt = Most of the components used with Arduino board works fine with 3.3 volt and 5 volt. 8.GND (8)(Ground) − There are several GND pins on the Arduino, any of which can be used to ground your circuit. 9.Vin (9) − This pin also can be used to power the Arduino board from an external power source, like AC mains power supply. 10.Analog pins = The Arduino UNO board has six analog input pins A0 through A5. These pins can read the signal from an analog sensor like the humidity sensor or temperature sensor and convert it into a digital value that can be read by the microprocessor. 11.Main microcontroller = Each Arduino board has its own microcontroller (11). You can assume it as the brain of your board. The main IC (integrated circuit) on the Arduino is slightly different from board to board. The microcontrollers are usually of the ATMEL Company. You must know what IC your board has before loading up a new program from the Arduino IDE. This information is available on the top of the IC. For more details about the IC construction and functions, you can refer to the data sheet. 12.ICSP pin = Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of MOSI, MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial Peripheral Interface), which could be considered as an "expansion" of the output. Actually, you are slaving the output device to the master of the SPI bus. 13.Power LED indicator = This LED should light up when you plug your Arduino into a power source to indicate that your board is powered up correctly. If this light does not turn on, then there is something wrong with the connection. 14.TX and RX LEDs = On your board, you will find two labels: TX (transmit) and RX (receive). They appear in two places on the Arduino UNO board. First, at the digital pins 0 and 1, to indicate the pins responsible for serial communication. Second, the TX and RX led (13). The TX led flashes with different _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 speed while sending the serial data. The speed of flashing depends on the baud rate used by the board. RX flashes during the receiving process. 15.Digital I/O = The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM (Pulse Width Modulation) output. These pins can be configured to work as input digital pins to read logic values (0 or 1) or as digital output pins to drive different modules like LEDs, relays, etc. The pins labeled “~” can be used to generate PWM. 16 AREF = AREF stands for Analog Reference. It is sometimes, used to set an external reference voltage (between 0 and 5 Volts) as the upper limit for the analog input pins The Arduino Ecosystem The Arduino is not just one simple thing making it a little hard to define. It is a microcontroller platform, an open-source design that encourages modification and reuse, a community that has embraced and grown up around the Arduino, and a new crop of projects and devices that can trace their lineage to the Arduino and have in return contributed back to the development of various aspects of the entire Arduino ecosystem. The Arduino Platform The Arduino ecosystem begins with the Arduino platform, itself several layers of hardware and software working together to create a cohesive whole. We can start with the hardware interface board—that little, blue circuit board that you build into your projects. It has a fairly standard onboard microcontroller that can interact with the world around it by using its programmable inputs and outputs, as well as a USB port and controller for easily communicating with your computer. This USB connectivity and friendly sockets for hookup wires to easily plug in to, contribute to the high level of usability in the interface board design. Equally important to this ecosystem is the Arduino development environment, a program based on the Processing development environment (http://processing.org) that you use to write, edit, compile, and upload your Arduino source code to the interface board. The Arduino team made the general assumption that people don’t really care about the myriad of technical specifics involved with microcontroller architecture—they just want it to do something cool. With that in mind, the Arduino development environment wraps up some of the more techie parts of programming AVR microcontrollers into a nice, simple library of Arduino-specific commands that are easier to use and easier to understand, and built right into every sketch written for the Arduino. This development environment is so versatile, that an Arduino interface board is not even needed to use it. Instead, we can use the very same AVR microcontroller as is built onto the interface board, but in an entirely different device—such as the example shown in Figure 1-3 of a microcontroller on a breadboard that has been programmed with an Arduino sketch. _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 Finally, we are brought to the Arduino bootloader, a little chunk of code residing on the microcontroller that allows it to be easily programmed with a simple serial connection rather than cranky, external hardware. As long as the bootloader has been programmed onto the microcontroller beforehand, we can use that chip in whatever device we choose, such as the breadboard in Figure 1-3, and still write code for it using the rest of the Arduino platform. It helps if we use one of the microcontrollers common to the Arduino, but this is not always a prerequisite, with many other Atmel microcontrollers working nearly as well with little or no modification. Open-Source Hardware The Arduino platform is itself pretty useful for microcontroller projects, but that alone is not enough to propel the popularity and widespread adoption of the platform. Instead of closing the design of the interface board and development environment, the entire Arduino project is deeply entrenched in the emerging practice of open-source hardware. Unlike open-source software, of which Linux is usually the often-cited example, open-source hardware seeks collaboration where physical objects are the outcome. It engages a distributed model of hardware development with contributors generally residing in different parts of the world. Rather than closed systems, open source projects allow an individual freedom to access the source files of a design, make improvements, and redistribute these improvements to a larger community. The Arduino ecosystem fundamentally embodies this aspiration for openness in design, architecture, collaboration, and philosophy. You can see it for yourself as all of the design files, schematics, and software are freely available to download, use, modify, remake, and even resell. What started as a seemingly serendipitous decision to open the Arduino design and _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 software to the greater community, spurred by the closing of the design school where the Arduino team was first formed, has lead to an entirely new movement in design. The practice of contributors having the liberty to use these designs freely (free as in speech) and with no obligation to buy anything (free as in beer) helps make the Arduino as endearing as a collection of silicon and copper can be. Not to mention that this creative feedback loop ensures that every inspired innovation derived from the Arduino platform is met with ever more imaginative uses for even more new things. Community Maybe even more important than the hardware platform itself, the Arduino community is most likely the single greatest cog in the Arduino machine. This community of makers has contributed to the Arduino ecosystem by developing code and libraries, designing new hardware, teaching workshops and classes, and sharing what they’ve made. The Arduino is now being taught in high schools, colleges, and universities everywhere and “Arduino Night” is a regular ritual at any of a number of hacker spaces around the world Arduinoland This community-driven research and development seems to have a magical effect on any project to come out of the wake of the Arduino, and this proverbial kingdom of mystical wonder is something I’m going to call Arduinoland. Maybe it’s because the hardware design works, is open, and is hackable, or maybe it’s because of a communal desire to build upon each other’s work, but whatever it is, in Arduinoland, soldering iron marvels, difficult to nearly impossible to pull off in a garage just ten years ago, seem to happen every day. Anything from interactive electronic textiles, autonomous flying aircraft, networked beer keg refrigerators, photographic missions to near space, and immersive architectural installations have all sprouted from this open Arduino ecosystem Arduino is C… Mostly In addition to the various aspects of the diverse Arduino ecosystem, we have the programming language of the Arduino platform, which is the central focus of this book. The core language used in the Arduino development environment is the C computer programming language first developed at the research institute of Bell Laboratories in the early 1970s for use with the UNIX operating system. C uses a procedural language syntax that needs to be processed by a compiler to map human-readable code to machine instructions. The long-standing popularity of C lends the Arduino some of its heritage, but the code that we are writing in this book is only mostly C. Because there are aspects of the C language that look like it was written by dyslexic aliens, and with the language sometimes accused of being overly cryptic and difficult for beginners to pick up, the Arduino team has developed the standard Arduino library that provides a simple and targeted set of functions that make programming the Arduino interface board about as easy as it can get. Now, these libraries are themselves actually C++, itself a subset of the original C language, but we really don’t need to go there. What’s important is that most of the code that we will write for the Arduino, including its syntax, structure, operators, control statements, and functions, remain fundamentally and functionally the same as C. What will be unique to _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 the Arduino, however, are all sorts of functions that you will come to know and love, including pinMode(), digitalWrite(), and delay() that are specific to the standard Arduino library. For the purposes of this book, this basic framework of C combined with the additional Arduino library that is automatically a part of every sketch that we write, is what we will refer to as Arduino C. To illustrate this point, Listings 1-1 and 1-2 provide two examples of the same source code to blink the onboard LED connected to digital pin 13. Listing 1-1. Blink LED with avr-libc #include #include int main(void) { while (1) { PORTB = 0x20; _delay_ms(1000); PORTB = 0x00; _delay_ms(1000); } return 1; } Listing 1-2. Blink LED with Arduino void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); } These two different listings show two functionally identical sketches, one written with the Arduino library and one written without. The really nifty thing here is that, if you want to geek out, the Arduino development environment is fully compatible and extensible using C/C++ code written using the avrlibc library, a subset of the standard C library, and the GCC compiler, both written for Atmel’s standard 8-bit AVR microcontrollers. Listing 1-1 is written with avr-libc while Listing 1-2 is written using the Arduino library. They both are compatible with the Arduino development environment and can be uploaded the same way to the Arduino board. The first example also consumes a fifth of the amount of memory as the second example, coming in at 210 bytes as opposed to 1010 bytes. What the Arduino example lacks in memory efficiency, however, it more than makes up for in usability and integration with the Arduino interface board. For example, referring to the digital pin that our LED is connected to as pin number 13 is generally easier for most people than the hexadecimal address 0x20 on PORTB. This simplicity is one the benefits to writing code using Arduino C. That is not to say that one is better than the other, but simply that this scalability and flexibility is an oftenoverlooked benefit of learning on the Arduino _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 platform because it allows budding code-monkeys the opportunity to develop into ever more powerful architectures later. We will focus on programming the Arduino using the standard Arduino libraries, although if you want to know more, full documentation on the avr-libc library package can be found at www.nongnu.org/avr-libc/. While we are at it, it is also worth mentioning that it is even possible to program the Arduino interface board using other development environments more often associated with computer development, such as Eclipse, NetBeans, or any other development package that you are familiar with … or if you have an aversion to the color teal. What’s Needed Now that we know why we are learning to write code and generally what this platform offers, we are going to need a little bit of hardware to complete the projects discussed in the rest of this book. Even though we will focus our discussions primarily on programming the Arduino, the hardware is what makes programming the Arduino so much fun. The best place to get started with prototyping electronic circuits with the Arduino is through one of the many starter kits available from various retailers for anywhere from $40 to $100 USD or about €30 to €70 EUR. SparkFun Electronics (www.sparkfun.com) offers the Starter Kit and Inventor’s Kit; Adafruit Industries (www.adafruit.com) the Budget Pack or Starter Pack; MAKE Magazine’s Maker SHED (www.makershed.com) with either a Getting Started Kit or the Mintronics Survival Pack; or the Arduino Store’s (http://store.arduino.cc) Workshop Kit. Table 1-1 provides a simple shopping list of some of the specific hardware that we will use in the following pages, which you might want to purchase individually or at least check whether or not the kit you are considering contains these items. Prices are only approximate and are listed just to give you an idea on the costs. Keep in mind that this is just a general list of recommended parts; feel free to purchase something different—as long as it’s somewhat similar to what’s listed—and to order from vendors of your choice. For example, rather than using an Arduino Uno, the standard Arduino interface board, maybe you’re interested in wearables or e-textiles and would like to use a LilyPad Arduino instead. That’s fine, just be sure to read up on the differences between the recommended hardware and your own on the Arduino Hardware page at http://arduino.cc/en/Main/Hardware. Likewise, many of the items on the list, such as resistors, are kind of like horseshoes in that close enough will usually get the job done. For a more detailed list that includes all the parts used in this book, refer to the expanded hardware list in the appendix at the end of this book. _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 This minimal list of hardware will get you through about half of the book with other specific components necessary for the more advanced projects. When required, information on additional parts will be provided at the start of the chapter or section. While detailed in each chapter, these projects will include the use of temperature sensors, force/flex sensors, light sensors, accelerometers, wind sensors, liquid crystal displays, stepper motors, hobby servos, and other sorts of hardware. Although, rather than mandate a hefty toolbox of equipment, the projects and code will be written with as much flexibility as possible to allow the use of other sensors or actuators, depending on what you have available and your own personal interests and project ideas. To keep things simple, we will power the Arduino Uno from the USB port of our computer using a USB cable for each one of the projects in this book. Once we upload the programs to our boards, however, the program is permanently stored on the board just like a USB flash drive. We could (if we wanted) power the Arduino board from an external power source, such as DC power supply or battery, and not need the USB cable at all. For more information, refer to the section “Connecting & Powering Arduino” on the Arduino Playground at http://arduino.cc/playground/Main/ArduinoCoreHardware. Getting Up and Running By this point, you have a pretty good sense for what the Arduino is, its history, and some of what you can do with it. It’s really exciting that with a few carefully written commands, which you will learn in the upcoming chapters, you can make things light up or move, sense the world around you, and generally make things more fun. And now that you have some basic hardware in hand, including an Arduino _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 interface board, it’s time to get up and running. We need to do the following before moving on to the next chapter: 1. Download and install the Arduino development environment 2. Connect the Arduino board with a USB cable and install drivers 3. Launch the Arduino application and open the Blink example 4. Select your board and serial port 5. Upload your first sketch Don’t worry. It’s not that difficult to get going. In fact, it’s generally hard to go wrong, because it is nearly impossible to burn the house down or cut off an arm with an Arduino. And even if you wire up something wrong, it’s probably okay because the Arduino board is a tough little guy and can take some abuse. So don’t worry that you might mess something up. Of course, we are going to make some mistakes, but hopefully they will teach us something and we will become better programmers and makers because of it. So let’s get on with it. Installing the Software First things first, you need to download and install the Arduino software. Because the Arduino Team is always making updates to the software, you should head to the main download page on the Arduino web site (http://arduino.cc/en/Main/Software), Connecting the Arduino With the software installed, you should be able to connect your Arduino to the USB port on your computer using an A-B USB cable. The Arduino’s power indicator LED will light up on the board, letting us know that it has power and is ready to go. With a brand-new Arduino Uno, the first time that it powers up, the pin 13 LED marked L will begin to blink rapidly, letting us know that the board has been fully tested. On Windows-based PCs or older Arduino boards, it is necessary to install a driver for the Arduino’s onboard USB to Serial convertor. Opening a Sketch Now you can launch the Arduino development environment. This will bring up an empty window if this is your first time out. Open an example sketch by navigating to the File menu ➤ Examples ➤ 1.Basics and select the sketch named Blink. Selecting the Board and Serial Port _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 Before we can upload our sample sketch, we need to select the correct board type and serial port that the board is attached to on our computer. Setting the correct board can be done in the Tools ➤ Board menu by selecting Arduino Uno or one of the other corresponding board names. We need to choose the correct serial port under the Tools ➤ Serial Port menu, as shown in Figure 1- 12. This port should be named COM3, or something similar, on a Windows PC; or something like /dev/tty.usbmodem or /dev.tty.usbserial on the Mac. Uploading a Sketch Once you have selected the proper board and serial port, it’s time for the fun part. To upload a sketch onto the Arduino board, simply hit the Upload button on the toolbar, as shown in Figure 1-13. The onboard LEDs marked RX and TX will blink furiously and you will receive a message in the status bar that says, “Done uploading.” That’s all there is to it! What is Sketching in Code? The idea of sketching in code is a way of thinking about writing code as a simple intuitive process, just like drawing in a sketchbook. In this way, an Arduino program is called a sketch and is saved in a folder called a sketchbook. Sketching means we can get our hands dirty and quickly try out a new idea. It is a skill available to all of us, not just artists and designers, and neither is it limited to pens, paper, pencils, or napkins. The Structure of Arduino C Our first project will provide an interesting backdrop for understanding what’s going on with the basic structure of programming an Arduino sketch. This section will attempt to explain some of the larger points of how sketches are organized and generally work and will apply to every sketch that we might write, not just our previous example. We will get to more on the specifics of functions and statements, including their syntax and parameters, a little later. To begin with, Arduino C follows a set of rules that govern syntax and structure and tell the interface board what to do. Some of these rules are inherited from C and others have been put into place by the Arduino development team to simplify usage for beginners. C is a top-down structural programming language, meaning that lines of code are executed in order from the top of the program until it reaches the end at the bottom. We will adopt this top-down method in our analysis of the sketch for Project 1 to discuss the major points of the Arduino programming syntax and structure, beginning with our first lines of code. Using Comments Looking at our first example, the first three lines contain a small descriptive block of text that we refer to as comments. Comments are areas of text that are ignored by the compiler, but provide useful notes and documentation that give us a little extra information about the sketch to make the code easier to _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 understand for us humans. Use them to provide information about the project that the sketch is written for, about what certain variables do, what information a function needs to work properly, or what will happen if some part of the code is changed. Because comments take up no memory space on the Arduino board, they should be used generously throughout your sketch and are one of those good habits to have that should be adopted early on. Other people, including your future self, will be happy and thank you. Here’s our example: // sets digital pins as outputs Basic Functions The C programming language is broken down into blocks of code called functions that form the main building blocks of any program. In this project we have two functions, setup() and loop(), each containing statements that do specific actions performed in order from top to bottom. As a requirement of the Arduino library, every sketch must at the very least contain both a setup() and loop() function in order to operate, even if they are empty. The following is technically a complete and working Arduino sketch, although it doesn’t do much: void setup() { } void loop() { } While we can and will create our own functions later in the book to handle certain tasks or consolidate blocks of code, if we intend to use one of these two functions we would need to use both of them. Statements and Syntax As mentioned earlier, nestled inside the curly braces of functions are lines of code called statements. Quite simply, these are the parts of the sketch that actually do something, such as calls to other functions, including those functions that are a part of the standard Arduino library, declaring and assigning values to variables, or performing arithmetic operations or comparisons. In our first project, anything in between the curly braces { } is a statement; take the following, for example: digitalWrite(9, HIGH); This line of code is a statement that calls one of the Arduino library’s built-in functions named digitalWrite(), which is used to turn on or off one of the Arduino digital pins previously configured as an output. Verifying and Uploading Taking this concept of sketching to heart by tossing together some lines of code that we are still not fully up to speed on, we’ve got a sketch entered into the Arduino development environment and we think we have all the syntax and statements correct, but now what? In the last chapter, we blindy uploaded our first sketch full of confidence, but how do we know if the code we entered will work or not before uploading it to the Arduino board? To answer these questions, let’s revisit uploading our sketch to the Arduino board, looking a little closer at the verifying and uploading process, including how to save your sketches and some of the common errors we might run into. _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 Verifying As mentioned earlier, it is always a good idea to periodically verify a sketch that you’re working on, just to make sure that you’re on the right track, and even though this happens automatically before uploading a sketch to the Arduino board, it’s a good idea to verify before uploading. To verify our code is to check that our code is correct and follows all the right syntactical rules that our sketches need to follow. We verify our sketch by hitting the Verify button in the top left of the toolbar in the Arduino development environment. Verifying will run the sketch through a process called compiling, where the development environment takes the Arduino C code that we have written and translates it into a binary format that the actual microcontroller will understand. Saving Once you’ve got a working sketch that is ready to be uploaded to your Arduino board, or even if it is still in-progress, you should probably save your file frequently. To save a sketch, all we need to do is hit the Save button in the toolbar. The Arduino development environment saves files in the default sketchbook folder named Arduino, usually found in your Documents folder. Sketch names must contain standard characters and numbers, however, they cannot begin with a number and cannot include a space character. Uploading Once our sketch is successfully compiled and has been safely stored in our sketchbook, we are ready to upload our sketch to the Arduino board by hitting the Upload button in the toolbar Pressing the Upload button will send a signal to the Arduino microcontroller that tells it that it needs to stop what it’s doing and pay attention to the incoming code. Common Errors You’ve just punched in your new sketch with eager anticipation to see that LED blink. You hit the Verify button, and after a long second or two, an error pops up. Now what? If things didn’t go as planned because of a compiler error, the notice bar will turn orange and sometimes give you a cryptic form of message, that basically tells you to go back and fix something. This can be a little disappointing at first, but hang in there, I’m sure you’ll figure it out. There are two general types of errors that we should be concerned with: hardware and syntax. Coming up are steps for troubleshooting these various error codes to help you get going again. General hardware errors occur because something does not match the hardware you have or a connection isn’t made correctly, as in the following example errors: Serial port not found. Problem uploading to board. First, check that your board is connected to your computer and is indicating good power with a lit LED marked ON. With this verified, next check that the correct serial port and the proper board type have _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html DON HONORIO VENTURA STATE UNIVERSITY Cabambangan, Villa de Bacolor 2001, Pampanga, Philippines DHVSU Main Campus, Villa de Bacolor, Pampanga Tel. No. (6345) 458 0021; Fax (6345) 458 0021 Local 211 URL: http://dhvsu.edu.ph ASIA 413 ADVANCED SYSTEM INTEGRATION ARCHITECTURE MODULE 01 both been selected under the Tools menu. If the USB cable is not connected or the wrong serial port has been selected, the development environment will offer to connect using a different serial port. Sometimes a more serious hardware failure will generate similar errors and you will need to talk to your reseller to iron these problems out. _____________________________________________________________________________________ Beginning Arduino Programming (Technology in Action) 1st ed. Edition2011 by Brian Evans www.tutorialspoint.com/arduino/arduino_board_description.html