Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active August 18, 2020 22:22
Show Gist options
  • Save bboyho/03ce16ee280695a108733cf9615919fa to your computer and use it in GitHub Desktop.
Save bboyho/03ce16ee280695a108733cf9615919fa to your computer and use it in GitHub Desktop.
PIR_Screensaver_wake-up.ino
/******************************************************************************
PIR_Screensaver_wake-up.ino
Modified by: Ho Yun "Bobby" Chan
June 4th, 2019
Original sketch for SparkFun's PIR Motion Detector
(https://www.sparkfun.com/products/13285)
Jim Lindblom @ SparkFun Electronics
May 2, 2016
========== DESCRIPTION ==========
Whenever the PIR sensor detects movement, it'll write the alarm pin LOW.
This will wake up a Pi using HID if it is in screensaver mode if you are
using this as a dashboard (e.g. Magic Mirror). This will turn off the monitor
to save power and give the display a rest.
========== HARDWARE CONNECTIONS ==========
The PIR motion sensor has a three-pin JST connector terminating it. Connect
the wire colors to an ATmega32U4 (e.g. Pro Micro or Arduino Leonardo)
or SAMD21 Mini/Dev like this:
- Black: D2 - signal output (pulled up internally by a 5V/3.3V Arduino)
- White: GND
- Red: 5V
Note: While the PIR motion sensor can work at 5V, it is recommended to add a
bypass jumper between the 78L05 voltage regulator's VIN and VOUT pin on the PIR
motion sensor. This will ensure that we are not violating the dropout voltage for
the 78L05 voltage regulator. You may notice false positives sporadically. For
more information check out the note from the PIR Motion Sensor Hookup Guide:
https://learn.sparkfun.com/tutorials/pir-motion-sensor-hookup-guide#min_voltage
Optional: Connect an LED and current limiting resistor
to pin 13 (if your Arduino doesn't already have an LED there).
This is for feedback to indicate that there is movement.
Development environment specifics:
Arduino 1.6.7
******************************************************************************/
//#include <Keyboard.h>
#include <Mouse.h>
#define Serial SerialUSB //debug with SAMD21, comment out if using an ATmega32U4
int proximity; //PIR motion sensor state
const int MOTION_PIN = 2; // Pin connected to motion detector
const int LED_PIN = 13; // LED pin - active-high
int j = 0;
int motionCheck[] = {1, 1, 1, 1, 1, 1}; // storing the last 6 alarms from the motion sensor
void setup(){
Serial.begin(9600); //for debugging
// The PIR sensor's output signal is an open-collector,
// so a pull-up resistor is required:
pinMode(MOTION_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH); //set Status LED on
//Keyboard.begin();
Mouse.begin();
//wait for the PIR motion sensor calibrate by not moving in
//front of it while LED ON
delay(15000);//wait for PIR motion sensor to settle down
for (int i = 0; i < 10; i++) {
digitalWrite(LED_PIN, HIGH);//set Status LED on
delay(50);
digitalWrite(LED_PIN, LOW); //set Status LED off
delay(50);
}
Serial.println("ATmega32U4/SAMD21 Ready to Wake up the Pi's Monitor If There is Movement!");
} //end setup()
void loop(){
for (j = 0; j < 6; j++){
motionCheck[j] = digitalRead(MOTION_PIN);
//Serial.println(motionCheck[j]);
}
// If the sensor's output goes low, motion is detected
if ((motionCheck[0] == 0) && (motionCheck[1] == 0) && (motionCheck[2] == 0) && (motionCheck[3] == 0) && (motionCheck[4] == 0) && (motionCheck[5] == 0)){
digitalWrite(LED_PIN, HIGH);
//Keyboard.write('z'); // send a 'z' to the computer via Keyboard HID
Mouse.move(50, 0, 0);// wake up Pi
delay(500);//small delay so that the computer has time to receive key
Mouse.move(-50, 0, 0);// move cursor back to where it was
Serial.println("Motion detected!");
}
else {
digitalWrite(LED_PIN, LOW);
}
delay(1000); // 1000 milli x60sec x 10min delay so there aren't a kajillion mouse triggers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment