digitalRead() & how to use digitalRead function in Arduino

What is the digitalRead() function in Arduino, and what digitalRead() function do in Arduino programming are some questions that may arise in your mind while working with Arduino?

Hello guys! Today we going to see about Arduino digitalread function so let’s begin.

What is digitalRead() function in Arduino??

digitalread function in Arduino

So digitalRead() is basically an in-built function used by Arduino to read values from sensors, buttons, etc. in Arduino you can use the digital pin as input or output. If you are using the digital pin as input then you have to read the status of that pin. digitalRead() function helps to read the current status of the digital pin of Arduino. Suppose you have to read sensor value or button status whether button press or not. 

Arduino digitalread

digitalRead() function return values in form 1 and 0 which means 1 - HIGH & 0 – LOW. digitalRead() function also indicate us the values of HIGH & LOW. HIGH means 5v and LOW means 0 v. you can use the digital read function to set a task or to do something such as glow a led or turn ON the buzzer when the sensor value is HIGH etc. the sensor which has in-built ADC (Analog to digital converter) chip gives us digital values which is read by Arduino.  

Arduino uno digitalread

For example, here we have a flame sensor that detects the fire and sends signals in form of 1 and 0 to Arduino 1 means flame detected and 0 means no flame present. In Arduino, we have 14 digital pins 0 to 13 so we can use the digital read function for digital pins only, we cannot use the digital read function for analog pins of Arduino. We usually connect the signal pin of the sensor to the digital pin of the Arduino. We normally don’t use 0 & 1 pins as they are transmitter (TX) & receiver pins (RX) for serial communications. Digital input/output pins are default set as inputs and hence there is no need to declare as input initially in the pin mode inside void setup.

            pin Mode(11, INPUT); 

But in the case of declaring pins at the output, it is mandatory to set the pin as OUTPUT in the pin mode function because of the current limiting resistor therefore it is advisable that one should declare each and every digital pin of Arduino as input or output in pin mode.    

             digitalRead(pin);

This digital read function passes one argument which is Arduino pin current status which we want to read. The digital Read function returns an integer value of 1 or 0 that we are storing in a variable.

What is the difference between digitalRead() and analogRead()??

digitalRead() reads current status of digital pin whether its HIGH or LOW in form of 0 & 1 means 0v & 5v(1 - HIGH & 0 – LOW).  digitalRead() returns one values whether 0 or 1 which represents 0v and 5v. digitalRead() is used by digital pin (0-13) of Arduino.

analogRead( ) reads Analog voltage from 0v to 5v. analogRead() returns integer values from 0 to 1023. analogRead() returns values from 0 to 1023 which represents 0v to 5v. analogRead() uses an analog pin from A0-A5 as input and fed it to ADC to convert it to give integer values. 

So we will look at one basic example of digital read, we have connected one push button to know the current status if it is pressed or not, using the digital read function.

Arduino digital read buttons

Here is a schematic of the circuit. We have connected the push button at pin number 8 of our Arduino board. We have given VCC 5v and GND to push the button.

Code -

int pushButton = 8;  // connect push button to pin 8 of Arduino

void setup() {

   pinMode(pushButton, INPUT); // declare push button as INPUT
  Serial.begin(115200);        // Set baud rate of 115200
}
void loop() {
  int buttonState = digitalRead(pushButton);  // read the status of push button
  if(buttonState==HIGH)                      // check the condition if button presses or not
  {
   Serial.println("Button pressed");            // print the button status
   }
   else if(buttonState==LOW)                // check the condition if button presses or not
   {
    Serial.println("Not pressed");     // print the button status
    }
  delay(100);                               // delay of 100 millisecond
}

Here we have declared a push button that is connected at pin number 8 of Arduino. We have declared a baud rate of 115200 and push button pin as INPUT.

This is a void loop function the code inside it will repeat continuously again & again. So here we are continuously monitoring whether the button is pressed or not. If the button is pressed then the status of the pin connected to the button will be HIGH and Arduino will read value logic 1 which we will get our serial monitor. When the button is released pin status will change to LOW which will be read as logic 0.

In short, when the button is pressed digital read returns us 1 and when the button is not pressed digital read will return us 0.

If the button is pressed, then Arduino detects logic 1.

If the button is not pressed, then Arduino detects logic 0.  

We will store the value of our button status in a variable name as my_status. Now we will use the serial library to print the value in our serial monitor. The serial.print() function is used to print the value of the current status of the button.

digital read serial Arduino

Now we used the serial monitor to observe the output. You can see when there is no button pressed we print as “button not pressed” and when the button is pressed we print “button pressed”.

Conclusion –

Today we learn about the digital read function in Arduino, where to use the digital read function, how to use the digital read function, what the digital read function does in Arduino, syntax, and parameters of the digital read function.

"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu