Arduino Seminar/Workshop
Foundations
ENGR. JIMMY CARTER B. NILO
REGISTERED ELECTRONICS ENGINEER
MASTERS OF SCIENCE IN ELECTRONICS & COMMUNICATIONS
ENGINEERING
Based from e-Gizmo presentation. “What is a Microcontroller”
Ian
Topics
1. Introduction
2. Getting Started
3. Sketch Fundamentals
Introduction
What is Arduino?
Arduino is an open-source physical computing
platform based on a simple microcontroller board,
and a development environment for writing software
for the board.
Arduino comes with its own open-source Integrated
Development Environment (IDE).
Why Arduino?
Inexpensive
Cross-platform
Simple, clear programming environment
Open-source and extensible software
Open-source and extensible hardware
/ 34
Arduino Specifications
Microcontroller ATmega1681
Operating Voltage 5V
Input Voltage (recommended) 7-12 V
Input Voltage (limits) 6-20 V
Digital I/O Pins 14 (of which 6 provide PWM
output)
Analog Input Pins 6
DC Current per I/O Pin 40 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 16 KB (of which 2 KB used by
bootloader)
Integrated Development Environment
The Arduino development environment contains a text
editor for writing code, a message area, a text console,
a toolbar with buttons for common functions, and a
series of menus. It connects to the Arduino hardware
to upload programs and communicate with them.
It can be downloaded at
https://www.arduino.cc/en/Main/Donate
Integrated Development Environment
Figure 1: Arduino IDE ( Old Version )
2010 8 / 34
Figure 2: Arduino IDE ( Latest Version )
Getting Started
Materials Needed:
We will now start to use Arduino by downloading a sketch.
Make sure you have the following:
Arduino/Gizduino Board
USB Cable
Figure 3: Arduino Board
Figure 4: USB Cable
Arduino Parts
Figure 5: Arduino Uno Parts
Connect Arduino to PC
Connect the Arduino/Gizduino board to your computer using
the USB cable. The green power LED should go on. Make
sure that the power pin selector is placed at USB mode as
shown in the picture.
Figure 5: Green power LED
2010 11 / 34
Launching Arduino IDE
Double click the Arduino application
Figure 6: Arduino Icon
Opening a Sketch
Open the Blink example sketch found at your examples
folder.
Setting up Arduino IDE
Go to Tools -> Board menu and select Arduino Uno
( Depends on the Board Model )
Select the serial device of the Arduino board from the
Tools -> Serial Port menu.
To find out, you can disconnect your Arduino
board and re-open the menu; the entry that disappears
should be the Arduino board. Reconnect the board and
select that serial port.
Uploading Program
Click “Verify“ button to check your code for syntax errors.
After compiling, click “Upload” button in the
environment. If the upload is successful, the message
"Done uploading" will appear in the status bar.
Figure7: Upload Button
A few seconds after the upload finishes, you should see the
pin 13 (Lit) LED on the board start to blink (in orange). If it
does, congratulations! You've gotten Arduino up-and-
running
10 16 / 34
Sketch Fundamentals
We will now analyze the Blink code that we uploaded earlier. In
this section, we will learn the following:
1 Sketch
2 Comments
3 Variables
4 Functions
5 Control Structures
2010 17 / 34
What is a Sketch?
Definition
Sketch
It is the unit of code that is uploaded to and run on an
Arduino board.
Example
Blink
The sketch we uploaded earlier
Comments
Comments are ignored by the Arduino when it runs the
sketch.
It is there for people reading the code: to explain what
the program does, how it works, or why it's written the
way it is.
For multi-line comment use /* and */.
For single-line comment use //.
Comment Example
Multi-line Comment Example
/*
Blink
Turns on an LED on for one second, then off for one second,
repeatedly.
...
*/
Single-line Comment Example
// LED connected to digital pin 13
/ 34
Variables
Definition
Variable
Place for storing a piece of data
Variable has a name, a value, and a type.
The following example shows how to create a variable whose
name is pin, whose value is 13, and whose type is int.
Variable Example
int pin = 13;
Variable Example
You may now refer to this variable, by its name, at which
point its value will be looked up and used.
Variable Example
In the statement:
pinmode(pin, OUTPUT);
It would be equivalent to the following statement:
pinmode(13, OUTPUT);
ThinkLab (Manila) Arduino Workshop 2010 22 / 34
More Variable Examples
Arithmetic Expressions
int counter; /*variable names should always start with a letter*/
counter = 55; //counter is equal to 55 counter++; //increment
counter by 1, counter = 56 counter--; //decrement counter by
1, counter = 55
counter = counter - 25; */decrease counter by 25, counter = 30*/
counter += 25; //increase counter by 25, counter = 55
ThinkLab (Manila) Arduino Workshop 2010 23 / 34
Boolean Logic
Definitions
AND
Symbolized by & (&& if conditional)
Returns True only if all inputs are True, otherwise False.
OR
Symbolized by | (|| if conditional)
Returns True if one or more inputs are True, otherwise False.
ThinkLab (Manila) Arduino Workshop 2010 24 / 34
Boolean Logic Example
Boolean Logic
1010 & 1100
1010 | 1100
What is the value of each statement in the example?
ThinkLab (Manila) Arduino Workshop 2010 25 / 34
Functions
Definition
Function
Also known as procedure or subroutine.
A named piece of code that can be used from elsewhere in a sketch.
ThinkLab (Manila) Arduino Workshop 2010 26 / 34
Function Example
Function Definition Example
void setup(){
/ initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
The first line provides information about the function, like
its name, "setup". The text before and after the name
specify its return type and parameters. The code between
the { and } is called the body of the function: what the
function does.
You can call a function that is already been defined (either
in your sketch or as part of the Arduino language). An
example would be the delay() function.
Special Functions
There are two special functions that are part of every Arduino sketch:
setup() and loop().
Definitions
setup() A function that is called once, when the sketch starts.
Setting pin modes or initializing libraries are placed here.
loop() A function that is called over and over and is the
heart of most sketches.
2010 28 / 34
Control Structures
Definition
If-Else Statement
Most basic of all programming control structures.
It allows you to make something happen or not depending on
whether a given condition is true or not.
Syntax
if (some Condition) {
/ do stuff if the condition is true } else {
/ do stuff if the condition is false (optional)
}
If-Else Example
Example
int test = 0;
if (test == 1) {
printf(“Success”);
} else
{ printf(“Fail”);
What is the output of this program?
While Loops
Definition
While Loop
Continue program until a given condition is true
Syntax
while(some condition) {
/ do stuff until condition becomes false
}
ThinkLab (Manila) Arduino Workshop 2010 31 / 34
For Loops
Definition
For Loop
Distinguished by a loop counter
Syntax
for (start; condition; operation) {
/ do stuff until condition becomes false
}
ThinkLab (Manila) Arduino Workshop 2010 32 / 34
Loops Example
While Loop
count = 0;
while (count <= 2) {
printf(“Hello”);
count++
}
For Loop
int count;
for (count = 0; count <= 15; count++) {
printf(“Hello”);
}
For each example, how many times will “Hello” be displayed?
ThinkLab (Manila) Arduino Workshop 2010 33 / 34
Conclusion
We learned the following in this lecture:
Information about Arduino
Downloading a sketch to your Arduino
device
Various programming techniques used
in sketches
Questions?