Consider the following Arduino code snippet controlling an LED connected to pin 13.
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
void loop() {
// empty loop
}What will be the state of the LED after setup() finishes?
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
void loop() {
// empty loop
}Remember that digitalWrite(pin, HIGH) sets the pin voltage to 5V (or 3.3V), turning the LED ON if connected correctly.
Setting pin 13 as OUTPUT and writing HIGH to it turns the LED ON. The empty loop() does not change the pin state.
Analyze this Arduino code snippet:
void setup() {
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(8, HIGH);
digitalWrite(8, LOW);
}
void loop() {}
What is the final state of pin 8 after setup() completes?
void setup() {
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(8, HIGH);
digitalWrite(8, LOW);
}
void loop() {}The last digitalWrite call sets the pin state.
The pin is set LOW last, so the final state is LOW (LED OFF).
Consider this Arduino code:
void setup() {
digitalWrite(7, HIGH);
}
void loop() {}
Pin 7 was never set as OUTPUT. What happens when this code runs?
void setup() {
digitalWrite(7, HIGH);
}
void loop() {}Think about how Arduino handles digitalWrite on pins configured as INPUT.
If pinMode is not set, digitalWrite(HIGH) enables the internal pull-up resistor on that pin.
When using digitalWrite() to control an LED connected to an Arduino output pin, which statement about power consumption is correct?
Consider the whole circuit, not just the microcontroller pin.
Power consumption depends on the LED current and resistor, which depends on pin voltage and circuit design.
Examine this Arduino code snippet:
void setup() {
for (int pin = 2; pin <= 4; pin++) {
pinMode(pin, OUTPUT);
digitalWrite(pin, pin % 2 == 0 ? HIGH : LOW);
}
}
void loop() {}
Which pins are HIGH after setup() runs?
void setup() {
for (int pin = 2; pin <= 4; pin++) {
pinMode(pin, OUTPUT);
digitalWrite(pin, pin % 2 == 0 ? HIGH : LOW);
}
}
void loop() {}Check which pins are even and which are odd.
Pins 2 and 4 are even, so set HIGH; pin 3 is odd, so set LOW.
