ECE 513 Part1 ArduinoUnoFundamentals Ver1.2
ECE 513 Part1 ArduinoUnoFundamentals Ver1.2
ECE 513 Part1 ArduinoUnoFundamentals Ver1.2
Arduino NANO
USB to
Serial
Converter
PWR IN USB
(to Computer)
RESET
SCL\SDA
(I2C Bus)
POWER
5V / 3.3V /
GND/ Vin
Digital I\O
PWM(3, 5, 6, 9, 10,
11)
Analog
INPUTS
Arduino UNO Specs:
- 14 digital I/O pins
(6 pins can be used as PWM outputs)
- 6 analog inputs
-16 MHz ceramic resonator
- USB connection
- Power jack
- ICSP header
- Reset button
Arduino UNO Specs Summary:
Microcontroller: ATmega328
Operating Voltage: 5V
Input Voltage(recommended): 7 – 12V
Input Voltage(limits): 6 – 20V
Digital I/O Pins: 14 (of which 6
provide PWM
output)
Analog Input Pins: 6
DC Current per I/O Pin: 40mA
Arduino UNO Specs Summary:
DC Current for 3.3V per I/O Pins: 50mA
Flash Memory: 32KB (ATmega328)
SRAM: 2KB (ATmega328)
EEPROM: 1KB (ATmega328)
Clock Speed: 16MHz
“sketch” – a program you write to run on an
Arduino board
“pin” – an input or output connected to
something. E.G. output to an LED
“digital” – value is either HIGH or LOW
(aka on/off, one/zero) e.g.switch state
“analog (PWM output)” – value ranges, usually
from 0 – 255 e.g. LED brightness, motor
speed, etc
Arduino Software Environment
New
New tab
Open
Status Area
Save
Console
Arduino Software : Skeleton / Structure
// declaration of libraries used
//declaration of variables
void setup()
{
statements;
}
void loop()
{
statements;
}
//user-defined functions
Arduino Software : Structure
Every Arduino program has two
required functions:
void setup()
{
// body of the setup function
statements;
}
void loop()
{
// body of the loop function
statements;
}
Arduino Software : Structure
void setup() { }
-All the code between the two curly brackets will
be run once when the Arduino program first run.
void loop() {}
-This functions is run after setup has finished. After
it has run once it will be run again, and again, until
power is removed.
Arduino Software : Special Symbols
//
- Single line comment
/* */
- Multi – line comment
{}
- used to define when a block of code starts and ends
-used in functions as well as loops
;
- each line of code must be ended with a semicolon
Arduino Software : Constants
HIGH
- used to turn ON an Arduino pin
LOW
-used to turn OFF an Arduino pin
INPUT
- used to set a specific pin to be an input
OUTPUT
- used to set a specific pin to be an output
Example1: Blinking LED
Create a program that would perform the ff.:
- Initialize I/O pins used
- Toggle the LED continuously
- Use 100 ms delay
1. Connect the Arduino Board to computer using USB
cable
2. Double-click the Arduino IDE
3. Write a program to the Arduino IDE
editor and save
4. Compile
digitalWrite(pin,value);
value: HIGH/LOW
ex: digitalWrite(13, HIGH);
delay(time_ms);
time_ms = unsigned long
ex: delay(2500); // delay of 2.5 sec.
Syntax:
Serial.begin(baud);
Common Serial Functions
println() / print()
- Prints data to the serial port as human-
readable ASCII text
Syntax:
Serial.println(valueToPrint);
Serial.print(valueToPrint);
Common Serial Functions
if(Serial)
- Indicates if the specified Serial port is ready
Syntax:
if(Serial)
Return Type: Boolean
Boolean: returns true if the specified serial port is available
Common Serial Functions
available()
- Get the number of bytes (characters)
available for reading from the serial port
Syntax:
Serial.available()
Returns the number of bytes available to read
Common Serial Functions
read()
- Reads incoming serial data
Syntax:
Serial.read()
Returns the first byte of incoming serial data available(or -1 if no
data is available)
Second Program: Serial Monitor
Third Program: Serial Monitor
Arduino Software : Fundamentals (Data Types)
Type Memory Range Notes
(bytes)
= Assignment a=b
+ Addition a+b
- Subtraction a–b
* Multiplication a*b
/ Division a/b
% Modulus a%b
Arduino Software : Fundamentals (Compound Ass.)
Operator Description
! logical NOT Operation
&& logical AND Operation
II logical OR Operation
Arduino Software : Fundamentals (Relational
Operators)
Operator Operation Example Meaning
== Equal a == b Is a equal to b ?
!= Not equal a != b Is a not equal to
b?
< Less than a < b Is a less than b?
> Greater than a > b Is a greater than
b?
<= Less than or a <= b Is a less than or
equal equal to b?
>= Greater than or a >= b Is a greater than
equal or equal to b?
Arduino Software : Fundamentals (Conditional)
If Statement
The syntax for an If statement:
if (boolean - expression)
{
statement 1;
statement 2;
...
statement n;
}
Arduino Software : Fundamentals (Conditional)
If Statement
Logical
Expression true
statement 1
false
Arduino Software : Fundamentals (Conditional)
If - Else Statement
The syntax for an If – Else statement:
if (boolean - expression)
{
statement – list - 1
}
else
{
statement – list - 2
}
Arduino Software : Fundamentals (Conditional)
If – Else Statement
Logical
false Expression true
statement 2 statement 1
Arduino Software : Fundamentals (Conditional)
if(logical expression 1)
statement – list – 1
else
statement – list – 2
}
else
{
if(logical expression 2)
statement – list – 3
else
statement – list – 4
}
Arduino Software : Fundamentals (Conditional)
Else - If Statement
Syntax:
if(logical expression 1) {
statement – list – 1
}
else if(logical expression 2) {
statement – list -2
}
else if(logical expression 3) {
statement – list -3
}
else {
Statement – list – n
}
Arduino Software : Fundamentals (Conditional)
Switch Statement
Syntax :
switch (expression) {
case value : statement(s);
break;
case value : statement(s);
break;
.......
default : statement(s);
break;
}
Arduino Software : Fundamentals (Conditional)
Switch Statement
statement 1
Case Value
true statement 1 break
1
false
Case Value
true statement n break
n
false
Default
statement
Arduino Software : Fundamentals (Iteration)
while loops
Form:
while ( expression ) statement;
or
while ( expression ) {
statement(s);
}
Arduino Software : Fundamentals (Iteration)
Arduino Software : Fundamentals (Iteration)
do-while loops
Form:
or
do {
statement(s);
}while (expression) ;
Arduino Software : Fundamentals (Iteration)
Arduino Software : Fundamentals (Iteration)
for loops
Form:
or
statement(s);
}
Arduino Software : Fundamentals (Iteration)
Arduino Software : Fundamentals (Iteration)
break
- used to exit from a do … while, for , or while loop
operation
- also, used to exit from a switch statement
continue
- causes the next iteration of the enclosing for, while,
or do…while loop to begin
Arduino Software : Fundamentals (Iteration)
return
- terminate a function and return a value from a
function to the calling function, if desired
Syntax:
return value;
Arduino Software : Fundamentals :Variables
Declaring Variables
-before they are used, all variable have to be declared
Syntax:
type varName;
or
type varName = value;
Example:
int inputVariable1;
int inputVariable2 = 0;
Arduino Software : Fundamentals :Variables
Variables Scope
Global variable – one that can be seen by every function in a
program
- any variable declared outside of a function
const
- preferred for defining constants
Syntax:
const type varName = value;
Example:
const float pi = 3.14;
Arduino Software : Fundamentals
#include
-used to include outside libraries in your sketch/program
Syntax:
#include <library.h>
Example:
#include <EEPROM.h>
Arduino Software : Fundamentals (Array)
Array
-is a collection of values that are accessed with an index number
-any value in the array may be called upon by calling the name of
the array and the index number of the value
y1 = myArray[1]; // y1
int myArray[] = {value0,value1,value2,…} contains 1
Example:
char string1[] = “Arduino”; //7 chars + null char
functions
-isa block of code that has a name and a block
of statements that are executed when the
function is called
Syntax:
type functionName(parameters)
{
statements;
}
Arduino Software : Fundamentals
Example1:
int delayVal()
{
int v; //create temporary variable ‘v’
v = analogRead(pot); //read potentiometer value
v /= 4; //converts 0-1023 to 0-255
return v; //return final value
}
Arduino Software : Fundamentals
Example2:
void setup() {
Serial.begin(9600);
}
void loop() {
int i = 2;
int j = 3;
int k;
k = myMultiplicationFunction(i , j);
Serial.println(k);
delay(500);
}
int myMultiplicationFunction(int x, int y) {
int result;
result = x*y;
return result;
}
Fourth Program: LED Fading
Fourth Program: LED Fading
Arduino Software : Commands
analogWrite(pin,value)
- changes the PWM rate on one of the
pins marked PWM
- value may be a number between 0 and
255 that represents the scale between
0 and 5V output voltage
Example:
analogWrite(9,128); //Dim AN led on pin
9 to 50%
// NOTE: -> commands are CASE-sensitive
Controlling DC Motor
void loop() {
val = analogRead(potPin);
digitalWrite(ledPin, HIGH);
delay(val);
digitalWrite(ledPin, LOW);
delay(val); // stop the program for some time }