• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

eBook Raspberry Pi pico remark example pag168 PIR detector

Q&A Forum › eBook Raspberry Pi pico remark example pag168 PIR detector
0 Vote Up Vote Down
Jozef Lodeweyckx asked 1 year ago
3 Answers
0 Vote Up Vote Down
Jozef Lodeweyckx answered 1 year ago

I had some problems that the LED stays on now and then.
By moving the “led on” instruction to the interrupt function the problem was solved.
This is the adapted version:
from machine import Pin, Timer
import time
# PIR sensor pin
pir_pin = 28
# LED pin
led_pin = 20
# Set up LED and PIR sensor
led = Pin(led_pin, Pin.OUT)
pir = Pin(pir_pin, Pin.IN)
# Create timer
motion_timer = Timer()
# Create global variables
motion = False
motion_printed = False
def handle_motion(pin):
global motion
motion = True
motion_timer.init(mode=Timer.ONE_SHOT, period=1000, callback=turn_off_led)
#REMARK modification proposal
led.on()
def turn_off_led(timer):
global motion, motion_printed
led.off() # Turn off the LED
print(‘Motion stopped!’)
motion = False
motion_printed = False
# Attach interrupt to the PIR motion sensor
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_motion)
while True:
if motion and not motion_printed:
print(‘Motion detected!’)
#REMARK modification proposal
#led.on() 
motion_printed = True
elif not motion:
time.sleep(0.1) # Other tasks that you might need to do in the loop

0 Vote Up Vote Down
Jozef Lodeweyckx answered 1 year ago

I forgot that the same applies for the  print(‘Motion_printed!’) instruction .
 

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi Jozef.
Thanks for you suggestion.
 
I’ll compare your suggested code with my code and see if I can make any improvements.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.