0
0
Raspberry Piprogramming~10 mins

MotionSensor (PIR) in Raspberry Pi - Interactive Code Practice

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

Complete the code to import the GPIO library for Raspberry Pi.

Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Agpiozero
Btime
CRPi.GPIO
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'gpiozero' instead of 'RPi.GPIO' when the code expects RPi.GPIO syntax.
Importing unrelated libraries like 'time' or 'os'.
2fill in blank
medium

Complete the code to set the GPIO mode to BCM numbering.

Raspberry Pi
GPIO.setmode([1])
Drag options to blanks, or click blank then click option'
AGPIO.BOARD
BGPIO.BCM
CGPIO.OUT
DGPIO.IN
Attempts:
3 left
💡 Hint
Common Mistakes
Using BOARD mode when BCM mode is required.
Confusing input/output constants with mode constants.
3fill in blank
hard

Fix the error in setting up GPIO pin 17 as input with pull-down resistor.

Raspberry Pi
GPIO.setup(17, [1], pull_up_down=GPIO.PUD_DOWN)
Drag options to blanks, or click blank then click option'
AGPIO.IN
BGPIO.OUT
CGPIO.PUD_UP
DGPIO.PUD_DOWN
Attempts:
3 left
💡 Hint
Common Mistakes
Setting pin as output instead of input.
Confusing pull-up and pull-down resistor constants.
4fill in blank
hard

Fill both blanks to read the PIR sensor state and print 'Motion detected!' when motion is detected.

Raspberry Pi
if GPIO.[1](17) == [2]:
    print('Motion detected!')
Drag options to blanks, or click blank then click option'
Ainput
BGPIO.HIGH
CGPIO.LOW
Doutput
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'output' instead of 'input' to read pin state.
Comparing to GPIO.LOW instead of GPIO.HIGH.
5fill in blank
hard

Fill all three blanks to set up GPIO, read PIR sensor, and clean up GPIO after use.

Raspberry Pi
import RPi.GPIO as GPIO
import [1]

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

try:
    while True:
        if GPIO.input(17) == GPIO.HIGH:
            print('[2]')
        [3](0.5)
except KeyboardInterrupt:
    GPIO.cleanup()
Drag options to blanks, or click blank then click option'
Atime
BMotion detected!
Ctime.sleep
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to import the time module.
Using print as a function call in the blank instead of the message string.
Not calling sleep as a function.