Consider this Arduino code snippet that reads a button and lights an LED:
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}What happens when the button connected to pin 2 is pressed?
const int buttonPin = 2; const int ledPin = 13; void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); } void loop() { int buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
Think about what digitalRead returns when the button is pressed and how digitalWrite controls the LED.
When the button is pressed, digitalRead(buttonPin) returns HIGH, so the code sets the LED pin HIGH, turning the LED ON. Otherwise, the LED stays OFF.
Which of the following best explains why digital input/output (I/O) pins are the foundation of microcontroller projects like Arduino?
Think about how microcontrollers interact with the outside world.
Digital I/O pins let the microcontroller sense the environment (input) and control devices (output) by turning pins ON or OFF, which is essential for most projects.
Here is a code snippet intended to toggle an LED on pin 13 every second:
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}What is the error that will prevent this code from compiling?
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}Check punctuation carefully after each statement.
The line digitalWrite(13, LOW) is missing a semicolon at the end, causing a syntax error.
Analyze this Arduino code that reads a digital sensor and controls two LEDs:
const int sensorPin = 4;
const int led1 = 10;
const int led2 = 11;
void setup() {
pinMode(sensorPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
int val = digitalRead(sensorPin);
if (val == HIGH) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
} else {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
}
}What happens when the sensorPin reads LOW?
const int sensorPin = 4; const int led1 = 10; const int led2 = 11; void setup() { pinMode(sensorPin, INPUT); pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); } void loop() { int val = digitalRead(sensorPin); if (val == HIGH) { digitalWrite(led1, HIGH); digitalWrite(led2, LOW); } else { digitalWrite(led1, LOW); digitalWrite(led2, HIGH); } }
Look at the else block that runs when sensorPin reads LOW.
When sensorPin reads LOW, the else block sets led1 LOW (OFF) and led2 HIGH (ON).
In Arduino programming, why is it important to call pinMode() to set a pin as INPUT or OUTPUT before using digitalRead() or digitalWrite()?
Think about what happens if a pin tries to send and receive signals at the same time.
Setting pinMode tells the microcontroller if the pin is for input or output. This prevents electrical conflicts and ensures correct signal behavior.
