Consider the following Arduino code that reads a button state connected to pin 7 and prints a message:
void setup() {
pinMode(7, INPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(7);
if (buttonState == HIGH) {
Serial.println("Button pressed");
} else {
Serial.println("Button not pressed");
}
delay(1000);
}Assuming the button is pressed (pin 7 reads HIGH), what will be printed to the Serial Monitor?
void setup() {
pinMode(7, INPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(7);
if (buttonState == HIGH) {
Serial.println("Button pressed");
} else {
Serial.println("Button not pressed");
}
delay(1000);
}digitalRead() returns HIGH or LOW depending on the voltage at the pin.
If the button is pressed and connected properly, digitalRead(7) returns HIGH, so the code prints "Button pressed".
Given this Arduino code snippet:
pinMode(5, INPUT_PULLUP); int buttonState = digitalRead(5);
If the button connected to pin 5 is not pressed, what is the value of buttonState?
pinMode(5, INPUT_PULLUP); int buttonState = digitalRead(5);
INPUT_PULLUP mode sets the pin HIGH by default when the button is not pressed.
With INPUT_PULLUP, the pin reads HIGH when the button is not pressed because the internal pull-up resistor pulls the pin voltage up.
Examine this Arduino code snippet:
void setup() {
pinMode(8, INPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(8)
if (state == HIGH) {
Serial.println("High");
}
}What error will the Arduino IDE show when compiling?
void setup() {
pinMode(8, INPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(8)
if (state == HIGH) {
Serial.println("High");
}
}Check the end of the line where digitalRead is called.
The line int state = digitalRead(8) is missing a semicolon at the end, causing a syntax error.
Consider a button connected to an Arduino input pin. Sometimes digitalRead() returns LOW even when the button is not pressed. Why could this happen?
Think about what happens to an input pin voltage if it is not connected to anything.
If the input pin is floating (not connected to a defined voltage), it can randomly read LOW or HIGH. Using a pull-up or pull-down resistor fixes this by setting a default voltage.
Analyze this Arduino code snippet:
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT);
Serial.begin(9600);
}
void loop() {
int val2 = digitalRead(2);
int val3 = digitalRead(3);
if (val2 == LOW && val3 == HIGH) {
Serial.println("Case 1");
} else if (val2 == HIGH && val3 == LOW) {
Serial.println("Case 2");
} else {
Serial.println("Other case");
}
delay(1000);
}Assuming pin 2 button is pressed (connected to ground) and pin 3 is floating (not connected), what will be printed?
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT);
Serial.begin(9600);
}
void loop() {
int val2 = digitalRead(2);
int val3 = digitalRead(3);
if (val2 == LOW && val3 == HIGH) {
Serial.println("Case 1");
} else if (val2 == HIGH && val3 == LOW) {
Serial.println("Case 2");
} else {
Serial.println("Other case");
}
delay(1000);
}Remember that a floating input pin can read either HIGH or LOW unpredictably.
Pin 2 with INPUT_PULLUP reads LOW when pressed. Pin 3 is floating and can read either HIGH or LOW unpredictably. Since val3 is not guaranteed HIGH or LOW, the first two conditions are unlikely both true, so "Other case" prints.
