Motor Control Robots with Voltage Regulator - What is digitalRead() in arduino ?
What is digitalread() in arduino ?
- The main objective of digitalread in arduino Reads the value from a specified digital pin, either HIGH or LOW.
- We have to give the digital Pin number in the small brackets.
- One important thing to note here is that when reading the data from digital Pin so that digital Pin must have to be an input. So need to declare that Pin as an input.
Syntax
int Reading = digitalRead (int PinNumber);
Example:
Reading = digitalRead (8);
Hardware Required
- Arduino or Genuino Board
- A momentary switch, button, or toggle switch
- 10k ohm resistor
- hook-up wires
- breadboard
Circuit
![Arduino Circuit](https://wikitechy.com/final-year-project/dotnet/motor-control-robots-with-voltage-regulator/img/motor-control-robots-with-voltage-regulator-images/arduino-circuit.png)
Arduino Circuit
There are certain steps to be followed in connecting the circuit
- STEP-1 : Connect three wires to the board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground.
- STEP-2 : The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10k ohm) to ground. The other leg of the button connects to the 5 volt supply.
- STEP-3 : Pushbuttons or switches connect two points in a circuit when press them. When the pushbutton is open there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and reads as LOW, or 0.
- STEP-4 : It makes a connection between its two legs when the button is closed (pressed) and connecting the pin to 5 volts, so that the pin reads as HIGH, or 1.
- STEP-5 : When disconnect the digital i/o pin from everything, the LED may blink erratically. It doesn't have a solid connection to voltage or ground, and it will randomly return either HIGH or LOW this is done because the input is "floating" .That's why need a pull-down resistor in the circuit.
Schematic
![Schematic](https://wikitechy.com/final-year-project/dotnet/motor-control-robots-with-voltage-regulator/img/motor-control-robots-with-voltage-regulator-images/schematic.png)
Schematic
Example Code
int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}