Labs / Bipes + MicroPython / 10 Comprehensive Case

10.3 Infrared Motion Sensor Light

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

Young makers, today we're going to make an extremely practical smart home gadget — the infrared motion-activated sensor light! Just like the sensor lights in stairwells, it turns on automatically when someone approaches, and turns off after a delay once the person leaves; you can also add a buzzer for alerts. By learning this project, you'll master the core logic of "human detection + delay control + output feedback", and easily replicate sensor devices in daily life~

1. Case Introduction

Modules Used

Creative Gameplay

Perfectly reproduces the core logic of corridor motion sensor lights, with maximum practicality:

  1. Someone approaches: The infrared sensor detects a human body → the LED light turns on immediately, and the buzzer (optional) beeps once as a prompt;
  2. When people leave: The LED light will not turn off immediately; instead, it will switch off after a 5-second delay (customizable) to avoid turning off the light right after people walk away.
  3. Unoccupied status: The LED light remains off, which is energy-saving and intelligent.

Core Learning Points (Essential for Beginners to Master)

  1. Level detection logic of the digital input module (infrared sensor);
  2. Loop Judgment vs. Interrupt Trigger (Two Methods for Human Body Detection);
  3. Precise control of output devices (LEDs / buzzers);
  4. Delay-off logic (the key to turning off the lights with a delay after people leave);
  5. Sensor anti-shake processing (to avoid false triggers).

2. Wiring Instructions

10.3 Infrared Motion Sensor Light

This case requires connecting two core modules. The wiring process is simple — just connect them according to the corresponding relationship below:

  1. Piranha LED light module (connected to DIO0 interface)

Development Board Pins

Module Pins

Description

Pin 7

DOUT

Level output/PWM output pins on MCU

Pin 8

NC

Do not use

  1. PIR Human Proximity Sensor (connected to DIO1 interface)

Development Board Pins

Module Pins

Description

Pin 9

DIN

Level signal input pin

Pin 10

NC

-

3. Program Description

The core of this case is "detect human body → turn on the light + delay → turn off the light with a delay after the person leaves". We will break it down step by step according to the building block logic to make it clear:

  1. Infinite Loop Module (repeat while true)
    • Function: Keeps the program continuously detecting the status of the infrared sensor without stopping.
  2. Condition Judgment Module (if read digital input)
    • Configuration: pin=Pin 12/GPIO9, Pull-up=true
    • Logic: Detect the level signal of the infrared sensor (which outputs a high level when triggered) to determine whether there is a human body approaching.
  3. Post-trigger action (within the do branch)
    • print "Infrared sensor triggered": the terminal prints the prompt "Human body detected";
    • delay 200 milliseconds: debounce delay, which prevents repeated execution caused by signal jitter;
    • Set output pin Pin10/GPIO7 to true: to control the LED to turn on.
  4. No operation triggered (within the else branch)
    • Set output pin Pin10/GPIO7 to false: Control the LED to turn off when the human body is far away.

After running the program:

10.3 Infrared Motion Sensor Light

A Beginner's Guide to Avoiding Common Pitfalls

  1. Is the LED not lighting up?
    • Check whether the LED pins are connected to GPIO7 and whether the output pins in the program match;
    • Confirm that the infrared sensor has indeed detected a human body (which can be judged via terminal printout).
  2. Sensor false triggering?
    • Adjust the sensitivity of the sensor (turn it slightly counterclockwise to reduce the detection distance);
    • Keep away from heat sources such as air conditioners and table lamps, as these heat sources may be misidentified as "human bodies";
    • Forgot to add the 200ms debounce delay, which causes triggers even with minor signal jitter — make sure to add it!
  3. Do the lights turn off immediately when people leave?
    • Since there is no delay block added in the else branch, according to the advanced optimization logic, just add a 5-second delay in the else clause first, then turn off the light.

✨ Interactive Mini Quiz

  1. Think about it: If you change "keep the light on for 5 seconds" to 10 seconds, which parameter needs to be modified? (Answer: Change time.sleep (5) to time.sleep (10) in the code, and set the delay block in the blocks to 10 seconds)
  2. Try it: Add an "LED blinking" effect to the sample — when someone approaches, the LED flashes quickly 3 times then stays on. Can you modify the code/blocks to make it work?

xml files can be directly imported into the building blocks:

LED lights up when human approaches.xml 3 KB