digitalRead() helps you check if a button or switch is ON or OFF by reading the voltage on a pin.
digitalRead() for input reading in Arduino
Introduction
Syntax
Arduino
int state = digitalRead(pin);pin is the number of the digital pin you want to read.
The function returns HIGH if the pin is receiving voltage, or LOW if not.
Examples
Arduino
int buttonState = digitalRead(2);
Arduino
if (digitalRead(7) == HIGH) { // do something }
Sample Program
This program reads a button connected to pin 4. It prints "Button is pressed" when the button is pressed, and "Button is not pressed" when it is not.
Arduino
const int buttonPin = 4; void setup() { pinMode(buttonPin, INPUT); Serial.begin(9600); } void loop() { int buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { Serial.println("Button is pressed"); } else { Serial.println("Button is not pressed"); } delay(500); }
Important Notes
Make sure to set the pin mode to INPUT before using digitalRead().
Use pull-up or pull-down resistors to avoid random readings when the button is not pressed.
Summary
digitalRead() reads if a pin is HIGH or LOW.
Use it to check buttons, switches, or sensors.
Always set pinMode to INPUT before reading.
Practice
1. What does the
digitalRead() function do in Arduino?easy
Solution
Step 1: Understand the purpose of digitalRead()
The function digitalRead() checks the voltage level on a digital pin and returns HIGH or LOW.Step 2: Differentiate from other functions
Unlike digitalWrite() which sets pin output, digitalRead() only reads input state.Final Answer:
Reads the voltage level (HIGH or LOW) from a digital input pin -> Option BQuick Check:
digitalRead() reads pin state [OK]
Hint: digitalRead() always reads input pin state [OK]
Common Mistakes:
- Confusing digitalRead() with digitalWrite()
- Thinking digitalRead() sets pin output
- Mixing digitalRead() with analogRead()
2. Which of the following is the correct syntax to read a digital pin 7 in Arduino?
easy
Solution
Step 1: Recall the correct function syntax
The correct function call is digitalRead(pinNumber); where pinNumber is the pin to read.Step 2: Check each option
Only digitalRead(7); matches the correct syntax digitalRead(7); others are invalid function calls or assignments.Final Answer:
digitalRead(7); -> Option CQuick Check:
Correct function call [OK]
Hint: digitalRead(pinNumber) reads pin state correctly [OK]
Common Mistakes:
- Using assignment instead of function call
- Wrong function name like readDigital()
- Adding extra words like digitalReadPin()
3. What will be the output on the Serial Monitor if the button connected to pin 2 is pressed (assuming HIGH when pressed)?
void setup() {
pinMode(2, INPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(2);
Serial.println(buttonState);
delay(500);
}medium
Solution
Step 1: Analyze pinMode and digitalRead usage
Pin 2 is set as INPUT, so digitalRead(2) reads the button state correctly.Step 2: Understand button press state
When the button is pressed, the pin reads HIGH which is 1, so Serial.println prints 1 repeatedly.Final Answer:
1 -> Option AQuick Check:
Pressed button = HIGH = 1 [OK]
Hint: Pressed button reads HIGH (1) on digitalRead() [OK]
Common Mistakes:
- Assuming pressed button reads 0 instead of 1
- Forgetting to set pinMode to INPUT
- Confusing analogRead with digitalRead
4. Identify the error in this code snippet that reads a button state on pin 4:
void setup() {
Serial.begin(9600);
}
void loop() {
int state = digitalRead(4);
Serial.println(state);
delay(1000);
}medium
Solution
Step 1: Check pin configuration
The code does not set pin 4 as INPUT using pinMode(), which is required before reading.Step 2: Verify other parts
digitalRead() can be used in loop(), Serial.begin() must be in setup(), and delay() is allowed.Final Answer:
Missing pinMode(4, INPUT) in setup() -> Option DQuick Check:
Always set pinMode before digitalRead() [OK]
Hint: Always set pinMode(INPUT) before digitalRead() [OK]
Common Mistakes:
- Forgetting pinMode() setup
- Moving Serial.begin() to loop() incorrectly
- Thinking delay() breaks digitalRead()
5. You want to detect if a switch connected to pin 3 is pressed, but the switch is wired so it connects the pin to GND when pressed (active LOW). Which code snippet correctly reads the switch state and prints "Pressed" or "Not Pressed" accordingly?
A)
pinMode(3, INPUT);
if (digitalRead(3) == HIGH) {
Serial.println("Pressed");
} else {
Serial.println("Not Pressed");
}
B)
pinMode(3, INPUT);
if (digitalRead(3) == LOW) {
Serial.println("Pressed");
} else {
Serial.println("Not Pressed");
}
C)
pinMode(3, OUTPUT);
if (digitalRead(3) == LOW) {
Serial.println("Pressed");
} else {
Serial.println("Not Pressed");
}
D)
pinMode(3, INPUT_PULLUP);
if (digitalRead(3) == HIGH) {
Serial.println("Pressed");
} else {
Serial.println("Not Pressed");
}hard
Solution
Step 1: Understand active LOW switch wiring
The switch connects pin 3 to GND when pressed, so digitalRead(3) returns LOW when pressed.Step 2: Check pinMode and condition
pinMode must be INPUT (not OUTPUT). The condition to detect press is digitalRead(3) == LOW.Final Answer:
Code snippet B -> Option AQuick Check:
Active LOW switch reads LOW when pressed [OK]
Hint: Active LOW means pressed when digitalRead() returns LOW [OK]
Common Mistakes:
- Using OUTPUT mode instead of INPUT
- Checking for HIGH instead of LOW for active LOW switch
- Using INPUT_PULLUP but checking wrong logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
