arduino digital read

To read a digital input using Arduino in the C programming language, you can use the digitalRead() function. This function reads the value of a specified digital pin and returns either HIGH or LOW, depending on the voltage level of the pin.

Here is an example of how to use the digitalRead() function in Arduino:

int pin = 2; // Specify the digital pin number

void setup() {
  pinMode(pin, INPUT); // Set the pin as an input
  Serial.begin(9600); // Initialize the serial communication
}

void loop() {
  int value = digitalRead(pin); // Read the value of the pin
  Serial.println(value); // Print the value to the serial monitor
  delay(1000); // Wait for 1 second
}

In this example, we declare a variable pin and assign it the value of the digital pin number you want to read. In the setup() function, we set the pin mode to INPUT using the pinMode() function. Then, in the loop() function, we use the digitalRead() function to read the value of the pin and store it in the value variable. We then print the value to the serial monitor using the Serial.println() function. Finally, we add a delay of 1 second using the delay() function before repeating the process.

Please note that this example assumes you have already set up the necessary hardware and have a basic understanding of Arduino programming.