USB motion sensor (PIR) to activate monitor

I have an always on net-book for weather forecast (yeah, when paragliding, you need the most up to date forecast)
It basically displays a full screen custom web-page with time and date and weather.

Its always on, not really good for the back-light and energy consumption.

So I got the idea of plugging a PIR sensor to wake up the screen when someone approach it.

Take an Arduino Micro Pro (4€ for the Chinese version), a PIR sensor (2€), a Hammond case (1€) and an USB A male from scrap (0€).

Let’s give a try, with a basic wiring, and this sketch to emulate keyboard thanks to it’s USB HID, the Arduino Micro Pro is a must.

Updated 05-01-2017:

  • code with USB wakeuphost and capslock instead of ctrl
  • green TX led always on
  • orange RX led only on motion
#include <Keyboard.h>

#define SENSOR_PIN 10   // Senor state input pin
#define RX_LED_PIN  17  // The RX LED has a defined Arduino pin
static bool sensor_previous_state = false;

void setup()
{
  pinMode(SENSOR_PIN, INPUT);  // PIR sensor pin as input
  pinMode(RX_LED_PIN, OUTPUT);     //  RX LED as an output
  digitalWrite(RX_LED_PIN, HIGH);  //  RX LED off
  TXLED0;                     // switch on TX green to show init/powered up (only available by macro)
  sensor_previous_state = digitalRead(SENSOR_PIN); // get sensor initial state (can be already true)
}

void loop()
{
  bool sensor_current_state = digitalRead(SENSOR_PIN);
  if ( sensor_previous_state == false   // looping until we detect a rising edge
       && sensor_current_state == true) {// when sensor state is trigged, it takes about 20 sec to recover
    digitalWrite(RX_LED_PIN, LOW);   // set the LED on
    USBDevice.wakeupHost();
    Keyboard.press( KEY_CAPS_LOCK );
    Keyboard.release( KEY_CAPS_LOCK );
    TXLED0;                     // great hackery in this: we have to force down the TXLED
    delay(1000);                  // wait a bit for the led
  } else {
    digitalWrite(RX_LED_PIN, HIGH);    // set the LED off
    TXLED0;
  }
  sensor_previous_state = sensor_current_state;
}
2017 update: lightguide for green and orange LEDs:
p70105-114417
Update: LED lightguides from the scrap box
And voila!
Quite nice, discrete and works very well, for about 10€/$.

31 thoughts on “USB motion sensor (PIR) to activate monitor

  1. This is really great! What are the wiring schematics though? I want to build this so I want to be sure to get this right. Thanks.

  2. As ive only played with the rpi and uno r3 I had no idea the pro was hid! lol.
    Have you worked out the solution as to stop it pressing ctrl constantly?
    Thanks

  3. This is great stuff. I have used your guide to create my own usb motion sensor. This is to wake my kitchen touchscreen. I have also updated the USBCore.h and script slightly to also support walking the pc when it’s asleep.

    More details on my blog
    http://digitalgeek.me/blogview.php?id=29

  4. Hi,
    I notice in your Schematic drawing no additional resistor but there is in the image above it.
    Is the additional Resistor a requirement?
    Regards
    Steve

  5. The resistor is optional, but it’s always nice to put one (from 10ohm to 1k), to adapt impedances and limit current.

  6. I’m planning something similar to this, using 4 separate sensors for 4 different keystrokes, but I’m a complete novice. would a single pro micro suffice for all 4 sensors?

  7. Hi. Just want to ask if you can create a micro sensor that beeps or blinks a led llight when someone is approaching or coming near on the sensor about 1 to 2 meters? Please email me if you can create this one. Very Interested.

    Thank you.
    Vener

  8. Sure, it’s quite easy to make, as a proof of concept, no problem.
    But if you need something, low power, reliable, and you are not an EE, then go to Amazon buy a end product

  9. Hello,
    Do you know any commercial product that does exactly this (emulate keyboard input on motion detection)? I need many of these so doing them is not really an option. Thanks!
    Gnagnu

  10. Nope, I’m not aware of similar “USB PIR sensor” as commercial product. Otherwise I would have bought one instead of spending 2hours hacking it.

    You can request a design on freelancer.com and find a company such seedstudio to manufacture it.

    If you find one, don’t hesitate to post it here.

  11. Since I happened to have issues with keyboard states, I changed to use mouse movements. If i had no-zero (e.g. 10) mouse movements, they were easy to see in Windows for debugging. I found that mouse movements of zero still trigger Windows. I also toggle the Pro Micro TX LED when motion is detected. Code:

    #include Mouse.h

    int sensorPin = 10;

    void setup()
    {
    pinMode(sensorPin, INPUT);
    Serial.begin(9600);
    }

    void loop()
    {
    if (digitalRead(sensorPin) == 1)
    {
    TXLED1; // Turn on TX LED to indicate movement
    Mouse.move(0, -0);
    delay(100);
    Mouse.move(0,0);
    delay(100);
    TXLED0; // Turn off TX LED
    }
    }

  12. Pingback: Portable soldering fumes extractor fan DIY (battery operated and rechargeable) | DotMana

  13. Sure, but as I previously said, as it’s hand made, it’s rather expensive, are you willing to buy anyway?

  14. hello, Total newbie question here, where do you put the code that you have written above? Do you program the board or does it run on your computer?

    Thanks!

  15. For a key to wake you could try using F13, there exist key codes all the way up to F24 but they do nothing so they should be perfect for this.

  16. Hi Tweepy,
    I’ve used your latest code (5 Jan 2017) with two new Arduino Micro Pro initially in good working order, but after loading the sketch got a desktop warning “USB device not recognised”, and in the Device Manager / USB Controllers folder the board appears now as “Unknown USB Device (Device Descriptor Request Failed)”.
    Both boards are frozen now in this state and all my attempts to reset them failed.
    Any idea what possibly went wrong, and what can I do to fix it.
    Many thanks, George
    Win 10 V1703, Arduino 1.8.1

  17. Hi George, thanks for your message. I’m not sure to be able to help you as I’m only on linux desktop, not Windows.
    But from my limited knowledge of the Arduino plateform, it appears the Micro Pro doesn’t have a dedicated programmer as the UNO or other model, but uses the main CPU as bootloader at startup then launches your sketch.
    When fiddling with this Micro Pro, I found out that when your software overflows or crash, it reboots in loop and never gives you access to the bootloader to reupload a new sketch. To workaournd this, with a wire, connect RST and GND, then upon releasing, hit Upload sketch. While tricky, it seems to work for me.

  18. Thanks for your reply, Tweepy.
    I have another machine around running Ubuntu. Are you suggesting that it might be easier to salvage these two boards on that machine?

  19. No, I’m not saying that, Windows should be fine, it’s just you have to make an hardware reset with the RST pin just before uploading the sketch.

  20. I’ve tried this several times before writing to you, but didn’t work. The USB error message disappears for a couple of seconds, but in this time the USB port on Arduino IDE stays greyed out so nothing can be uploaded.

  21. Hi Tweepy,
    I’ve managed to load the sketch, the CTRL-key is pressed and released at motion (I can see it on the on-screen keyboard) but the computer doesn’t wakeup. BTW the computer wakes up when the CTRL key is pressed on the physical keyboard.
    Any ideas why is this happening? Many thanks, George

Leave a Reply

Your email address will not be published. Required fields are marked *