Jump to content

Welcome, Guest!

allenm

Using a Motion Sensor to Trigger Interrupts

Recommended Posts

Thought I would share some of my Raspberry Pi tests. Rather than being project oriented, most of my tests have had an emphasis on specific sensors/devices and simple Python programs to demonstrate their functionality. This was mainly intended to be a personal log of my Raspberry Pi lab tests, but if anyone can benefit or wants to add their observations so much the better.

 

Hardware: Raspberry Pi, Gertboard (not essential to test) and Parallax Motion Sensor #555-28027 (Radio Shack 276-135).

 

While there are library modules (wiringPi and RPi.GPIO for example) available that handle interrupts, I took a more direct approach for this test and used the poll function in the GPIO facility in Linux.

 

For the sake of clarity, I will first show using this method to check the state of a GPIO pin without the benefit of using an interrupt.

 

Before running the program, a user interface must be created for GPIO25 by executing these statements from the prompt:

 

sudo su

echo 25 >/sys/class/gpio/export # create a user interface for GPIO25

echo in >/sys/class/gpio25/direction # configure GPIO25 as an input

 

This creates a directory with several files representing the configuration and state of GPIO25. For example, the 'value' file will contain a '0' if GPIO is low or an '1' if GPIO is high. The 'direction' file contains 'in' after GPIO25 is created. We could set the contents of any of these files, but their default values are suitable for this next test.

 

I am using the same Parallax motion sensor tested earlier, except BUF1 is jumpered to GPIO25 on my Gertboard. As before, the motion sensor outputs a high for 3 seconds when it senses motion. Otherwise its output remains at low.

 

gpio25-read.py

 

import time

 

f = open('/sys/class/gpio/gpio25/value', 'r') # create a read file object

 

while 1:

f.seek(0) # reset pointer location in file to beginning

motion = f.read(1) # read 1st byte of read file object

if motion == '1':

print('Warning! Motion Detected!')

else:

print('All is quiet...')

time.sleep(1)

 

 

The above program simply hangs in a loop and reads the pin every second. The downside of this approach is the cpu is given no time to do anything else.

 

Before running the next program using interrupts, we make sure we have created a user interface for GPIO25 by executing these statements from the prompt:

 

sudo su

echo 25 >/sys/class/gpio/export # create a user interface for GPIO25

echo in >/sys/class/gpio25/direction # configure GPIO25 as an input

echo both >/sys/class/gpio25/edge # configure GPIO25 to recognize both rising & falling transitions

 

gpio25-interrupt.py

 

import time

import select

import sys

 

f = open('/sys/class/gpio/gpio25/value', 'r') # create a read file object

myInterrupt = select.poll() # create a polling object

myInterrupt.register(f, select.POLLPRI) # add read file object as a source to satisfy poll requests

 

while 1:

myInterrupt.poll() # wait for an interrupt

f.seek(0) # reset pointer location in file to beginning

motion = f.read(1) # read 1st byte of read file object

if motion == '1':

print('Warning! Motion Detected!')

else:

print('All is quiet...')

 

 

With this method a user would be free to add code putting the cpu to work doing other functions, such as controlling other GPIO pins, performing caculations, etc.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

About Us

Bringing you the latest Raspberry Pi news, tutorials and user forum.

The Fruity Computer

×
×
  • Create New...