Sunteți pe pagina 1din 3

How to use digitalRead in Arduino

 As I have explained in the above section that Arduino UNO has 14 digital pins in total
starting from 0 to 13 as shown in below figure:

 So, you can see in the above figure that we have RXD at 0 which is sued for Serial
receving and then we have TXD at 1 used for Serial writing.
 So, these pins from 0 to 13 are all digital and after these digital Pins we have GND.
 Now I hope you have got the idea of digital Pins.

 Next thing is How to use these digital Pins, normally we connect different digital
sensors with these digital Pins.
 For example, I have a digital Sensor named as Vibration Sensor. This sensor gives
HIGH when it feels vibrations and gives LOW in normal condition.
 So, I am gonna connect the Signal Pin of this Sensor with any digital Pin of Arduino.
 Now, coming towards digitalRead command, this digitalRead command is used in
Arduino for reading the status of digital Pins on Arduino.
 So, when we want to read whether the digital Pin of Arduino is HIGH or LOW, we
use this digitalRead command.
 The syntax of digitalRead is as follows:

int Reading = digitalRead (int PinNumber);

 digitalRead command takes one input which is the value of the Pin, like if you wanna
read the digital status of Pin # 8 then you have to enter 8 in the small brackets of
digitalRead.
 digital Read returns Boolean data which is either HIGH or LOW and it is saved in the
integer variable which I have named Reading in the above syntax.
 So, let’s have a look at the example of digitalRead:

Reading = digitalRead (8);

 In the above example, I am reading the status of digital Pin # 8 of Arduino and saving
it value in the Reading variable.
 So, I hope now you have understood completely How to use the digital Read in
Arduino.

Note:
 One important thing to note here is that because we are reading the data from digital
Pin so that digital Pin must have to be an input. So, you need to declare that Pin as an
input.

 So, let’s have a look at a small code in which we will read the status of pin # 8 of
Arduino and then display its status on Serial Terminal.

Arduino
int Pin = 8; // Initializing Arduino P
int Reading;

void setup() {

1
int Pin = 8; // Initializing Arduino Pin
2
int Reading;
3
4
void setup() {
5
pinMode(Pin, INPUT); // Declaring Arduino Pin as an Input
6
}
7
8
void loop() {
9
Reading = digitalRead(Pin); // Reading status of Arduino digital Pin
10
11
if(Reading == HIGH)
12
{
13
Serial.println("HIGH");
14
}
15
16
if(Reading == LOW)
17
{
18
Serial.println("LOW");
19
}
20
}
21

Summary

So, here’s a short summary of the above discussion for a quick revision:

Definition:

 digitalRead is used to read the status of any digital Pin in Arduino.


 We have to give the digital Pin number in the small brackets.

Syntax:

 Syntax of digital Read is:


int Reading = digitalRead (int PinNumber);

Return:

 digitalRead returns HIGH or LOW depending on the status of corresponding digital


Pin.

Example:

Reading = digitalRead (8);

Restriction:

 Before reading status of any digital Pin, we have to first declare that Pin as an input.:

pinMode(8,INPUT);

So, that’s all about today. I hope you have enjoyed today’s tutorial and are gonna learn
something out of it. Let me know if you have any questions in it. Take care

S-ar putea să vă placă și