25% developed

A Beginner's Arduino Guide/Printable version

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Introduction

Who is this book for?[edit | edit source]

This book is intended as an introductory guide for getting set up in Arduino. It is intended as a guide for anyone who wants to program microcontrollers, but doesn't have any background. However, Arduino involves electronics and programming, so an understanding on these would be very helpful before starting.


What is Arduino?

What, Why Arduino?[edit | edit source]

The starting point, the Arduino Uno

The Arduino contains a microcontroller. Microcontrollers evolved from the same microprocessors that started the PC revolution such as the Motorola 6502 and Intel 8088. Microcontrollers differ from modern CPUs in that IO, RAM, and Flash memory are imbedded in a single chip. They are found in embedded applications such as the control panel of a microwave, printer or car dashboard. Microcontrollers exist in a variety of configurations and price points starting at under two dollars for quantity one. Two popular microcontroller companies used in the DIY community are AMTEL and Microchip PIC. They are popular because they give away the software development environment (C language) for free and they scale up to supporting Linux.

The Arduino is open source hardware. It contains chips from for profit companies. The open source features are:

  • The form factor: the way wires attach to it, the way add on products stack on top of it
  • The circuit board
  • The circuit that connects the chips
  • Standard boot loader (what happens when you turn it on)

The Arduino evolved out of the open source Wiring project, which was inspired by the Processing programming language. The Arduino was created at the Interaction Design Institute in Ivrea Italy (where Olivetti is located) in 2003 in almost one evening (24 minute video link). The goal of Wiring is to create an single board microcomputer, a programming language and an IDE that caters to artists. The Arduino is a success for several reasons:

  • Surface mount chips are hard to solder. The DIY community needed to move away from designing circuits, building unique big circuit boards and soldering chips. They needed to move to small standard circuit boards that are stacked on top of each other.
  • Test driven software development
  • Example driven software learning
  • Active community expanding examples
  • Physical feedback through lights, motors, sounds of software success
  • Price points, competition and branding necessary for economic success
Stickers and Documentation that comes with the Uno

From the 1990's through the 2000's, both universities and companies came up with new microcontrollers targeting the DIY educational market. The starting price point had been around $300, putting it out of reach of any student not seriously interested in microcontrollers. The Arduino lowered this to around $30. This not only made competitors in the education market reduce their prices, it expanded the consumer base interested in such products greatly, expanding the market for more capable microcontrollers too.

In summary the Arduino has a lower starting point in both cost, experience and expectations. Yet it scales up to the dominate professional software development environments used by electrical engineers to create circuits and computer engineers to build systems. It also scales up to enabling complicated open source, Linux applications such as the robot operating system (ROS) and autopilots to travel in battery operated robots.

Different Arduino models

The term "Arduino" refers to a family of controllers that use the same software but have different target applications and form factors. The rest of this document refers to the Uno Arduino specifically.

Shields stacked on top of Uno

Shields[edit | edit source]

The Arduino Uno's function is expanded by sandwiching different circuit boards on top of it. These circuit boards are called shields. New shields spark creativity, demand and profit. Pictured are a USB Host Shield .. which enables the Arduino to act like a PC rather than the device being plugged into the PC.

USB Host Shield

To the right is a picture of an Uno (blue) with a USB Host Shield (red) and motor Shield (green) stacked on top of it. The USB Host Shield and UNO have stackable or female headers. The motor shield has been made with breakaway or male headers that prevent stacking of more shields.

Motor Shield

Connections[edit | edit source]

Power[edit | edit source]

The Uno can be powered through the USB cable. Eventually adding stuff to the Uno will cause everything to stop working. The solution is to plug in a 5 to 12 volt DC in cable to the Uno. Things will then work, more will be added and eventually the Uno will stop working again. At this point the individual shields may need their own power sources. This is especially true if motors are involved.

The USB cable is designed to be attached to a computer so that software can be downloaded to the Arduino. Once the program is downloaded, the USB cable can be disconnected (if another power source is available) and the program will run without a computer.

USB[edit | edit source]

A USB mouse or keyboard can not be plugged into the Arduino as if it were a computer. The USB port on an Uno is a slave port, not the USB On-The-Go port found in cell phones. The Uno needs a USB host to talk to. Keyboards and mice are slaves. Two slaves can not talk to each other. That is why a USB Host shield for the Arduino exists.

6 Analog In Pins[edit | edit source]

Six pins (or holes where pins go) are clearly labeled on the Uno. EEG, ECG, temperature sensors, light sensors, and strain gauges are examples of devices that turn physical measured information into a voltage. The Uno can turn the voltage into a number between 0 and 1023 through the Analog pins. The maximum voltage defaults to 5 volts, but can be changed to 1.1 or 3.3 volts through software.

AREF[edit | edit source]

The word "AREF" on the Uno points a pin. AREF stands for Analog REFerence. A different maximum voltage for the Analog pins can be connected here and then selected through software. The maximum voltage is 5 volts. 1.1 and 3.3 volts can be selected through software. Anything different will have to be connected to the AREF pin. Doing this enables the Uno to be more accurate when converting information from the 6 analog pins to numbers. Uno Connections

Digital Pins[edit | edit source]

The digital pins put out either 0 or 5 volts. They can turn off and on LEDs. Or they could tell a fax machine LCD panel what to say. Pins 3,5,6,9,10,11 are PWM or Pulse Width Modulation pins, meaning that the time on and time off do not have to equal. This enables the control of Electronic Speed Controllers (ESC). This enables an Uno to change the speed of helicopter propellers. Pins 0 and 1 stand for transmit and receive ... with anything including another UNO. This for example would enable one UNO to talk to another.

Shield pins[edit | edit source]

The Uno pins move up through the headers and thus are available to all the shields. The shields themselves may have additional features. There is a protoboard shield that adds no features other than providing a platform on which to build unique circuits.

The Competition[edit | edit source]

The Arduino has spun off a number of competing open source hardware projects. The uno header pin out and compatibility with Arduino software are the two major features of all Arduino boards.

Many companies are making "almost compatible" Arduino products like the Pinguino. Some feature the pic processor rather than the arduino's Atmel. Others like the papilio (based on Xilinix FPGA) don't claim software compatibility, just hardware compatibility with the UNO header pinout.

Some projects require running a Linux distribution on the device that is being built. The Raspberry Pi and pcduino are examples of these.


Where to get Arduino?

There are several types of Arduino controllers:

Genuine Arduino Controllers[edit | edit source]

An official Arduino Uno R3. These boards are made in Italy, and support the Arduino developers. The baseline boards, these have the best compatibility with the Arduino IDE, and has a large amount of community documentation and support to accompany them.

These controllers usually cost about $25 USD. Most of the funding goes to support the Arduino organization. We strongly encourage you to purchase at least one Genuine Arduino or make a contribution to the project.

Genuine controllers often work best with the Arduino software. They use common serial controller chips such as the FTDI chip that have certified signed drivers for both the PC and Mac.

Arduino Compatibles[edit | edit source]

A SparkFun RedBoard Plus. This board is generally compatible with the Arduino Uno design, but swaps a USB Type B port for a USB Type C port, among other changes.

Arduino compatibles are distinct from clones, in that they usually have distinct capabilities and significantly different hardware from official Arduino models. Arduino compatibles typically have their own branding, and don't try to pass themselves off as official Arduinos.

Here are some common features compatibles add or change compared to official Arduinos:

  • Different microcontroller (More power efficient, more powerful, different architecture, etc)
  • Different USB or serial port.
  • Solderless expansion connectors.
  • Built in Wireless radio for Wi-Fi, Bluetooth, etc.
  • Built in sensors.
  • Different formfactors.

Arduino Clones[edit | edit source]

An Arduino Nano clone. Cheap and easy to come by, the low cost of clones means they are often used in projects requiring a large number of boards, harsh environments where the board can be expected to be destroyed or be irretrievable, or otherwise uses where long term quality is not a concern. Another use for clones is simply to use as the more permanent base for a project, freeing up a more premium dev board for a new project.

Because Arduino is open source, there are a number of Arduino clones that simply use official designs with either no modification, or modifications exclusively for cost reductions. These clones are typically unbranded, though less scrupulous sellers may try to pass off counterfeits falsely labeled as official Arduino boards.

These are typically around $5, however you can find some Arduino Nano versions for as little as $2.27 (includes shipping). eBay is a popular place to purchase Arduino controllers, however be warned that many of the suppliers are in Hong Kong and China, and shipping times can be longer then usual. Allow one or two weeks for shipping at a minimum. Don't be shocked if a clone unit is a dud or acts funky - it comes with the territory.

The lower-cost Arduino Clones use a lower-cost serial interface chip such as the CH340D. The drivers for these chips are not included in the Arduino IDE. You must install these drivers manually.


Arduino Sketch
Arduino IDE with blink program open - a common sketch.

Arduino Sketches are lines of code that the user of an Arduino device writes in easy to understand language to tell the Arduino what to do.

These lines of code are then converted to machine language (complex language that the Arduino can understand) and stored (uploaded) into the Arduino device.

Writing and uploading sketches are usually done using an Integrated Development Environment (IDE) e.g the Arduino IDE (an all-in-one piece of software for working with Arduino devices)

The Arduino sketches are written by default using the Arduino language which has its unique keywords, expressions and rules.

Arduino Sketch Structure[edit | edit source]

The Arduino Sketch is made up of two parts 'Setup' and 'Loop'

The setup configures (adjusts the settings of) the Arduino device while the loop contains the actions that the user wants the Arduino to perform

Arduino Language

The Arduino Language is the set of words, expressions and rules that are used to create Arduino Sketches.

The Arduino Language is based on C and C++

The Arduino Language allows for the use of any functions from the 'AVR Libc' which is a subset of the standard C library for Atmel AVR 8-bit RISC micro-controllers

The language is made up of keywords that are written based on rules.

Arduino Keywords[edit | edit source]

The keywords used to create the sketches can be divided into three types- Functions ,Structures and Values ( Values can be either Constants or Variables).

Below are a non-exhaustive list of standard Arduino sketch words, to add words, new libraries can be imported into the IDE.

Functions[edit | edit source]

Functions are small groups of code that do a single task or group of tasks (they have one function). They include

Category Keyword
Digital I/O
  • pinMode()
  • digitalWrite()
  • digitalRead()
Analog I/O
  • analogReference()
  • analogRead()
  • analogWrite() - PWM
Due & Zero only
  • analogReadResolution()
  • analogWriteResolution()
Advanced I/O
  • tone()
  • noTone()
  • shiftOut()
  • shiftIn()
  • pulseIn()
Time
  • millis()
  • micros()
  • delay()
  • delayMicroseconds()
Math
  • min()
  • max()
  • abs()
  • constrain()
  • map()
  • pow()
  • sqrt()
Trigonometry
  • sin()
  • cos()
  • tan()
Characters
  • isAlphaNumeric()
  • isAlpha()
  • isAscii()
  • isWhitespace()
  • isControl()
  • isDigit()
  • isGraph()
  • isLowerCase()
  • isPrintable()
  • isPunct()
  • isSpace()
  • isUpperCase()
  • isHexadecimalDigit()
Random Numbers
  • randomSeed()
  • random()
Bits and Bytes
  • lowByte()
  • highByte()
  • bitRead()
  • bitWrite()
  • bitSet()
  • bitClear()
  • bit()
External Interrupts
  • attachInterrupt()
  • detachInterrupt()
Interrupts
  • interrupts()
  • noInterrupts()
Communication
  • Serial
  • Stream
USB (32u4 based boards and Due/Zero only)
  • Keyboard
  • Mouse

Structures these are words used to create a flow or structure in the program. they include

Category Keyword
Overall Structure
  • setup()
  • loop()
Control Structures
  • if
  • if...else
  • for
  • switch case
  • while
  • do... while
  • break
  • continue
  • return
  • goto
Arithmetic Operators
  • = (assignment operator)
  • +  (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (modulo)
Comparison Operators
  • == (equal to)
  • != (not equal to)
  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)
Boolean Operators
  • && (and)
  • || (or)
  • ! (not)
Bitwise Operators
  • & (bitwise and)
  • | (bitwise or)
  • ^ (bitwise xor)
  • ~ (bitwise not)
  • << (bitshift left)
  • >> (bitshift right)
Compound Operators
  • ++ (increment)
  • -- (decrement)
  • += (compound addition)
  • -= (compound subtraction)
  • *= (compound multiplication)
  • /= (compound division)
  • %= (compound modulo)
  • &= (compound bitwise and)
  • |= (compound bitwise or)
Pointer Access Operators
  • * dereference operator
  • & reference operator
Syntax
  • ; (semicolon)
  • {} (curly braces)
  • // (single line comment)
  • /* */ (multi-line comment)
  • #define
  • #include

Values[edit | edit source]

These act like containers and hold a certain value they may be constant which implies a container whose value that doesn't change throughout the program or a variable, which implies a container whose value that does change.

Category Keyword
Constants
  • HIGH | LOW
  • INPUT | OUTPUT | INPUT_PULLUP
  • LED_BUILTIN
  • true | false
  • integer constants
  • floating point constants
Data Types
  • void
  • boolean
  • char
  • unsigned char
  • byte
  • int
  • unsigned int
  • word
  • long
  • unsigned long
  • short
  • float
  • double
  • string - char array
  • String - object
  • array
Conversion
  • char()
  • byte()
  • int()
  • word()
  • long()
  • float()
Variable Scope & Qualifiers
  • variable scope
  • static
  • volatile
  • const
Utilities
  • sizeof()
  • PROGMEM