ARDUINO
GPIO: DIGITAL
IN
Parag Narkhede & Shripad Deshpande
Department of Electronics and Telecommunication Engineering
Symbiosis Institute of Technology, Pune
GPIO General Purpose Input Output
computer understands only 1-0
language i.e.
Digital Language
So
GPIO can be considered as
Digital Inputs and/or Digital Outputs
each
Digital Pin
can
either TAKE INPUT or GIVE OUTPUT
2
Digital Input
SWITCH interfacing with
ARDUINO for LED control
3
Switch • No polarity terminals
d o
e n
t
n
n
t
n e
o d
I
I
ly
c
4
Connecting Switch with Arduino
220 Ohm
5
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
Structure of an Arduino “sketch”
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() – configure pin as input or output •
digitalWrite() – pass the appropriate value to digital pin
• delay() – pause the program for particular time •
digitalRead() – read the input present at the digital pin
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
LED control using Switch
int buttonState = 0;
// declare an integer “buttonState” and initialize
// it to 0; this is required to store the button
// state (pushed or released)
void setup()
{
pinMode(2, INPUT); //switch is an input device
pinMode(13, OUTPUT); // LED is an output
device }
void loop()
{
buttonState = digitalRead(2); // read the
button status and store in //
variable
if(buttonState == HIGH) // compare
digitalWrite(13,HIGH);
// Turn ON LED if switch pressed
else
digitalWrite(13,LOW);
// Turn OFF LED if switch is not pressed
}
13
Sophisticated Arduino Sketch
#define LED 13
#define SWITCH 2
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
• The official Arduino website www.arduino.cc
• Provides tutorials, references, designs
• Blog: https://blog.arduino.cc/
• Forum: https://forum.arduino.cc/
• All the functions can be found at:
• https://www.arduino.cc/en/Reference/HomePage
• Famous Arduino Introductory websites:
• Adafruit
• Sparkfun
15