You are on page 1of 25

Introduction to

ARDUINO
BAU – FALL 2018
CONTENTS
INTRODUCTORY LEVEL

Section 1 • i/o pins


Section 2 • Software main blocks, & main Buttons
Arduino UNO • Power pins • Selecting Port for Arduino
Board • BREADBOARD
Software • Pin Mode, Digital/Analog Read/Write

Section 3 • Delay()
Section 4 • LED blinking (Flasher)
Programming • If, else • LED activation using Push Button
Functions • For loop
Examples • Fading an LED (up Dimming)

2 Introduction to ARDUINO 25.11.2018


ARDUINO UNO

Power Pins
Digital Pins : 12
In / Out
Including
Analog Output Pins : 6
Analog Input
Pins : 6

3 SECTION 1 : ARDUINO BOARD 25.11.2018


ARDUINO UNO

DIGITAL INPUT ALL 18


DIGITAL OUTPUT ALL 18
ANALOG INPUT A0, A1, A2, A3, A4, A5 6
ANALOG OUTPUT D3, D5, D6, D9, D10, D11 6

DIGITAL IN/OUT EITHER 0V OR 5V (LOW OR HIGH) 0-1


ANALOG IN RANGE FROM 0V TO 5V 0 -> 1023
ANALOG OUT RANGE FROM 0V TO 5V 0 -> 255

4 SECTION 1 : ARDUINO BOARD 25.11.2018


ARDUINO UNO
USB Port
DC Adapter
For both powering ON
From 7v to 12v
& Programming (Communicating)

5 SECTION 1 : ARDUINO BOARD 25.11.2018


BREADBOARD

BLOCK A

BLOCK B

BLOCK C

BLOCK D

6 SECTION 1 : ARDUINO BOARD 25.11.2018


BREADBOARD EXAMPLE

7 SECTION 1 : ARDUINO BOARD 25.11.2018


IDE Software
There are three main Blocks inside IDE software:

o First Block for Global Variables (General Variables)


o Second Block for Initializing and ONE TIME RUN
o Third block for CONTINUOUS RUNING

8 SECTION 2 : SOFTWARE 25.11.2018


IDE Software
There are two main buttons to be used inside IDE
software:
o First Button for VERIFYING if the code contains errors
o Second Button for verifying and UPLOADING the
code to the ARDUINO Board
o Third Button for creating a NEW FILE

9 SECTION 2 : SOFTWARE 25.11.2018


IDE Software
Before uploading the code the port that the
Arduino is connected to should be selected:

o Tools > Port > Select the port Arduino is connected to

10 SECTION 2 : SOFTWARE 25.11.2018


Pin Configuration
pinMode()

• In order to configure howether a PIN will act as an INPUT or OUTPUT,


it should be configured for one time thus there is no need to write the configuration
in the loop the setup is the best choice.
• Example:
void setup() {
pinMode (2 , INPUT); PIN 2 configured as an INPUT PIN
pinMode (10 , OUTPUT); PIN 10 configured as an OUTPUT PIN
}

11 SECTION 2 : SOFTWARE 25.11.2018


Pin Configuration
digitalWrite()

• To write on a digital pin HIGH or LOW (0v or 5v) it must be one of the Digital
Output pins (ALL of them).
• Example:
digitalWrite (2 , LOW );
digitalWrite (10 , HIGH);

12 SECTION 2 : SOFTWARE 25.11.2018


Pin Configuration
digitalWrite()

• To write on a digital pin HIGH or LOW (0v or 5v) it must be one of the Digital
Output pins (ALL of them).
• Example:
digitalWrite (2 , LOW ); PIN 2 will output a LOW Value : 0V
digitalWrite (10 , HIGH); PIN 10 will output a HIGH Value : 5V

5v 0v V lamp = 5 – 0 = 5v (ON)
13 SECTION 2 : SOFTWARE 25.11.2018

0v 0v V lamp = 0 – 0 = 0v (OFF)
Pin Configuration
digitalRead()

• To read if a digital pin is HIGH or LOW (0v or 5v) it must be one of the Digital
INPUT pins (ALL of them).
• Example:
digitalRead (2);
digitalRead (10);

14 SECTION 2 : SOFTWARE 25.11.2018


Pin Configuration
digitalRead()

• To read if a digital pin is HIGH or LOW (0v or 5v) it must be one of the Digital
INPUT pins (ALL of them).
• Example:
digitalRead (2); PIN 2 will read a HIGH Value : 5V
digitalRead (10); PIN 10 will read a LOW Value : 0V

15 SECTION 2 : SOFTWARE 25.11.2018


Pin Configuration
analogWrite()

• To write on an analog pin value between HIGH & LOW (from 0 to 255) it must
be one of the analog Output pins (D3, D5, D6, D9, D10, D11).
• Example:
analogWrite (3 , 125); PIN 3 will output a HIGH Value : 2.5V (50%)
digitalWrite (10 , 255); PIN 10 will output a HIGH Value : 5V

5v 0v V lamp 5v (Full Brightness)


16 SECTION 2 : SOFTWARE 25.11.2018

2.5v 0v V lamp 2.5v (Half Brightness)


Pin Configuration
analogRead()

• To read an analog value across a pin between HIGH & LOW (from 0 to 1023) it
must be one of the analog Input pins (A0, A1, A2, A3, A4, A5).
• Example:
analogRead (A0); PIN A0 will read a LOW Value : 0V (0)
analogRead (A3); PIN A3 will read a HIGH Value : 5V (1023)

17 SECTION 2 : SOFTWARE 25.11.2018


Basic Functions
Delay ()

• During a Delay the Arduino enters a freeze period as if it was PAUSED


• Example:
delay(100); Delay period of 100 milli-seconds
delay(1000); Delay period of 1 second
delay(60000); Delay period of 1 minute

• For periods smaller than 1ms you should use:


delayMicroseconds(100); Delay period of 100 micro-seconds

18 SECTION 3 : Programming Function 25.11.2018


Basic Functions
If () , else ()

• To set a condition use if () otherwise if it is not true the else() will executed.
• Example:
int x = 10;
if (x > 8){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
}

19 SECTION 3 : Programming Function 25.11.2018


Basic Functions
for () loop

• To repeat Something number of times for loop can be used.


• Example:
for (int i = 0 ; i < 5 ; i ++){
digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,LOW);
delay(1000);
}

20 SECTION 3 : Programming Function 25.11.2018


Basic Functions
for () loop

• To repeat Something number of times for loop can be used.


• Example:
for (int i = 0 ; i < 5 ; i ++){
digitalWrite(2,HIGH);
delay(1000); It will be repeated 5 times
digitalWrite(2,LOW);
delay(1000);
}
1 2 3 4 5
1000 1000
21 SECTION 3 : Programming Function 25.11.2018
Practical Examples
LED blinking (Flasher)

• Example 1
void setup () { void setup () { Built-in LED
pinMode (13,OUTPUT); pinMode (2,OUTPUT);
} }
void loop () { void loop () {

External LED
Built-in LED

digitalWrite (13,HIGH); digitalWrite (2,HIGH);


delay (1000); delay (1000);
digitalWrite (13,LOW); digitalWrite (2,LOW);
delay (1000); delay (1000);
} }
22 SECTION 4 : Examples 25.11.2018
Practical Examples
LED activation using Push Button
• Example 2
void setup () {
pinMode (13,OUTPUT);
pinMode (2,INPUT);
}
void loop () {
if (digitalRead (2) == HIGH)
digitalWrite (13,HIGH);
Resistor
else
digitalWrite (13,LOW); Button
}
23 SECTION 4 : Examples 25.11.2018
Practical Examples
Fading an LED (up Dimming)

• Example 3
void setup () {
pinMode (3,OUTPUT);
}
void loop () {
for ( int i = 0 ; i <= 255 ; i ++ ) {
digitalWrite (3, i );
delay (50);
}
}
24 SECTION 4 : Examples 25.11.2018
THANK YOU!
November 25 / “Tue 27,Wed 28”
Phone ext.:
3483
Email:
y.ajrah@bau.edu.lb
Office:
B3 – Industrial Automation & Control LAB

You might also like