On Arduino board there are many digital inputs and outputs (digital I/O), which allow you to connect the Arduino to various sensors, actuators, and other ICs. Learning use of them allows you to use the Arduino to do many interesting and useful things, such as reading switches / inputs, lighting LED/indicators, and controlling relays.
Analog & Digital signals
Analog signals may take on any value within a range of
values, while digital signals can have only two distinct values: HIGH (1) or
LOW (0). You can use digital signals in various situations.
Functions
The below options are Arduino functions associated
with digital signals that we will use in this tutorial:
pinMode()
digitalRead()
digitalWrite()
pinMode
(pin_number, mode)
Arduino digital I/O pins can be used as input or output; but
first you have to configure the pins you wish to use as digital Input or Output
with this function.
You just have to tell the pin number or name and mode:
INPUT or OUTPUT.
You can also enable internal pull-up by using INPUT_PULLUP, When mode is set
to INPUT_PULLUP, a 20 kohm pull-up resistor is internally connected to the
pin to force the input HIGH if there is nothing connected to the pin.
Example:
Suppose you want to use pin no.7 as Input, then you will
write the code as given below
pinMode (7, INPUT);
If you want to use internal pull up, then code will be like
this
pinMode(7, INPUT_PULLUP);
If you want to use pin 7 as output, the code will be as given
below.
pinMode(12,OUTPUT);
Function:
digitalWrite(pin_number,value)
When you want to control and digital output then this
function is used.
First mention the pin number or name, and then after comma,
its value, HIGH or LOW.
Example:
Suppose you want to use pin 12 as output, then you will write
the value as given below
digitalWrite(12, HIGH); //(
to make it high)
digitalWrite(12, LOW); //( to
make it low)
Function: digitalRead(pin_number)
This function is use to read a digital value from a pin. ‘pin_number’ is
the number of the digital Input pin you want to read. This function returns one
of two values: HIGH or LOW.
Example: if you want to read digital pin 12 then you will
write code as…
digitalRead(12);
Experiment
1: Blinking LED with some delay
In this experiment,
we will make an LED ON and OFF , for this we will use digitalWrite() function,
but first we have to set the mode of the pin as output.
Hardware Required
1 x LED
1 x 330 ohm resistor
1 x Arduino UNO
1 x breadboard
2 x jumper Wires
The figure above shows how to connect an LED through 330 ohm resistor to the Arduino. As shown, the LED is connected to digital I/O pin 12 of the Arduino through the resistor. The resistor limits the current through the LED and it protect the Arduino also from overloading.
The program below first configures pin 12 to be an OUTPUT,
then sets the digital output pin to HIGH for 500 ms, then to LOW for another 500
ms.
‘delay()’ is an inbuilt function, which is used to give some
delay in the code.
‘delay(500)’ means it provides 500ms delay.
Program
for Experiment #1
int outputLed = 12;
void setup()
{
pinMode(outputLed, OUTPUT);
// sets the pin as output
digitalWrite (outputLed, LOW); // sets the output pin low
}
void loop()
{
digitalWrite(outputLed, HIGH); //sets the output led high
delay(500); //500ms delay
digitalWrite(outputLed, LOW); //sets the output led low
delay(500); //500ms delay
}
Run the experiment
Connect the anode
of the LED to one end of the resistor 330 ohm and the other end of the resistor
to digital I/O pin 12 on the Arduino board.
Connect the
cathode of the LED to the Arduino GND pin.
Connect the
Arduino to the PC using Arduino USB cable and transfer the program to Arduino
using Arduino IDE software.
USB cable is also providing the power to the Arduino board
so, no need to supply separately to the Arduino.
Experiment
2: Control an LED By a Button
This experiment will demonstrate how to control a
digital output by a digital input. When you press the pushbutton connected to a
digital input will turn the LED ON or OFF. The program uses both the functions digitalWrite() and digitalRead().
Hardware Required
1 x LED
1 x 330
ohm resistor
1 x 10 Kohm resistor
1x pushbutton switch/micro switch
1 x Arduino UNO
1 x breadboard
6 x jumper Wires
As you can see from the diagram above, we are now using two
Arduino digital I/O pins. An LED is connected to pin 12, which is configured as
an OUTPUT. A pushbutton is connected to pin 7, which is configured as an INPUT.
When you press the pushbutton switch, pin 7 is set to HIGH, and the program will
then set the output of pin 12 to HIGH and turns on the LED. On Release of the
pushbutton resets pin 7 to LOW. The program then sets pin 12 to LOW, which
turns off the LED.
Program
for Experiment #2
int inputButton = 7;
int outputLed = 12;
void setup()
{
pinMode(inputButton, INPUT); // sets the pin as input
pinMode(outputLed, OUTPUT);
// sets the pin as output
digitalWrite (outputLed, LOW); // sets the output pin low
}
void loop()
{
int buttonStatus = digitalRead (inputButton); //variable to store input pin status
if(buttonStatus == HIGH)
{
digitalWrite(outputLed, HIGH); //sets the output led high
delay(10); //10ms delay
}
else
{
delay(10)
digitalWrite(outputLed, LOW); //sets the output led low
}
}
Run the experiment
Connect the circuit as shown in the above diagram.
Connect the Arduino using Arduino USB cable.
Connect the Arduino to the PC using Arduino USB cable and
transfer the program to Arduino using Arduino IDE software.
USB cable is providing also providing the power to the
Arduino so, no need to provide the power separately.
Press the button LED should be ON and on release of the
button LED should be OFF.
Comments
Post a Comment