pcbway




digitalreadHello everyone, I hope you all are fine and having fun. Today’s tutorial is the next episode in the series of basic Arduino tutorial. In today’s tutorial, we are gonna have a look at How to use digitalRead in Arduino. In the previous tutorial, we have seen how to do the Serial Communication using Arduino in which we have seen How to use Arduino Serial Write and we have also have a look at How to use Arduino Serial Read. In those two tutorials, we have seen that Pin # 0 and 1 are used for Serial communication in Arduino. So, now we are gonna discuss the next Arduino Pins.

Other than Serial Pins in Arduino UNO, we also have 12 digital Pins. Serial Pins are also digital Pins so in total we have 14 digital Pins in Arduino UNO starting from 0 to 13. I am gonna explain them in detail in today’s tutorial and we will also have a look at How to use this digitalRead command in Arduino. So, let’s get started with it:

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:

digitalRead, arduino digital,digital pins arduino, arduino digital pins

  • 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.

 

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 !!! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment