Difference between revisions of "Sparkfun: PIR Motion Sensor"
From eLinux.org
(Created page with "Category:ECE497 == Overview == The PIR Motion Sensor can be purchased from [https://www.sparkfun.com/products/8630 SparkFun]. The [http://www.sparkfun.com/datasheets/Compon...") |
|||
| Line 3: | Line 3: | ||
== Overview == | == Overview == | ||
| − | The PIR Motion Sensor can be purchased from [https://www.sparkfun.com/products/8630 SparkFun]. The [http://www.sparkfun.com/datasheets/ | + | The PIR Motion Sensor can be purchased from [https://www.sparkfun.com/products/8630 SparkFun]. The [http://www.sparkfun.com/datasheets/Sensors/Proximity/SE-10.pdf datasheet] describes it: |
''Easy-to-use motion detector with an analog interface. Power it with 5-12VDC, and you'll be alerted of any movement.'' | ''Easy-to-use motion detector with an analog interface. Power it with 5-12VDC, and you'll be alerted of any movement.'' | ||
| − | + | [http://bildr.org/2011/06/pir_arduino/ Arduino Tutorial] | |
| − | + | == Inputs and Outputs == | |
| − | + | This unit works great from 5 to 12V (datasheet shows 12V). You can also install a jumper wire past the 5V regulator on board to make this unit work at 3.3V. Sensor uses 1.6mA@3.3V. | |
== Bone Usage == | == Bone Usage == | ||
| − | |||
| − | |||
| − | |||
| − | |||
=== Reading and Interpreting Analog Input Data === | === Reading and Interpreting Analog Input Data === | ||
| − | |||
| − | |||
=== Sample C Code === | === Sample C Code === | ||
| Line 32: | Line 26: | ||
<pre> | <pre> | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
| + | #include <string.h> | ||
| + | #include <errno.h> | ||
| + | #include <unistd.h> | ||
| + | #include <fcntl.h> | ||
| + | #include <poll.h> | ||
#include <signal.h> | #include <signal.h> | ||
| − | #include < | + | #include <unistd.h> |
| + | #include "gpio.h" | ||
| − | #define | + | /**************************************************************** |
| − | #define | + | * Constants |
| − | #define | + | ****************************************************************/ |
| + | |||
| + | #define SYSFS_GPIO_DIR "/sys/class/gpio" | ||
| + | #define POLL_TIMEOUT (3 * 1000) /* 3 seconds */ | ||
| + | #define MAX_BUF 64 | ||
| + | /* {"gpio1_6", "gpio1_7", "gpio1_2", "gpio1_3", "gpio1_13", "gpio1_12", "gpio1_15", "gpio1_14"} */ | ||
| − | int | + | /**************************************************************** |
| + | * Global variables | ||
| + | ****************************************************************/ | ||
| + | int keepgoing = 1; // Set to 0 when ctrl-c is pressed | ||
| + | /**************************************************************** | ||
| + | * signal_handler | ||
| + | ****************************************************************/ | ||
| + | // Callback called when SIGINT is sent to the process (Ctrl-C) | ||
void signal_handler(int sig) | void signal_handler(int sig) | ||
{ | { | ||
printf( "Ctrl-C pressed, cleaning up and exiting..\n" ); | printf( "Ctrl-C pressed, cleaning up and exiting..\n" ); | ||
| − | + | keepgoing = 0; | |
} | } | ||
| − | /* | + | /**************************************************************** |
| − | * | + | * main |
| − | * | + | ****************************************************************/ |
| − | * | + | int main(int argc, char **argv) |
| − | * | + | { |
| − | * | + | struct pollfd fdset[2]; |
| − | */ | + | int nfds = 2; |
| − | int | + | int gpio_fd, timeout, rc; |
| − | + | char *buf[MAX_BUF]; | |
| − | char | + | unsigned int gpio; |
| + | int len; | ||
| − | if (( | + | if (argc < 2) { |
| − | printf(" | + | printf("Usage: gpio-int <gpio-pin>\n\n"); |
| − | exit(1); | + | printf("Waits for a change in the GPIO pin voltage level or input on stdin\n"); |
| + | exit(-1); | ||
} | } | ||
| − | //Set | + | // Set the signal callback for Ctrl-C |
| − | + | signal(SIGINT, signal_handler); | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | gpio = atoi(argv[1]); | |
| − | + | ||
| − | + | gpio_export(gpio); | |
| − | + | gpio_set_dir(gpio, 0); | |
| + | gpio_set_edge(gpio, "falling"); // Can be rising, falling or both | ||
| + | gpio_fd = gpio_fd_open(gpio); | ||
| − | + | timeout = POLL_TIMEOUT; | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | while( | + | while (keepgoing) { |
| − | printf(" | + | memset((void*)fdset, 0, sizeof(fdset)); |
| − | + | ||
| + | fdset[0].fd = STDIN_FILENO; | ||
| + | fdset[0].events = POLLIN; | ||
| + | |||
| + | fdset[1].fd = gpio_fd; | ||
| + | fdset[1].events = POLLPRI; | ||
| + | |||
| + | rc = poll(fdset, nfds, timeout); | ||
| + | |||
| + | if (rc < 0) { | ||
| + | printf("\npoll() failed!\n"); | ||
| + | return -1; | ||
| + | } | ||
| + | |||
| + | if (rc == 0) { | ||
| + | printf("."); | ||
| + | } | ||
| + | |||
| + | if (fdset[1].revents & POLLPRI) { | ||
| + | lseek(fdset[1].fd, 0, SEEK_SET); // Read from the start of the file | ||
| + | len = read(fdset[1].fd, buf, MAX_BUF); | ||
| + | printf("\nMotion Detected, value=%c, len=%d\n", | ||
| + | buf[0], len); | ||
| + | usleep(5000000); | ||
| + | } | ||
| + | |||
| + | if (fdset[0].revents & POLLIN) { | ||
| + | (void)read(fdset[0].fd, buf, 1); | ||
| + | printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]); | ||
| + | } | ||
| + | |||
| + | fflush(stdout); | ||
} | } | ||
| − | + | ||
| − | + | gpio_fd_close(gpio_fd); | |
| + | return 0; | ||
} | } | ||
</pre> | </pre> | ||
| − | |||
| − | |||
Revision as of 21:22, 23 September 2012
Contents |
Overview
The PIR Motion Sensor can be purchased from SparkFun. The datasheet describes it:
Easy-to-use motion detector with an analog interface. Power it with 5-12VDC, and you'll be alerted of any movement.
Inputs and Outputs
This unit works great from 5 to 12V (datasheet shows 12V). You can also install a jumper wire past the 5V regulator on board to make this unit work at 3.3V. Sensor uses 1.6mA@3.3V.
Bone Usage
Reading and Interpreting Analog Input Data
Sample C Code
The code shown below is sample code to demonstrate reading analog output from the ADXL335.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include "gpio.h"
/****************************************************************
* Constants
****************************************************************/
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64
/* {"gpio1_6", "gpio1_7", "gpio1_2", "gpio1_3", "gpio1_13", "gpio1_12", "gpio1_15", "gpio1_14"} */
/****************************************************************
* Global variables
****************************************************************/
int keepgoing = 1; // Set to 0 when ctrl-c is pressed
/****************************************************************
* signal_handler
****************************************************************/
// Callback called when SIGINT is sent to the process (Ctrl-C)
void signal_handler(int sig)
{
printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
keepgoing = 0;
}
/****************************************************************
* main
****************************************************************/
int main(int argc, char **argv)
{
struct pollfd fdset[2];
int nfds = 2;
int gpio_fd, timeout, rc;
char *buf[MAX_BUF];
unsigned int gpio;
int len;
if (argc < 2) {
printf("Usage: gpio-int <gpio-pin>\n\n");
printf("Waits for a change in the GPIO pin voltage level or input on stdin\n");
exit(-1);
}
// Set the signal callback for Ctrl-C
signal(SIGINT, signal_handler);
gpio = atoi(argv[1]);
gpio_export(gpio);
gpio_set_dir(gpio, 0);
gpio_set_edge(gpio, "falling"); // Can be rising, falling or both
gpio_fd = gpio_fd_open(gpio);
timeout = POLL_TIMEOUT;
while (keepgoing) {
memset((void*)fdset, 0, sizeof(fdset));
fdset[0].fd = STDIN_FILENO;
fdset[0].events = POLLIN;
fdset[1].fd = gpio_fd;
fdset[1].events = POLLPRI;
rc = poll(fdset, nfds, timeout);
if (rc < 0) {
printf("\npoll() failed!\n");
return -1;
}
if (rc == 0) {
printf(".");
}
if (fdset[1].revents & POLLPRI) {
lseek(fdset[1].fd, 0, SEEK_SET); // Read from the start of the file
len = read(fdset[1].fd, buf, MAX_BUF);
printf("\nMotion Detected, value=%c, len=%d\n",
buf[0], len);
usleep(5000000);
}
if (fdset[0].revents & POLLIN) {
(void)read(fdset[0].fd, buf, 1);
printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]);
}
fflush(stdout);
}
gpio_fd_close(gpio_fd);
return 0;
}