Get Knowledge - Information

Digitalread – Tutorial On How To Use DigitalRead in Arduino.

162

How to use digitalRead in Arduino

Hello everyone, I hope you all are fine and having fun. Today’s tutorial is the next episode in the series of basic Arduino tutorials. In today’s tutorial, we are gonna have a look at How to use digitalRead in Arduino.SO 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 had a look at How to use Arduino Serial Read. Here, 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.

For upon | 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 the below figure:

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

  • So, you can see in the above figure that we have RDX at 0 which is issued for Serial receiving, 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.
  • The 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 a Vibration Sensor. This sensor gives HIGH when it feels vibrations and gives LOW in normal conditions.
  • So, I am gonna connect the Signal Pin of this Sensor with any digital Pin of Arduino.
  • Now, coming towards the 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 its 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 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 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 the corresponding digital Pin.

Example:

Reading = digitalRead (8);

Restriction:

  • Before reading the 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 about it. Take care !!! 🙂

Comments are closed.