SunFounder ESP32 Starter Kit
2. Attach the interrupt to the PIR sensor pin, with the trigger set to “rising” (i.e., when the pin goes from low to
high voltage):
pir_sensor.irq(trigger=machine.Pin.IRQ_RISING, handler=motion_detected)
This sets up an interrupt on the pir_sensor pin, which is connected to the PIR motion sensor.
Here’s a detailed explanation of the parameters:
• trigger=machine.Pin.IRQ_RISING: This parameter sets the trigger condition for the inter-
rupt. In this case, the interrupt will be triggered on a rising edge. A rising edge is when the
voltage on the pin changes from a low state (0V) to a high state (typically 3.3V or 5V, depending
on your hardware). For a PIR motion sensor, when motion is detected, the output pin usually
goes from low to high, making the rising edge an appropriate trigger condition.
• handler=motion_detected: This parameter specifies the function that will be executed when
the interrupt is triggered. In this case, the motion_detected function is provided as the interrupt
handler. This function will be called automatically when the interrupt condition (rising edge) is
detected on the pir_sensor pin.
So, this line of code configures the PIR sensor to call the motion_detected function whenever
motion is detected by the sensor, due to the output pin going from a low to a high state.
3. In the main loop, if the motion_detected_flag is set to True, the LED will be turned on for 5 seconds and
then turned off. The flag is then reset to False to wait for the next motion event.
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
3.24 5.6 Two Kinds of Transistors
This kit is equipped with two types of transistors, S8550 and S8050, the former is PNP and the latter is NPN. They
look very similar, and we need to check carefully to see their labels. When a High level signal goes through an NPN
transistor, it is energized. But a PNP one needs a Low level signal to manage it. Both types of transistor are frequently
used for contactless switches, just like in this experiment.
Let’s use LED and button to understand how to use transistor!
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name ITEMS IN THIS KIT LINK
ESP32 Starter Kit 320+
You can also buy them separately from the links below.
3.24. 5.6 Two Kinds of Transistors 373