Complete the code to set the PIR sensor pin as input.
const int pirPin = 7; void setup() { pinMode([1], INPUT); } void loop() { // Your code here }
You need to set the PIR sensor pin as input using pinMode(pirPin, INPUT); where pirPin is the variable holding the pin number.
Complete the code to read the PIR sensor value.
int sensorValue = digitalRead([1]);INPUT instead of a pin number.To read the PIR sensor value, use digitalRead(pirPin); where pirPin is the pin connected to the sensor.
Fix the error in the code to turn on the built-in LED when motion is detected.
if (sensorValue == [1]) { digitalWrite(LED_BUILTIN, HIGH); } else { digitalWrite(LED_BUILTIN, LOW); }
The PIR sensor outputs HIGH (1) when motion is detected, so the condition should check for 1.
Fill both blanks to create a dictionary that maps sensor states to messages.
const char* messages[] = {"No motion", [1];
void loop() {
int state = digitalRead(pirPin);
Serial.println(messages[[2]]);
}1 instead of the state variable.The array holds messages for states 0 and 1. Use "Motion detected" for {{BLANK_1}} and state for {{BLANK_2}} to print the correct message based on the sensor state.
Fill all three blanks to complete the code that initializes serial communication, reads the PIR sensor, and prints the status.
void setup() {
Serial.[1](9600);
pinMode(pirPin, INPUT);
}
void loop() {
int [2] = digitalRead(pirPin);
Serial.println([3] == 1 ? "Motion detected" : "No motion");
delay(500);
}print instead of begin to start serial.Use Serial.begin(9600); to start serial communication. Read the sensor into sensorState and print the message based on its value.
