You are on page 1of 19

5/23/2015

m)
.co
Hands-on Certified Training on

Mechatronics

an
hm
with
Arduino

rre
Training by
Naveed ur Rehman

In assistance with

du
Muhammad Amin Qureshi
ee
Lecture #2
v
na
n(

PLEASE NOTE
ma

Softcopy of study material will be shared on


Google Drive:
eh

http://goo.gl/2xij0V
rR

During class, use this link:


http://192.168.5.123/arduino
du

Keep in touch and give us feedback at


ee

FB Event Page:
http://goo.gl/z4eWkF
v
Na

1
5/23/2015

m)
.co
Arduino Boards

an
hm
rre
du
ee
You may choose from the variety of Arduino boards
available in market!
v
na
n(

Arduino Boards
ma

Processor: ATmega328
Operating voltage: 5V
eh

Input voltage: 7 – 12 V
CPU Speed: 16 MHz
rR

Analog in: 6
Analog out: 0
Digital I/O: 14
Arduino UNO R3 PWM: 6
du
ee

Learn more: http://www.arduino.cc/en/Products.Compare


v
Na

2
5/23/2015

m)
.co
Arduino Boards

an
Processor: ATmega2560
Operating voltage: 5 V

hm
Input voltage: 7 – 12 V
CPU Speed: 16 MHz
Analog in: 16

rre
Analog out: 0
Arduino Mega 2560 Digital I/O: 54
PWM: 15

du
v ee
Learn more: http://www.arduino.cc/en/Products.Compare
na
n(

Arduino Boards
ma

Processor: AT91SAM3X8E
Operating voltage: 3.3 V
eh

Input voltage: 7 – 12 V
CPU Speed: 84 MHz
rR

Analog in: 12
Analog out: 2
Arduino Due Digital I/O: 54
PWM: 12
du
ee

Learn more: http://www.arduino.cc/en/Products.Compare


v
Na

3
5/23/2015

m)
.co
Arduino Boards

an
Processor: ATmega168/328
Operating voltage: 5 V

hm
Input voltage: 7–9V
CPU Speed: 16 MHz
Analog in: 8

rre
Analog out: 0
Arduino Nano Digital I/O: 14
PWM: 6

du
v ee
Learn more: http://www.arduino.cc/en/Products.Compare
na
n(

Arduino Boards
ma
eh
rR
du
ee

Arduino Yun
Learn more: http://www.arduino.cc/en/Products.Compare
v
Na

4
5/23/2015

m)
.co
Arduino Uno R3

an
hm
rre
du
v ee
Learn more: http://www.arduino.cc/en/Main/ArduinoBoardUno
na
n(

Powering Arduino
ma
eh
rR

Using USB cable


Using a Battery
du
ee

Using AC-DC adapter


v
Na

5
5/23/2015

m)
.co
Powering, Operating Voltage & LED

an
hm
rre
du
v ee
na
n(

Arduino: Input and Output (I/O)


ma

Reference:
eh

Say, “I’m Arduino”


rR

Input devices (or sensors) in me:


du

Eyes (you can see) and Nose (you can smell) etc.

Output devices (or mechanisms) in me:


ee

Tongue (you can talk), hands and legs (they move)


v
Na

6
5/23/2015

m)
.co
Arduino: Input and Output (I/O)

an
• Input and output devices work on Signals.
• Signals are the electrical means to transfer information

hm
between sensors (input devices) to Arduino and
Arduino to mechanisms (output devices).

rre
du
v ee
na
n(

Signals
ma

High
eh

Low Digital Signals


rR

Max
du
ee

Min Intermediate
Analog Signals
v
Na

7
5/23/2015

m)
.co
Signals: I/O Devices

an
Input, Digital Output, Digital

hm
rre
Input, Analog
du Output, Analog
v ee
na
n(

Arduino: Signals I/O


ma

There are several digital and analog input


and output (I/O) pins available on an
eh

Ardunio board.
rR

Lets calculate number of pins for


du

Arduino Uno!
ee
v
Na

8
5/23/2015

m)
.co
Arduino: Electrical Signals I/O

an
Digital I/O (#0 - #13)

hm
Analog Out ~ (#3,5,6,9,10,11)

rre
du
ee
Digital I/O (#A0 - #A5)
Analog In (#A0 - #A5)
v
na
n(

Arduino: Signals I/O


ma

Congratulations! You calculated right numbers.


eh

Digital:
Input: 14 + 06 = 20
rR

Output: 14 + 06 = 20
du

Analog:
Input: = 06
Output: = 06
ee
v
Na

9
5/23/2015

m)
.co
an
hm
rre
du
v ee
na
n(

Arduino Programming
ma

Use Arduino IDE Make changes, do


eh

to write C/C++ improvements, add


codes (Sketches) new features and so
rR

on…
Fix bugs :(
du

Compile them See programmed


and burn it to things working
ee

Arduino board in real !


v
Na

10
5/23/2015

m)
.co
Arduino Programming: IDE

an
1. Download the Arduino Software (or IDE):
http://www.arduino.cc/en/Main/Software

hm
2. Install it on your computer

rre
3. Click shortcut icon on Desktop to open IDE

du
v ee
na
n(

Arduino Programming: IDE


ma

1. Open IDE.
2. Connect Arduino board with
Computer using USB cable
eh

You are
3. Click Tools menu (it may
take slightly longer).
rR

4. Hover Board in Tools &

genius…make sure that right board


(Arduino Uno) is selected.
du

5. Now, hover Port in Tools and


make sure that some COM
port is selected. Don’t worry
ee

about
Selfie the number written
time…!
next to COM.
v
Na

11
5/23/2015

m)
.co
Arduino Programming: Default sketch

an
Global space to declare and define global variables

hm
This is configuration part. It runs on power-up or reset.

rre
du
Things to run repeatedly – almost endlessley!
v ee
na
n(

Arduino Programming
ma

What you think you will be doing in Sketches !!! ???


eh

Mostly, these FOUR things:


1. Digital Writing
rR

2. Digital Reading
3. Analog Writing
du

4. Analog Reading
ee

Huh!
v
Na

12
5/23/2015

m)
.co
Arduino Programming: Writing & Reading

an
Writing is like sending signal

hm
(or information) to a
mechanism (or output
device)

rre
du
Reading is like receiving
signal (or information) from
a sensor (or input device)
v ee
na
n(

Arduino Programming: Digital Write


ma

1.It is used to change the state of a mechanism to


either ON or OFF.
eh

2.If it is set to ON the mechanism, a HIGH signal


(+5V) is sent to mechanism.
rR

3.Vice versa, if it is set to OFF the mechanism, a


LOW signal (0 V) is sent to mechanism.
du

4.It can be used to switch ON or OFF a LED, motor,


heater etc.
ee
v
Na

13
5/23/2015

m)
.co
Digital Write: Circuit

an
Step #0: Schematic of circuit

hm
rre
du
v ee
na
n(

Digital Write: Sketch


ma

Step #1: Global space


Declare an integer variable with suitable name
eh

and define it with the digital I/O pin # (0 to 13) or


analog-in pin (A0 to A5).
rR

For example:
int dpin = 3;
du

or
int dpin = A3;
ee
v
Na

14
5/23/2015

m)
.co
Digital Write: Sketch

an
Step #2: Configure in setup()
Here, we will configure the pin mode i.e. the

hm
device is INPUT or OUTPUT. Use this statement:
pinMode(pin# or pin variable, mode);

rre
pin#: could be like 0 to 13 for digital I/O and A0 to
A5 for analog-in.

du
pin variable: would be the exact name of variable
used in global space to define pin #.
ee
mode: could be either OUTPUT to write date or
INPUT to read data.
v
na
n(

Arduino Programming: Digital Write


ma

Step #2: Configure in setup()


As we want to write data to mechanism, use
eh

OUTPUT mode. For our program, we should write:


rR

pinMode(3, OUTPUT);
or
du

pinMode(A3, OUTPUT);
or
pinMode(dpin, OUTPUT);
ee
v
Na

15
5/23/2015

m)
.co
Digital Write: Sketch

an
Step #3: Play in loop()
In loop() you can actually send the data (or signal).

hm
As we are dealing with “Digital” signals, we can
write either HIGH (ON) or LOW (OFF).

rre
A statement used to write the digital data is:
digitalData(pin# or pin variable, state);

du
state: it should be either HIGH or LOW.
ee
v
na
n(

Digital Write: Sketch


ma

Step #3: Play in loop()


Lets say we want to send a HIGH signal, so we
eh

should write:
rR

digitalWrite(3, HIGH);
or
du

digitalWrite(A3, HIGH);
or
digitalWrite(dpin, HIGH);
ee
v
Na

16
5/23/2015

m)
.co
Digital Write: Sketch

an
int dpin = 3;
void setup() {

hm
// put your setup code here, to run once:
pinMode(dpin,OUTPUT);

rre
}

void loop() {

du
// put your main code here, to run repeatedly:
digitalWrite(dpin,HIGH);
ee
}
v
na
n(

Arduino Programming: Digital Write


ma

Making an Arduino circuit:


LED glowing
eh
rR
du
ee
v
Na

17
5/23/2015

m)
.co
Arduino Programming: Delay

an
The delay statement gives a pause of given
milliseconds in loop().

hm
delay(milliseconds);

rre
For a delay of 1 second, write:
delay(1000);

du
v ee
na
n(

Arduino Programming: Digital Write


ma

int dpin = 3;
void setup() { Any
// put your setup code here, to run once:
eh

pinMode(dpin,OUTPUT);
}
idea,
what
rR

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(dpin,HIGH); will
du

delay(1000);
digitalWrite(dpin,LOW); happen
delay(1000);
?
ee

}
v
Na

18
5/23/2015

m)
.co
Arduino Programming: Digital Write

an
Making an Arduino circuit:
LED switching On and Off

hm
rre
du
v ee
na
n(

Naveed ur Rehman Muhammad Amin Qureshi


r R #2

Expert Trainer Training Assistant


wwwnaveedurrehman.com fb.com/ameen85
ma

Hands-on Certified Training on Mechatronics with Arduino


held at NED University of Engineering and Technology, Karachi
Lecture

16 May to 14 Jun, 15
eh
du

Thank You!
We’ll love to hear from you!!
ee

http://goo.gl/z4eWkF
v
Na

19

You might also like