I have completed https://learn.adafruit.com/matrix-porta ... o2-monitor from Learn.Adafruit.com to give to my daughter's kindergarten class (my daughter is the teacher). Since this will run 24 x 7 in a classroom, I would like to add functionality by including a PIR sensor that will shut off the display if no motion is detected for 30 minutes.
I have successfully added the Adafruit PIR Motion Sensor to pin A1 on the Matrix Portal board, and included the following lines of code in the program:
import digitalio
pir = digitalio.DigitalInOut(board.A1)
pir.direction = digitalio.Direction.INPUT
The CO2 Sensor LED Matrix display is fully functional, and I can confirm functionality of the PIR sensor via the REPL (pir.value returns True or False appropriately). What I cannot figure out is the code that would result in the display going blank if no motion is detected for 30 minutes.
Any suggestions would be greatly appreciated.
RGB Matrix Portal Room CO2 Monitor - Add PIR Sensor Timeout
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- Bravo_Sierra
- Posts: 7
- Joined: Mon May 10, 2021 12:57 am
- Bravo_Sierra
- Posts: 7
- Joined: Mon May 10, 2021 12:57 am
Re: RGB Matrix Portal Room CO2 Monitor - Add PIR Sensor Time
Or perhaps have the brightness dim after a set period of no motion in the classroom...
- dastels
- Posts: 15820
- Joined: Tue Oct 20, 2015 3:22 pm
Re: RGB Matrix Portal Room CO2 Monitor - Add PIR Sensor Time
Rough idea off the top of my head:
- you'll probably want to use the debouncer module to inform you of the edges/changes to the PIR output, since that's what matters, more than the value itself.
- have a variable called e.g. active that controls whether the display is active (i.e. on and updated), set it to True
- another variable (e.g. motion_stop_time) that captures the time motion stopped, set it to None
In the main loop:
- upon detecting motion starting set active to True and time.monotonic() to None.
- upon detecting motion ending set motion_stop_time to time.monotonic().
- when you would do a reading and display update, only do it if active is True
- if active is True and motion_stop_time is not None (i.e. the display is being shown/updated but motion has stopped), check if time.monotonic() >= motion_stop_time + 1800. (30 minutes in seconds)
- - if it is: set active to False and motion_stop_time to None
That's at least the skeleton of it:
motion starting actives the display, motion stopping starts a timer, the timer expiring deactivates the display.
Dave
- you'll probably want to use the debouncer module to inform you of the edges/changes to the PIR output, since that's what matters, more than the value itself.
- have a variable called e.g. active that controls whether the display is active (i.e. on and updated), set it to True
- another variable (e.g. motion_stop_time) that captures the time motion stopped, set it to None
In the main loop:
- upon detecting motion starting set active to True and time.monotonic() to None.
- upon detecting motion ending set motion_stop_time to time.monotonic().
- when you would do a reading and display update, only do it if active is True
- if active is True and motion_stop_time is not None (i.e. the display is being shown/updated but motion has stopped), check if time.monotonic() >= motion_stop_time + 1800. (30 minutes in seconds)
- - if it is: set active to False and motion_stop_time to None
That's at least the skeleton of it:
motion starting actives the display, motion stopping starts a timer, the timer expiring deactivates the display.
Dave
- dastels
- Posts: 15820
- Joined: Tue Oct 20, 2015 3:22 pm
Re: RGB Matrix Portal Room CO2 Monitor - Add PIR Sensor Time
If you wanted to dim the brightness, you would do the same thing, but instead of active controlling the reading/display it would be used to set the brightness.
Dave
Dave
- Bravo_Sierra
- Posts: 7
- Joined: Mon May 10, 2021 12:57 am
Re: RGB Matrix Portal Room CO2 Monitor - Add PIR Sensor Time
Thanks. This looks promising. I have the code for debouncing, and will stitch together with the code you have suggested. I'm sure I will have more questions, probably about where to introduce the new code into the program, but I will see if I can make this work on my own first.
- dastels
- Posts: 15820
- Joined: Tue Oct 20, 2015 3:22 pm
Re: RGB Matrix Portal Room CO2 Monitor - Add PIR Sensor Time
Ask away, I look forward to seeing what you come up with.
Dave
Dave
Please be positive and constructive with your questions and comments.