You are on page 1of 21

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 #3
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 Programming: Digital Read

an
1.It is used to read the state of a digital sensor (or
may be switch), whether it is HIGH or LOW.

hm
2.It can be used to read the state of a push
button, on/off switch, float sensor, flame sensor,

rre
capacitive touch sensor etc.

REMEMBER:

du
Receiving HIGH doesn’t mean that physical
switch is ON! Rather, it all depends upon the
ee
circuitry being deployed.
v
na
n(

Digital Read: “Switch” Circuit


ma

Step #0: Schematic of circuit


eh

There are mainly two kinds of circuit


configurations, one may build to read
rR

the state of a switch:

1. Pulldown Resistance Configuration


du

2. Pullup Resistance Configuration


ee
v
Na

2
5/23/2015

m)
.co
Digital Read: “Switch” Circuit

an
Pulldown Resistor Configuration:

hm
Switch Read
Pressed (Close) HIGH (1)
Unperssed (Open) LOW (0)

rre
du
v ee
na
n(

Digital Read: “Switch” Circuit


ma

Pullup Resistor Configuration:


eh

Switch Read
Pressed (Close) LOW (0)
rR

Unperssed (Open) HIGH (1)


du
ee
v
Na

3
5/23/2015

m)
.co
Digital Read: “Switch” Sketch

an
Step #0: Schematic of circuit (Pulldown)

hm
rre
Switch

10 kΩ
du
v ee
na
n(

Digital Read: “Switch” 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 sensor = 3;
du

or
int sensor = A3;
ee
v
Na

4
5/23/2015

m)
.co
Digital Read: “Switch” Sketch

an
Step #2: Configure in setup()
As we want to read data from a switch, use INPUT

hm
mode. For our program, we should write:

rre
pinMode(3, INPUT);
or
pinMode(A3, INPUT);
or
pinMode(sensor, INPUT); du
v ee
na
n(

Digital Read: “Switch” Sketch


ma

Step #3: Read in loop()


In loop() you can actually read the state (or signal)
eh

of switch.
A statement used to read the digital data is:
rR

int state = digitalRead(pin# or pin variable);


du

As we are dealing with “Digital” signals, we may


receive either HIGH or LOW.
ee
v
Na

5
5/23/2015

m)
.co
Digital Read: “Switch” Sketch

an
int sensor = 3;
void setup() {

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

rre
}

void loop() {

du
// put your main code here, to run repeatedly:
int state = digitalRead(sensor);
ee
}
v
na
n(

Digital Read: “Switch” Sketch


ma

Input?
1. You may connect a real “Switch” which can be
eh

a push (or reset) button, on/off button or a


two-way switch.
rR

2. Several sensors work as a switch (e.g. float


level sensor), so you may connect them to
du

work as switch.
3. It can also be simulated by simply touching ad
un-touching two jumpers.
ee
v
Na

6
5/23/2015

m)
.co
Digital Read: “Switch” Sketch

an
Output?
There can be several ways to indicate or utilize

hm
sensor output – We will discuss them later today.
For illustration, we are showing output in terms of

rre
glow of LED.

du
ee
When received HIGH When received LOW
LED = ON LED = OFF
v
na
n(

Digital Read: “Switch” Sketch


ma

Step #0: Schematic of circuit with LED (Pulldown)


eh
rR
du
ee
v
Na

7
5/23/2015

m)
.co
Digital Read: “Switch” Sketch

an
Step #0: Schematic of circuit with LED (Pulldown)

hm
rre
du
v ee
na
n(

Digital Read: “Switch” Sketch


ma

int sensor = 3;
int led = 7;
eh

void setup() {
// put your setup code here, to run once:
pinMode(sensor,INPUT);
rR

pinMode(led,OUTPUT);
}
du

void loop() {
// put your main code here, to run repeatedly:
int state = digitalRead(sensor);
ee

digitalWrite(led, state);
}
v
Na

8
5/23/2015

m)
.co
Arduino Programming: Digital Read

an
Making an Arduino circuit:
Reset button to glow LED (Pulldown)

hm
rre
du
v ee
na
n(

Digital Read: “Switch” Sketch


ma

Step #0: Schematic of circuit with LED (Pullup)


eh
rR
du
ee
v
Na

9
5/23/2015

m)
.co
Digital Read: “Switch” Sketch

an
Step #0: Schematic of circuit with LED (Pullup)

hm
rre
du
v ee
na
n(

Digital Read: “Switch” Sketch


ma

Remember, the sketchs for pulldown and pullup


configuration circuits are exactly
eh
rR

THE SAME
du
ee
v
Na

10
5/23/2015

m)
.co
Arduino Programming: Digital Read

an
Making an Arduino circuit:
Reset button to glow LED (Pullup)

hm
rre
du
v ee
na
n(

Arduino Programming: Digital Read


ma

Internal Pullup Configuration:


eh
rR
du
ee
v
Na

11
5/23/2015

m)
.co
Arduino Programming: Digital Read

an
Internal Pullup Configuration:

hm
Internal Pullup resistance

rre
Digital I/O
From
Switch

Inside Arduino
du
v ee
na
n(

Digital Read: “Switch” Sketch


ma

int sensor = 3;
int led = 7;
eh

void setup() {
// put your setup code here, to run once:
pinMode(sensor, INPUT_PULLUP); Only change in sketch is
rR

pinMode(led,OUTPUT); this configuration part!


}
du

void loop() {
// put your main code here, to run repeatedly:
int state = digitalRead(sensor);
ee

digitalWrite(led,state);
}
v
Na

12
5/23/2015

m)
.co
Arduino Programming: Digital Read

an
Making an Arduino circuit:
Reset button to glow LED (Internal pullup)

hm
rre
du
v ee
na
n(

Arduino Programming: Digital Read


ma

Making an Arduino circuit:


Float sensor
eh
rR
du
ee
v
Na

13
5/23/2015

m)
.co
Arduino Programming: Digital Read

an
Making your own float sensor for a water tank!

hm
rre
du
v ee
na
n(
ma
eh
rR
du
ee
v
Na

14
5/23/2015

m)
.co
Digital Sensors/Modules

an
Mostly, sensors come along with their electronic
modules which contains all the necessary things

hm
required to transfer relevant signal (voltage) to
Arduino.

rre
Usually, they have 03 pins:

VCC
Ground du
to be connected at +5V
to be connected at GND
ee
Signal to be connected at Digital I/O
v
na
n(

Digital Sensors
ma

Making an Arduino circuits :


U-Sensor
eh

Tilt sensor
Capacitive touch
rR

Flame sensor
Smoke sensor
du

CO sensor
ee
v
Na

15
5/23/2015

m)
.co
Inverted Logic

an
If you receive HIGH signal when sensor is not
active (or sensing), the sensor is said to be

hm
working on Inverted Logic.

rre
To reverse the inverted logic, you can use LOGICAL
NEGATION (!) just before digitalRead statement.

du
int value = !digitalRead(sensor);
v ee
na
n(

Other outputs
ma

Serial monitor:
Display any streaming response or calculations on
eh

computer screen using Serial monitor.


rR
du
ee
v
Na

16
5/23/2015

m)
.co
Other outputs

an
Serial monitor:
In Setup():

hm
Serial.begin(9600);

rre
Anywhere in Loop():
Print but don’t move to next line:

du
Serial.print(“The output is: ”);
Or print and move to next line:
ee
Serial.println(value);
v
na
n(

Other outputs
ma

Serial monitor:
When your program is running on board, press
eh

Serial monitor button.


rR
du
ee
v
Na

17
5/23/2015

m)
.co
Other outputs

an
Buzzer:
When you don’t want to monitor, just listen!

hm
rre
du
v ee
na
n(

Other outputs
ma

7-Segment:
Love numbers, show them!
eh
rR
du
ee
v
Na

18
5/23/2015

m)
.co
Other outputs

an
7-Segment:
You are provided with a Common Anode 7-Segment

hm
Display.

rre
NOTE:

To power display: pin#3 or pin#8 = +5V

du
Connect segments: with resistors!
To ON segment (LED): digitalWrite LOW (or 0)
ee
To OFF segment (LED): digitalWrite HIGH (or 1)
v
na
n(

Other outputs
ma

7-Segment:
Connect pins of Arduino and 7-Segment with 200-
eh

400Ω resistors.
rR
du
ee
v
Na

19
5/23/2015

m)
.co
Other outputs

an
7-Segment:

hm
rre
du
v ee
na
n(

Other outputs
ma

7-Segment:
void setup() void loop() // write '0'
eh

{ { digitalWrite(2, 0);
pinMode(2, OUTPUT); // write '1' digitalWrite(3, 0);
rR

pinMode(3, OUTPUT); digitalWrite(2, 1); digitalWrite(4, 0);


pinMode(4, OUTPUT); digitalWrite(3, 0); digitalWrite(5, 0);
pinMode(5, OUTPUT); digitalWrite(4, 0); digitalWrite(6, 0);
digitalWrite(5, 1);
du

pinMode(6, OUTPUT); digitalWrite(7, 0);


pinMode(7, OUTPUT); digitalWrite(6, 1); digitalWrite(8, 0);
pinMode(8, OUTPUT); digitalWrite(7, 1); delay(1000);
pinMode(9, OUTPUT); digitalWrite(8, 1); }
ee

} delay(1000);
v
Na

20
5/23/2015

m)
.co
Other outputs

an
7-Segment:

hm
Class work assignment!

rre
du
v ee
na
n(

Naveed ur Rehman Muhammad Amin Qureshi


r R #3

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

21

You might also like