EasyManuals Logo

SunFounder ESP32 User Manual

Default Icon
771 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #378 background imageLoading...
Page #378 background image
SunFounder ESP32 Starter Kit
(continued from previous page)
# Function to handle the interrupt
def motion_detected(pin):
global motion_detected_flag
print("Motion detected!")
motion_detected_flag = True
# Attach the interrupt to the PIR sensor pin
pir_sensor.irq(trigger=machine.Pin.IRQ_RISING, handler=motion_detected)
# Main loop
while True:
if motion_detected_flag:
led.value(1) # Turn on the LED
time.sleep(5) # Keep the LED on for 5 seconds
led.value(0) # Turn off the LED
motion_detected_flag = False
When the script is running, the LED will light up for 5 seconds and then go off when the PIR module detects someone
passing.
Note: The PIR module has two potentiometers: one adjusts sensitivity, the other adjusts detection distance. To make
the PIR module work better, you need to turn both of them counterclockwise to the end.
How it work?
This code sets up a simple motion detection system using a PIR sensor and an LED. When motion is detected, the LED
will turn on for 5 seconds.
Heres a breakdown of the code:
1. Define the interrupt handler function that will be executed when motion is detected:
def motion_detected(pin):
global motion_detected_flag
print("Motion detected!")
motion_detected_flag = True
372 Chapter 3. For MicroPython User

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the SunFounder ESP32 and is the answer not in the manual?

SunFounder ESP32 Specifications

General IconGeneral
BrandSunFounder
ModelESP32
CategoryController
LanguageEnglish