Arduino - GPIO - Digital in
Arduino - GPIO - Digital in
Arduino - GPIO - Digital in
GPIO: DIGITAL
IN
each
Digital Pin
can
either TAKE INPUT or GIVE OUTPUT
2
Digital Input
3
Switch • No polarity terminals
d o
e n
t
n
n
t
n e
o d
I
I
ly
c
4
task
• Push buttom switch is connected to Digital Pin
2 • Connect LED to Digital Pin 13
Task is to
- turn ON the LED when switch is pushed
- and turn it OFF when switch is released
void setup()
{
// put your setup code here, to run once;
//************ initial setup ************//
}
void loop()
{
// put your main code here, to run repeatedly;
//****** actual program to execute ******//
Functions to be used
pinMode()
▪ Description
Configures the specified pin to behave either as an input or
an output.
▪ Syntax
pinMode(pin, mode);
▪ Parameters
pin: the number of the pin whose mode you wish to
set
mode: INPUT, OUTPUT, or INPUT_PULLUP
▪ Returns
None
digitalWrite()
▪ Description
Write a HIGH or a LOW value to a digital pin.
▪ Syntax
digitalWrite(pin, value);
▪ Parameters
pin: the pin number
value: HIGH or LOW
▪ Returns
None
10
delay()
▪ Description
Pauses the program for the amount of time specified as
parameter.
▪ Syntax
delay(ms);
▪ Parameters
ms: the number of milliseconds to pause
▪ Returns
nothing
11
digitalRead()
▪ Description
Reads the value from a specified digital pin,
either HIGH or LOW.
▪ Syntax
digitalRead(pin);
▪ Parameters
pin: the number of the digital pin you want to read
(int) ▪ Returns
HIGH or LOW
12
int buttonState = 0;
void setup()
{
pinMode(SWITCH, INPUT);
pinMode(LED, OUTPUT);
}
void loop()
{
buttonState =
digitalRead(SWITCH);
if(buttonState == HIGH)
digitalWrite(LED,HIGH);
else
digitalWrite(LED,LOW);
}
14
Arduino Community
15