Arduino

Digital Read in Arduino

To read the digital input from the user, we used the approach of digital read serial. For example, we have to take the input of a digital stopwatch in a binary number; zero and one. This input can be read by the digital pins of Arduino, and can utilize the results in some other task.

First, understand what is the digital input? The input values have only two possible states: either LOW (0 volts) or HIGH(5 volts) are known as the digital inputs. In this write-up, the digital read serial in Arduino is explained with the help of an example.

digitalRead() function in Arduino

In Arduino, a built-in function is used for reading the digital inputs. This function of digitalRead() is used and the digital input from the digital pins of Arduino are taken. It reads the input either in High or Low state, moreover, the analog pins can also be used as digital pins in Arduino.

Example : digitalRead() function in Arduino

We will take an example of a push button so that when we press the push button, its state will become high and when we release it, its state will be Low. Consider the following code for this example:

int pButton=2;

void setup() {

Serial.begin(9600);

pinMode(pButton,INPUT);

}

void loop(){

int bState=digitalRead(pButton);

Serial.println(bState);

delay(5000);

}

Explanation: An integer variable “pbutton” is declared with the value of 2 so the push button can be connected to pin 2 and using the pinMode() function we declared the behavior of pin 2 as an input. Then we initialize another integer variable with the name of “bState” and store the states of the push button in it. Finally, we print the result of bState on the serial monitor and serial plotter through serial communication. This process will be repeated again and again for an infinite time period after the delay of 5 seconds (5000 milliseconds) because this code is executed in a loop section.

The serial monitor output will be like this:

And the serial plotter output will be like this:

The push-button was pressed, the Arduino read the input High and then the push button was released so it read the input low. Again the push button is pressed and then released so the next output becomes high and then low.

For its hardware configuration, we need the following components:]

  • Resistor (1kΩ)
  • Arduino Uno
  • Jumper wires
  • Push-button

The following is the circuit diagram of the circuit:

Connect one end of the resistor to the ground and the other terminal to the push button. Connect pin 2 with the same terminal of the push button which is connected with a resistor and the other terminal with the 5-volt power source. So when the push button is pressed, pin 2 receives a high signal and when the button is released, a low signal of 0 volts will be read by pin 2.

The hardware configuration is:

Conclusion

The digital read serial is the process by which we can read the input signals from the digital input pins of Arduino and the digital read serial can be done by using the built-in function of digitalRead(). In this write-up, the digital read serial is explained with the help of an example and the digital signals through digital pins have only two possible outcomes, either low or high.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.