IV - Programming With Arduino
IV - Programming With Arduino
Since the launch of the Arduino open-source platform, the brand has established
themselves at the center of an expansive open-source community. The Arduino
ecosystem is comprised of a diverse combination of hardware and software. The
versatility of Arduino and its simple interface makes it a leading choice for a wide range
of users around the world from hobbyists, designers, and artists to product prototypes.
The Arduino board is connected to a computer via USB, where it connects with the
Arduino development environment (IDE). The user writes the Arduino code in the IDE,
then uploads it to the microcontroller which executes the code, interacting with inputs
and outputs such as sensors, motors, and lights.
What is Arduino?
Arduino is an open-source electronics platform based on easy-to-use hardware and
Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing
something online. You can tell your board what to do by sending a set of instructions to the
Over the years Arduino has been the brain of thousands of projects, from everyday objects to
programmers, and professionals - has gathered around this open-source platform, their
Arduino was born at the Ivrea Interaction Design Institute as an easy tool for fast prototyping,
differentiating its offer from simple 8-bit boards to products for IoT applications, wearable, 3D
printing, and embedded environments. All Arduino boards are completely open-source,
empowering users to build them independently and eventually adapt them to their particular
needs. The software, too, is open-source, and it is growing through the contributions of users
worldwide.
Why Arduino?
Thanks to its simple and accessible user experience, Arduino has been used in thousands of
different projects and applications. The Arduino software is easy-to-use for beginners, yet
flexible enough for advanced users. It runs on Mac, Windows, and Linux. Teachers and students
use it to build low-cost scientific instruments, to prove chemistry and physics principles, or to get
started with programming and robotics. Designers and architects build interactive prototypes,
musicians and artists use it for installations and to experiment with new musical instruments.
Makers, of course, use it to build many of the projects exhibited at the Maker Faire, for example.
Arduino is a key tool to learn new things. Anyone - children, hobbyists, artists, programmers -
can start tinkering just following the step-by-step instructions of a kit, or sharing ideas online
There are many other microcontrollers and microcontroller platforms available for physical
computing. Parallax Basic Stamp, Netmedia's BX-24, Phidgets, MIT's Handyboard, and many
others offer similar functionality. All of these tools take the messy details of microcontroller
programming and wrap it up in an easy-to-use package. Arduino also simplifies the process of
working with microcontrollers, but it offers some advantage for teachers, students, and interested
microcontroller platforms. The least expensive version of the Arduino module can be
assembled by hand, and even the pre-assembled Arduino modules cost less than \$50
OSX, and Linux operating systems. Most microcontroller systems are limited to
Windows.
easy-to-use for beginners, yet flexible enough for advanced users to take advantage of
can be expanded through C++ libraries, and people wanting to understand the
technical details can make the leap from Arduino to the AVR C programming
language on which it's based. Similarly, you can add AVR-C code directly into your
make their own version of the module, extending it and improving it. Even relatively
Arduino boards senses the environment by receiving inputs from many sensors, and affects their
surroundings by controlling lights, motors, and other actuators. Arduino boards are the
microcontroller development platform that will be at the heart of your projects. When making
something you will be building the circuits and interfaces for interaction, and telling the
microcontroller how to interface with other components. Here the anatomy of Arduino Uno.
1. Digital pins Use these pins with digitalRead(), digitalWrite(), and
analogWrite(). analogWrite() works only on the pins with the PWM symbol.
handy target for your first blink sketch, this LED is very useful for debugging.
debugging.
6. GND and 5V pins Use these pins to provide +5V power and ground to
your circuits.
7. Power connector This is how you power your Arduino when it's not
plugged into a USB port for power. Can accept voltages between 7-12V.
Arduino and your computer. Expect them to flicker rapidly during sketch upload as
to your Arduino, and for communicating with your Arduino sketch (via Serial.
println() etc.).
The text of the Arduino getting started guide is licensed under a Creative Commons Attribution-
ShareAlike 3.0 License. Code samples in the guide are released into the public domain.
Programs written using Arduino Software (IDE) are called sketches. These sketches are written
in the text editor and are saved with the file extension. ino. The editor has features for
cutting/pasting and for searching/replacing text. The message area gives feedback while saving
and exporting and also displays errors. The console displays text output by the Arduino Software
(IDE), including complete error messages and other information. The bottom righthand corner of
the window displays the configured board and serial port. The toolbar buttons allow you to verify
and upload programs, create, open, and save sketches, and open the serial monitor.
NB: Versions of the Arduino Software (IDE) prior to 1.0 saved sketches with the extension.
pde. It is possible to open these files with version 1.0, you will be prompted to save the
Upload Compiles your code and uploads it to the configured board. See uploading below for
details.
Note: If you are using an external programmer with your board, you can hold down the "shift"
key on your computer when using this icon. The text will change to "Upload using Programmer"
Open Presents a menu of all the sketches in your sketchbook. Clicking one will open it within the
Note: due to a bug in Java, this menu doesn't scroll; if you need to open a sketch late in the list,
Additional commands are found within the five menus: File, Edit, Sketch, Tools, Help. The
menus are context sensitive, which means only those items relevant to the work currently being
File
New Creates a new instance of the editor, with the bare minimum structure of a
folders.
Open Recent Provides a short list of the most recent sketches, ready to be opened.
Sketchbook Shows the current sketches within the sketchbook folder structure;
clicking on any name opens the corresponding sketch in a new editor instance.
Examples Any example provided by the Arduino Software (IDE) or library shows
up in this menu item. All the examples are structured in a tree that allows easy access
by topic or library.
Close Closes the instance of the Arduino Software from which it is clicked.
Save Saves the sketch with the current name. If the file hasn't been named before,
Save as... Allows to save the current sketch with a different name.
Print Sends the current sketch to the printer according to the settings defined in
Page Setup.
Preferences Opens the Preferences window where some settings of the IDE may
Edit
Undo/Redo Goes back of one or more steps you did while editing; when you go
Cut Removes the selected text from the editor and places it into the clipboard.
Copy Duplicates the selected text in the editor and places it into the clipboard.
Copy for Forum Copies the code of your sketch to the clipboard in a form suitable
Copy as HTML Copies the code of your sketch to the clipboard as HTML,
Paste Puts the contents of the clipboard at the cursor position, in the editor.
Select All Selects and highlights the whole content of the editor.
selected line, moving the text one space on the right or eliminating a space at the
beginning.
Find Opens the Find and Replace window where you can specify text to search
Find Next Highlights the next occurrence - if any - of the string specified as the
Find Previous Highlights the previous occurrence - if any - of the string specified
as the search item in the Find window relative to the cursor position.
Sketch
Verify/Compile Checks your sketch for errors compiling it; it will report memory
Upload Compiles and loads the binary file onto the configured board through the
configured Port.
Upload Using Programmer This will overwrite the bootloader on the board; you
will need to use Tools > Burn Bootloader to restore it and be able to Upload to USB
serial port again. However, it allows you to use the full capacity of the Flash memory
for your sketch. Please note that this command will NOT burn the fuses. To do so a
the start of your code. For more details, see libraries below. Additionally, from this
menu item you can access the Library Manager and import new libraries from .zip
files.
Add File... Adds a supplemental file to the sketch (it will be copied from its
current location). The file is saved to the data subfolder of the sketch, which is
intended for assets such as documentation. The contents of the data folder are not
Tools
Auto Format This formats your code nicely: i.e. indents it so that opening and
closing curly braces line up, and that the statements inside curly braces are indented
more.
Archive Sketch Archives a copy of the current sketch in .zip format. The archive is
Fix Encoding & Reload Fixes possible discrepancies between the editor char map
data with any connected board on the currently selected Port. This usually resets the
Board Select the board that you're using. See below for descriptions of the various
boards.
Port This menu contains all the serial devices (real or virtual) on your machine. It
should automatically refresh every time you open the top-level tools menu.
chip and not using the onboard USB-serial connection. Normally you won't need this,
but if you're burning a bootloader to a new microcontroller, you will use this.
Burn Bootloader The items in this menu allow you to burn a bootloader onto the
Arduino board but is useful if you purchase a new ATmega microcontroller (which
normally come without a bootloader). Ensure that you've selected the correct board
from the Boards menu before burning the bootloader on the target board. This
Help
Here you find easy access to a number of documents that come with the Arduino Software
(IDE). You have access to Getting Started, Reference, this guide to the IDE and other documents
locally, without an internet connection. The documents are a local copy of the online ones and
Find in Reference This is the only interactive function of the Help menu: it
directly selects the relevant page in the local copy of the Reference for the function or
Sketchbook
The Arduino Software (IDE) uses the concept of a sketchbook: a standard place to store your
programs (or sketches). The sketches in your sketchbook can be opened from the File >
Sketchbook menu or from the Open button on the toolbar. The first time you run the Arduino
software, it will automatically create a directory for your sketchbook. You can view or change
the location of the sketchbook location from with the Preferences dialog.
Beginning with version 1.0, files are saved with a .ino file extension. Previous versions use
the .pde extension. You may still open. pde named files in version 1.0 and later, the
Allows you to manage sketches with more than one file (each of which appears in its own tab).
These can be normal Arduino code files (no visible extension), C files (.c extension), C++ files
Before compiling the sketch, all the normal Arduino code files of the sketch (.ino, .pde) are
concatenated into a single file following the order the tabs are shown in. The other file types are
left as is.
Uploading
Before uploading your sketch, you need to select the correct items from the Tools > Board and
Tools > Port menus. The boards are described below. On the Mac, the serial port is probably
adapter). On Windows, it's probably COM1 or COM2 (for a serial board) or COM4, COM5,
COM7, or higher (for a USB board) - to find out, you look for USB serial device in the ports
or similar. Once you've selected the correct serial port and board, press the upload button in the
toolbar or select the Upload item from the Sketch menu. Current Arduino boards will reset
automatically and begin the upload. With older boards (pre-Diecimila) that lack auto-reset, you'll
need to press the reset button on the board just before starting the upload. On most boards, you'll
see the RX and TX LEDs blink as the sketch is uploaded. The Arduino Software (IDE) will
When you upload a sketch, you're using the Arduino bootloader, a small program that has been
loaded on to the microcontroller on your board. It allows you to upload code without using any
additional hardware. The bootloader is active for a few seconds when the board resets; then it
starts whichever sketch was most recently uploaded to the microcontroller. The bootloader will
blink the on-board (pin 13) LED when it starts (i.e. when the board resets).
Libraries
Libraries provide extra functionality for use in sketches, e.g. working with hardware or
manipulating data. To use a library in a sketch, select it from the Sketch > Import Library
menu. This will insert one or more #include statements at the top of the sketch and compile the
library with your sketch. Because libraries are uploaded to the board with your sketch, they
increase the amount of space it takes up. If a sketch no longer needs a library, simply delete its
There is a list of libraries in the reference. Some libraries are included with the Arduino software.
Others can be downloaded from a variety of sources or through the Library Manager. Starting
with version 1.0.5 of the IDE, you do can import a library from a zip file and use it in an open
Third-Party Hardware
Support for third-party hardware can be added to the hardware directory of your sketchbook
directory. Platforms installed there may include board definitions (which appear in the board
menu), core libraries, bootloaders, and programmer definitions. To install, create the hardware
directory, then unzip the third-party platform into its own sub-directory. (Don't use "arduino" as
the sub-directory name or you'll override the built-in Arduino platform.) To uninstall, simply
For details on creating packages for third-party hardware, see the Arduino Platform specification.
Serial Monitor
This displays serial sent from the Arduino board over USB or serial connector. To send data to
the board, enter text and click on the "send" button or press enter. Choose the baud rate from the
drop-down menu that matches the rate passed to Serial.begin in your sketch. Note that on
Windows, Mac or Linux the board will reset (it will rerun your sketch) when you connect with
the serial monitor. Please note that the Serial Monitor does not process control characters; if your
sketch needs a complete management of the serial communication with control characters, you
can use an external terminal program and connect it to the COM port assigned to your Arduino
board.
You can also talk to the board from Processing, Flash, MaxMSP, etc (see the interfacing page for
details).
Preferences
Some preferences can be set in the preferences dialog (found under the Arduino menu on the
Mac, or File on Windows and Linux). The rest can be found in the preferences file, whose
Language Support
Since version 1.0.1 , the Arduino Software (IDE) has been translated into 30+ different
languages. By default, the IDE loads in the language selected by your operating system. (Note:
on Windows and possibly Linux, this is determined by the locale setting which controls currency
and date formats, not by the language the operating system is displayed in.)
If you would like to change the language manually, start the Arduino Software (IDE) and open
the Preferences window. Next to the Editor Language there is a dropdown menu of currently
supported languages. Select your preferred language from the menu, and restart the software to
use the selected language. If your operating system language is not supported, the Arduino
You can return the software to its default setting of selecting its language based on your
operating system by selecting System Default from the Editor Language drop-down. This
setting will take effect when you restart the Arduino Software (IDE). Similarly, after changing
your operating system's settings, you must restart the Arduino Software (IDE) to update it to the
Boards
The board selection has two effects: it sets the parameters (e.g. CPU speed and baud rate) used
when compiling and uploading sketches; and sets and the file and fuse settings used by the burn
bootloader command. Some of the board definitions differ only in the latter, so even if you've
been uploading successfully with a particular selection, you'll want to check it before burning the
bootloader. You can find a comparison table between the various boards here.
Arduino Software (IDE) includes the built-in support for the boards in the following list, all
based on the AVR Core. The Boards Manager included in the standard installation allows to add
support for the growing number of new boards based on different cores like Arduino Due,
ATmega328P running at 16 MHz with auto-reset, 6 Analog In, 14 Digital I/O and 6
PWM.
Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328P, 6 Analog In, 14 Digital I/O
and 6 PWM.
burned (4 KB) includes codes to initialize the on-board bluetooth module, 6 Analog
Duemilanove w/ ATmega168, but the bootloader burned has a slower timeout (and
blinks the pin 13 LED three times on reset); 6 Analog In, 14 Digital I/O and 6 PWM.