You are on page 1of 7

UG 106: Praxis I

Undergraduate Program
Handout: Arduino Tutorial Getting Started

September 2012 Semester


Asian Institute of Technology
Instructors: Waqar Shahid, Matthew N. Dailey

Arduino Getting Started Tutorial


Introduction: This tutorial will introduce you to the Arduino board and how to use it. We will also learn
some basics required to implement the equivalent of the Hello World program on the Arduino board. The
hardware version of Hello World is to make an LED (Light Emitting Diode) blink, instead of printing a
message (since we have no terminal to print to).
Credits: Thanks to Pham Huy Nguyen for pointers to Arduino tutorials.

What is Arduino?
The Arduino is a sophisticated single-board computer that can be programmed to sense and control the
physical world around us. It can be used as a dedicated computer for industrial automation or to control a
robot. It can take input from a variety of sensors connected to its input pins. It can process the information
locally or can send data to another computer for remote computation. The processed information can then
be used to make decisions and/or control motors or other actuators connected to the boards output pins.

Hardware
The processing unit on the Arduino is the ATmega328 microcontroller integrated circuit. It uses input and
output ports/pins to communicate with the outside world. Some of the microcontrollers inputs and outputs
are connected to peripherals and other electronic components on the board itself, and others are available
for use as sensor inputs or output to other devices. The microcontroller can be programmed using an open
source programming platform based on the C programming language and a wiring language that is very
easy to use.

(a) Front

(b) Back

Figure 1: Snap-Shot of Arduino Board UNO R3


A summary of the boards specifications follows:

Microcontroller
Operating Voltage
Input Voltage (recommended)
Input Voltage (limits)
Digital I/O Pins
Analog Input Pins
DC Current per I/O Pin
DC Current for 3.3V Pin
Flash Memory
SRAM
EEPROM
Clock Speed

ATmega328
5V
712V
620V
14 (of which 6 provide PWM output)
6
40 mA
50 mA
32 KB (ATmega328) of which 0.5 KB is used by the bootloader
2 KB (ATmega328)
1 KB (ATmega328)
16 MHz

Powering the Arduino Board The board can be powered through the USB connector or through an
external power supply. USB power is sufficient for blinking LEDS and other applications where the total
current required is small. For the external power supply, you can use a battery, an AC-DC adapter with an
appropriate output voltage, or a desktop power supply. The power pins on the board are:
VIN: The input voltage pin for Arduino for external power source. You can supply voltage through
this pin, or, if supplying voltage via the power jack, access it through this pin.
5V: This pin outputs a regulated 5V from the regulator on the board. The board can be supplied
with power either from the DC power jack (712V), the USB connector (5V), or the VIN pin of the
board (7-12V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can damage your
board. Generally, you should not wire a battery or power supply directly to this pin!
3V3: A 3.3V supply generated by the on-board regulator. Maximum current draw is 50 mA.
GND: The ground pin or the negative terminal of your board.

Input and Output: There are 14 digital pins/ports on the Arduino UNO. They all operate at 5V and
can be used for input or output. Each pin can give or receive approximately 40mA of current. There are 6
analog input pins/ports, labeled A0 through A5. Each analog input pin provides 10 bits of resolution (i.e.
1024 different values). By default they measure from ground to 5 volts.
Some of the pins on Arduino UNO board can also be used for other specialized function.
Serial: pin-0 and pin-1 can be configured to be used receiving and transmitting serial data to peripheral
device or computer.
External Interrupts: pin-2 and pin-3 can configured as external interrupt.
PWM: pin-3, pin-5, pin-6, pin-9, pin-10, pin-11 can be configured as Pulse width modulation (PWM)
outputs.
SPI: pin-10 (SS), pin-11 (MOSI), pin-12 (MISO), pin-13 (SCK) pins can be configured for serial
communication using SPI protocol.
LED: An SMD-LED is connected to pin-13 of the C. When the pin has logic-high, the LED is on,
and when it is logic-low it is off.
TWI: pin-A4 (SDA) and pin-A5 (SCL) pin support TWI communication.

A couple of other pins on the board are:


AREF: Reference voltage for analog input. By default the reference voltage is from 0 5 Volts.
Reset: To reset the micro-controller.

Further details and Schematics of the open-source board is available at


http://arduino.cc/en/uploads/Main/arduino_Uno_Rev3-02-TH.zip.
http://arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf.
http://arduino.cc/en/Main/ArduinoBoardUno.

Software
The Arduino Uno can be programmed with the open-source Arduino software. The programming platform
enables you to write and edit your program in wiring-language. It can cross compile your code for ATmega328.
It can also upload your program using the same software.
The C on the Arduino UNO comes with a pre-burned boot-loader, which allows to upload new code
to it without the use of an external hardware programmer. Instead of using a physical Reset button the
Arduino Uno is designed in a way that allows it to be reset by software running on a connected computer.
The Arduino programs can be categorized in 3 parts,structure, values and functions. For details on
programming please refer to http://arduino.cc/en/Reference/HomePage. The basic structure of the
programming language is simple. It will always contain 2 parts/functions, one is the setup() function,
which is the preparation part. The other is the loop() function which is the execution part. Therefore a
simple sketch of your program will look like as:
void setup()
{
\\statements here
}
void loop()
{
\\statements here
}

Arduino Software Platform Installation


We now go through a step-by-step instruction to install Arduino programming platform on our laptop and
to communicate it with the Arduino UNO R3.
1. Go to http://arduino.cc/en/Main/Software to choose and download the compressed packaged based
on your Operating System. For me, I prefer Ubundu (Linux) operating system. It is the best, if you
want to play with the hardware.
2. Based on your operating system selection a zipped file will be downloaded on your machine.
3. For Linux, Unzip the file in a folder.
4. Open a new Terminal in Linux and go to the folder you unzipped.
5. Run ./arduino shell script on your terminal and it will open the Arduino platform on your machine.
6. Connect one end of Arduino board USB cable with your machine
You are now ready to write, compile and upload your program for Arduino board.

Hello World- Blinking LED


For this program we need the hardware given as follows:
1. Arduino UNO R3
2. Bread-board
3. Jumper (connecting wires)
4. Light emitting diode (LED)
5. Resistor
Identifying the positive and the negative terminal of the LED is important before implementing the
circuit. Figure 3 shows the table to identify the value of resistor without the use of a multi-meter.

Figure 2: Equipment and Components Required, reprinted from Beginning to Android with Arduino
You can connect the circuit as shown in the Figure 4a,4b. You can see that the LED is connected to
pin-13 and to ground via 220 resistor. The resistor is there to control the current in the LED. Now, you
are ready to write your code to program ATmega328.
Referring to the installation of the Arduino software, once you run the shell script in Ubuntu or the open
the executable in Windows, you will see the Arduino IDE as shown in the Figure 5.
The Blinking LED program is part of the public domain examples program as given on th arduino
website. You can directly open the program from menu on the top left corner by following the hierarchy,
File-Examples-Basics-Blink. A program will be opened in a separate window and will look like as follows:
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.

Figure 3: Resistor Coloring Code, reprinted from http://www.elexp.com/t_resist.htm

(a) Schematics generated by Fritzing-0.7.7b

(b) Circuit Diagram generated by Fritzing-0.7.7b

Figure 4: Circuit and Schematics for Blinking LED

// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);

Figure 5: Arduino IDE for cross-platform compilation

}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH);
// turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(led, LOW);
// turn the LED off by making the voltage LOW
delay(1000);
// wait for a second
}

Press cntrl-R to compile the code. If there is no errors, then press cntrl-U to upload the compiled
program on your C Flash memory. As told earlier, once the program is uploaded successfully, the board
will automatically get a reset and after a few second will start blinking your LED.

Figure 6: Figure generated by Fritzing, taken from http://arduino.cc/en/Tutorial/Button

Exercise
Now, the next assignment is to implement a digital switch using the board.
We need 1 digital on-off switch and 1- 10 resistor and some more connecting wires.
We will also make use of the 5V input pin of the board and pin-2 as digital input pin.

Connect the circuit as shown in figure 6.


Use the function pinMode() to declare the mode of pin-2.
Use the if statement to check the condition of the digital switch or if the digital input pin is High.
If the input is HIGH then Blink the LED, otherwise, the LED should not blink.
Use digitalRead() function to check the condition of the pin-2.

References
http://arduino.cc.
http://arduino.cc/en/Main/ArduinoBoardUno.
http://arduino.cc/en/Guide/Introduction.
www.atmel.com/Images/doc8161.pdf.
Arduino Programming Notebook by - by Brian W. Evans.
Beginning Android ADK with Arduino by Mario Bohmer
http://arduino.cc/en/Tutorial/Button
http://arduino.cc/en/Tutorial/Blink