Consider this Arduino code snippet:
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}What happens to the LED connected to pin 13?
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}Think about what digitalWrite and delay do in the loop.
The code sets pin 13 as output. Then it turns the LED on (HIGH), waits 1 second, turns it off (LOW), waits 1 second, and repeats. This causes the LED to blink on and off every second.
Which AVR hardware register is directly affected when digitalWrite(13, HIGH) is called on an Arduino Uno?
Pin 13 on Arduino Uno corresponds to which AVR port?
Pin 13 on Arduino Uno is connected to bit 5 of PORTB. Writing HIGH sets that bit in PORTB register to 1, turning the pin on.
Look at this Arduino code that tries to blink the LED on pin 13 using direct AVR register manipulation:
void setup() {
DDRB |= (1 << 5);
}
void loop() {
PORTB ^= (1 << 5);
delay(500);
}But the LED does not blink. What is the most likely reason?
void setup() {
DDRB |= (1 << 5);
}
void loop() {
PORTB ^= (1 << 5);
delay(500);
}Check the board variant and pin mapping for pin 13.
On some Arduino boards (like Arduino Mega), pin 13 is not on PORTB5. Using PORTB5 only works on Arduino Uno. If the board is different, the LED won't blink because the wrong port bit is toggled.
Examine this Arduino code snippet:
void setup() {
pinMode(13, OUTPUT)
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
}What error will the compiler show?
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
}Look carefully at the end of the pinMode line.
The line pinMode(13, OUTPUT) is missing a semicolon at the end, causing a syntax error.
Given this Arduino code snippet:
void setup() {
DDRD = 0b00001111;
PORTD = 0b10101010;
}
void loop() {}How many bits are set to HIGH in the PORTD register after setup() runs?
void setup() {
DDRD = 0b00001111;
PORTD = 0b10101010;
}
void loop() {}Count the bits set to 1 in PORTD after assignment.
PORTD is set to 0b10101010 which has bits 7,5,3,1 set (4 bits). The question asks how many bits are set in PORTD register itself, which is 4 bits.
