EasyManua.ls Logo

SeeedStudio Grove Series - Page 38

Default Icon
51 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
*/
/* Macro Define */
#define PIR_MOTION_SENSOR 39 /* sig pin of the PIR sensor */
#define LED RED_LED /* led */
#define ON HIGH /* led on */
#define OFF LOW /* led off */
#define _handle_led(x) digitalWrite(LED, x) /* handle led */
/* the setup() method runs once, when the sketch starts */
void setup() {
pinMode(PIR_MOTION_SENSOR, INPUT); /* declare the sig pin as an INPUT */
pinMode(RED_LED, OUTPUT); /* declare the red_led pin as an OUTPUT */
_handle_led(OFF);
}
/* the loop() method runs over and over again */
void loop() {
if(isPeopleDetected()) {
_handle_led(ON); /* if we detect a people, turn on the led */
} else {
_handle_led(OFF); /* found nobody, turn off the light */
}
}
/* judge if there is a people around */
boolean isPeopleDetected() {
int sensor_val = digitalRead(PIR_MOTION_SENSOR); /* read sig pin */
if(HIGH == sensor_val) {
return true; /* people detected */
} else {
return false; /* people un-detected */
}
}
RESULT
When a person comes closer to the PIR sensor, the red LED will be turned on.
TIPS
Now that you know how to use the PIR sensor to detect motion, try to pair this with other Grove
modules, i.e. buzz the buzzer or trigger the relay when motion is detected.

Related product manuals