Bird
0
0
Arduinoprogramming~10 mins

PIR motion sensor in Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the PIR sensor pin as input.

Arduino
const int pirPin = 7;

void setup() {
  pinMode([1], INPUT);
}

void loop() {
  // Your code here
}
Drag options to blanks, or click blank then click option'
ApirPin
B7
COUTPUT
DLED_BUILTIN
Attempts:
3 left
💡 Hint
Common Mistakes
Using the pin number directly instead of the variable.
Setting the pin mode to OUTPUT instead of INPUT.
2fill in blank
medium

Complete the code to read the PIR sensor value.

Arduino
int sensorValue = digitalRead([1]);
Drag options to blanks, or click blank then click option'
AINPUT
BpirPin
CLED_BUILTIN
D8
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong pin number.
Passing INPUT instead of a pin number.
3fill in blank
hard

Fix the error in the code to turn on the built-in LED when motion is detected.

Arduino
if (sensorValue == [1]) {
  digitalWrite(LED_BUILTIN, HIGH);
} else {
  digitalWrite(LED_BUILTIN, LOW);
}
Drag options to blanks, or click blank then click option'
ALOW
B0
CHIGH
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for LOW or 0 instead of HIGH or 1.
Using the wrong constant for comparison.
4fill in blank
hard

Fill both blanks to create a dictionary that maps sensor states to messages.

Arduino
const char* messages[] = {"No motion", [1];

void loop() {
  int state = digitalRead(pirPin);
  Serial.println(messages[[2]]);
}
Drag options to blanks, or click blank then click option'
A"Motion detected"
Bstate
CsensorValue
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong message string.
Using a constant like 1 instead of the state variable.
5fill in blank
hard

Fill all three blanks to complete the code that initializes serial communication, reads the PIR sensor, and prints the status.

Arduino
void setup() {
  Serial.[1](9600);
  pinMode(pirPin, INPUT);
}

void loop() {
  int [2] = digitalRead(pirPin);
  Serial.println([3] == 1 ? "Motion detected" : "No motion");
  delay(500);
}
Drag options to blanks, or click blank then click option'
Abegin
BsensorState
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of begin to start serial.
Using different variable names for reading and printing.