Consider the following Arduino code that maps pins and prints their states. What will be printed on the serial monitor?
const int ledPin = 13; const int buttonPin = 7; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); } void loop() { int buttonState = digitalRead(buttonPin); if (buttonState == LOW) { digitalWrite(ledPin, HIGH); Serial.println("LED ON"); } else { digitalWrite(ledPin, LOW); Serial.println("LED OFF"); } delay(1000); }
Remember that INPUT_PULLUP mode means the button reads HIGH when not pressed and LOW when pressed.
The buttonPin is set with INPUT_PULLUP, so when the button is pressed, the pin reads LOW. The code turns the LED on and prints "LED ON" when the button is pressed (LOW). Otherwise, it turns the LED off and prints "LED OFF". This repeats every second.
Given an Arduino Uno, which pin below is typically used for PWM (Pulse Width Modulation) output?
Look for pins marked with a tilde (~) on the Arduino board.
On Arduino Uno, pins 3, 5, 6, 9, 10, and 11 support PWM output. Pin 9 is one of these PWM pins. Pin 13 is usually connected to the onboard LED and is not PWM. Pins 7 and 0 are digital pins without PWM capability.
Examine the code below. The LED connected to pin 8 never turns on. What is the reason?
int ledPin = 8; void setup() { pinMode(ledPin, INPUT); } void loop() { digitalWrite(ledPin, HIGH); }
Think about the difference between INPUT and OUTPUT pin modes.
When a pin is set as INPUT, digitalWrite does not drive the pin HIGH or LOW. To turn on an LED, the pin must be set as OUTPUT. Here, pinMode is incorrectly set to INPUT, so the LED never receives power.
Look at the code snippet below. What error will the Arduino IDE show when compiling?
const int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); delay(1000); }
Check the end of each statement for proper punctuation.
The line 'const int ledPin = 13' is missing a semicolon at the end. This causes a syntax error during compilation.
Given the following Arduino setup code, how many pins are set as OUTPUT?
const int pins[] = {2, 3, 4, 5, 6}; void setup() { for (int i = 0; i < 5; i++) { if (pins[i] % 2 == 0) { pinMode(pins[i], OUTPUT); } else { pinMode(pins[i], INPUT); } } } void loop() {}
Count how many pins in the array are even numbers.
The pins array contains 2, 3, 4, 5, 6. The code sets pins with even numbers as OUTPUT. Even pins are 2, 4, and 6, so 3 pins are OUTPUT.