ECE 513 Part1 ArduinoUnoFundamentals Ver1.2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 76

Lecturer:

Engr. Junard P. Kaquilala


Arduino
- is an open source physical computing
platform based on a simple input/output(I/O)
board and a development environment that
implements the Processing language.
- can be used to develop standalone interactive
objects or can be connected to software on
your computer(such as Flash, Processing,
VVVV, or Max/MSP)
Example of Arduino Boards
Arduino UNO Arduino MEGA

Arduino NANO

Arduino LEONARDO Arduino Diecimilia


Arduino
- the word “Arduino” can mean 3 things:

1. A physical 2. A programming 3. A community


piece of environment & philosophy
hardware
Advantages of using Arduino:
a. A multiplatform environment
b. Based on the Processing programming IDE,
an easy-to-use development environment
c. You program it via USB cable, not serial port
d. It is open source hardware and software
e. There is an active community of users
f. Developed in an educational environment
 Arduino UNO
- a microcontroller board based on the
ATmega328.
Microcontroller

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

Toolbar Sketch Name

Verify Serial Monitor

Upload Actual Code

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

5. Select your board: Tools>Board


6. Select your serial port: Tools>Port
7. Upload the program
First Program: Blinking LED
/*
Example 1: Blinking LED
*/
//declaration of variable/s
int ledPin = 13; //LED connected to pin 13
void setup() //use for initialization
{
pinMode(ledPin,OUTPUT); //pin 13 is output
}
void loop() //create a loop
{
digitalWrite(ledPin,HIGH); // LED on
delay(100); //waits for 100 msec
digitalWrite(ledPin,LOW); //LED off
delay(100); //wait for 100 msec
}
Arduino Software : Commands
pinMode(pin,mode);
mode: INPUT/OUTPUT/INPUT_PULLUP
ex: pinMode(13, OUTPUT);

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.

// NOTE: -> commands are CASE-sensitive


Serial
- Used for communication between
the Arduino board and a computer or
other devices
- It communicates on digital pins
0(RX) and 1(TX) as well as with the
computer via USB
Common Serial Functions
begin()
- sets the data rate in bps (Baud) for
serial data transmission
- For communicating with the computer, Baud is:
300, 600, 1200, 2400, 4800, 9600, 14400, 19200,
28800, 38400, 57600, or 115200

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)

boolean 1 true or false(0 / 1)

char 1 -128 to +128 Used to represent an ASCII


character code
byte 1 0 to 255

int 2 -32768 to +32767

unsigned int 2 0 to 65536 Can be used for extra


precision where negative
numbers are not needed
long 4 -2147483648 to 2147483647 Needed only for
representing very large
numbers
unsigned long 4 0 to 4294967295 The same concept in the
unsigned int
float 4 -3.4028235e+38 to
3.4028235e+38
double 4 the same as float
Arduino Software : Fundamentals (Math Operators)

Operator Operation Example

= Assignment a=b

+ Addition a+b

- Subtraction a–b

* Multiplication a*b

/ Division a/b

% Modulus a%b
Arduino Software : Fundamentals (Compound Ass.)

Operator Operation Example Meaning


= Assign value a = 8 a = 8
+= Add to current variable a += b a = a + b
-= Subtract from current a -= b a = a - b
*= Multiply current a *= b a = a * b
/= Divide current a /= b a = a / b
%= Modulus current a %= b a = a % b

Operator Operation Example Meaning


++ Increment by 1 a++ or ++a a = a + 1
-- Decrement by 1 a-- or --a a = a - 1
Arduino Software : Fundamentals (Boolean
Operators)

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)

Nested If - Else Statement


 Syntax:
if (logical expression) {

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:

do statement; while ( expression) ;

or

do {
statement(s);
}while (expression) ;
Arduino Software : Fundamentals (Iteration)
Arduino Software : Fundamentals (Iteration)
for loops

Form:

for (initialization; expression; modification)


statement;

or

for (initialization; expression; modification) {

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

Local variable – only visible to the function in which they are


declared
Arduino Software : Fundamentals :Variables
Variable Scope
Example:
int pinLed; //any function will see this variable
void setup() {
//…
}
void loop() {
int i; // “i” is only “visible” inside of function “loop”
//…
for (int j = 0;j<100;j++) {
//variable j can only be accessed inside the for-loop
brackets
//…
}
}
Arduino Software : Fundamentals
#define
- give a name to a constant value before the program is compiled
Syntax:
#define constantName value
Example:
#define ledPin 3

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

Declaring an Array Accessing an Array


Example: Example:
int myInts[6]; int myArray[5] = {0,1,2,3,4};

y1 = myArray[1]; // y1
int myArray[] = {value0,value1,value2,…} contains 1

int myPins[5] = {0,1,2,3,4};

char message[6] = “hello”;


Arduino Software : Fundamentals (String)
String
-a set of ASCII characters that are used to store textual information
-for storage, use one byte for each character in the string, plus a
null character to tell Arduino that it’s the end of the string

Example:
char string1[] = “Arduino”; //7 chars + null char

char string2[8] = “Arduino”; //same as above

String stringOne = “Hello String”;


Arduino Software : Fundamentals

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

DC motor is the most common


type of motor.
It has two leads, one positive and
one negative.
“If you connect these two leads
directly to a battery, the motor
will rotate. If you switch the leads,
the motor will rotate in the
opposite direction”
Fifth Program: DC Motor Control
Controlling Servo Motor

Servo motor is a rotary actuator or


linear actuator that allows for
precise control of angular or
linear position, velocity and
acceleration.
It has three wires: power, ground,
and signal.
Sixth Program: Servo Motor Control
Using Pushbuttons

Pushbutton is a component that


connects two points in a circuit
when you press it.
Seventh Program: Pushbutton controlling LED
Arduino Software : Commands
digitalRead(pin)
- once a pin is set as an INPUT you
can use this to return whether it is
HIGH or LOW

// NOTE: -> commands are CASE-sensitive


Using Potentiometer

Potentiometer is a simple knob


that provides a variable
resistance, which we can read into
Arduino board as an analog value
Eighth Program: Using Analog Input
/*
Example 7: Using Analog Input
Turns on and off a light emitting diode(LED) connected to digital pin 13. The amount of time the LED will be on and off depends
on the value obtained by analogRead(). In the easiest case we connect a potentiometer to analog pin 2.
*/
int potPin = 2;
int ledPin = 13;
int val = 0;
void setup() {
pinMode(ledPin, OUTPUT); }

void loop() {
val = analogRead(potPin);
digitalWrite(ledPin, HIGH);
delay(val);
digitalWrite(ledPin, LOW);
delay(val); // stop the program for some time }

You might also like