Wednesday, May 8, 2013

Motion activated Screen Saver in Python on Raspberry Pi


The past few weeks while installing my Raspberry Pi and Gertboard, configuring and testing other people’s software…I have been sitting on an idea for my first project. On one of my run’s to Radio Shack, I found a Parallax Passive Infrared (PIR) Sensor for only $5. Unknown if it would even work with my system (voltage and sensor type), I picked it up with this single idea in mind.
The idea was to have the PIR sensor detect me as I come into the room and turn off the Raspberry Pi’s Energy Saving Screen Blanking.  Well I dived right in and taught myself enough Python to get the sensor up and working (yes, the Parallax Rev. B PIR Sensor will work at 3.3v. (or 5 off the board as well)). I even managed to get the HDMI to engage and Disengage the power saving Screen Blanking. But it is a hardware issue that requires the screen to be redrawn to put the desktop back on the monitor.

Something that most people think isn't possible right now on the RPi (I have a few ideas on that though…). But even if I did get it working through TVService calls, it would not be feasible for the average user to try it out because it would have to be configured to each and every HDMI’s individual settings.
Well, my solution for now, is to run the xscreensaver service and let the PIR handle shutting down the screen-saver when I approach the computer. Not exactly what I was looking for, but still a fun and useful first project. I am going to share it with anyone who would like to try it. Shouldn't need a Gertboard. It only needs one GPIO pin for the PIR’s signal line. I chose 25, but it can be any unused gpio io pin. Then a 3v3 or 5v pin for the PIR “+” lead, and a ground for the PIR’s “-”
lead. Real simple, that’s the hardware except for the placement of the sensor, but that will be a personal choice for each person. Last thing about this hardware, the Parallax PIR sensor has a high and low setting. This is to adjust the range to between 15 to 30 feet. The sensor is ideal for the PI/Gertboard since it can run on 3-6 volts. It runs great on 3.3v.Now onto the code. Written in python. My first attempt after just learning about it a couple weeks ago. So please be kind in your critiques. It should run on everyone’s Raspbian system the way it is. One thing that will be required to run it “as is” is the xscreensaver configured and running on your X desktop. If you haven't installed it yet, you can get it with the simple shell command:

sudo apt-get install xscreensaver

(or use the synaptic or aptitude package installers…). After configured and tested. Then all you need to do is run code from a terminal using the command:

sudo python pir_test.py

It will give you a notice that you have 30 seconds to leave the PIR sensor area and then your screen will turn on your selected screen-saver. Walk into the area and as soon as the PIR sensor detects you, the program deactivates your screensaver, resets the gpio port and exits back to your desktop. Sweet, simple and all done in a few lines of code.
I really urge you to enter the code by hand and try to understand what each line is doing. The downloaded Python Code is FILLED with comments. My comments aren't at all necessary.  They are just to help you understand what is going on in the code. It is also not the only way to do the exact same thing. I have found a couple different ways to do each thing in the code,  This is just the way I put it together. It is usable as it is, but not practical if you have to activate it every time you leave your computer. It would take a few more line to make it check to see if anyone is at the computer and have it activate if no one is around for 10 minutes. Not too hard to get it running when you desktop starts and have it loop so it runs continuously and resets itself when turning off the screen saver. These are a few of the next steps I am going to take with it. I would love to see others take this tutorial and make something useful out of it. If you do, please let me know!

Here’s The Code:
pir_test.py


1 comment:

  1. pi@pyhome ~ $ xscreensaver-command -deactivate
    xscreensaver-command: warning: $DISPLAY is not set: defaulting to ":0.0".

    I use a monitor rca. from the terminal I get this error. can you help me?

    ReplyDelete