Professional Documents
Culture Documents
Mechatronics
Mechatronics
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
http://goo.gl/2xij0V
rR
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(
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(
Switch Read
Pressed (Close) LOW (0)
rR
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(
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(
of switch.
A statement used to read the digital data is:
rR
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(
Input?
1. You may connect a real “Switch” which can be
eh
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(
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(
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(
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(
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(
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(
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
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(
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
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
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
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:
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
} 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(
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