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€/$.

54 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

  22. I’m trying to build this project and it’s my first attempt at Arduino (sorry). I have realised that the board I purchased is the “Nano” not the “Micro Pro”. In my googling I’m having trouble determining if the Nano should be able to suit this project. The problem I am having is “‘USBDevice’ was not declared in this scope” which could be either it is not supported by the Nano or that I have some newbie library/programming error. Do you know if the Nano should be capable?

  23. The Nano is not compatible: it’s not a USB capable microcontroller.
    As specified in the article you need a Micro Pro. The Arduino Micro Pro has direct USB connectivity and don’t use a serial/USB adapter for programing/communication. When it works, it’s great but when buggy, because of the USB stack used in the bootloader for reprogramming, sometimes it can be a pain and a pair of tweezers for multiple reset are handy for recovery.

  24. Hi! I’m wondering if it is possible to make this inversely. So the sensor is on, but when motion is detected turns off. Would it be possible?

  25. This gadget sends a computer some even to wake it up.
    How do you imagine the opposite? Send an event to shutdown/suspend/lock screen?

  26. To lock the screen on PIR activity (win/mac key + L):

    #include

    #define PIR_PIN 6 // PIR signal input pin

    void setup()
    {
    pinMode( PIR_PIN, INPUT ); // PIR sensor pin as input
    digitalWrite( LED_BUILTIN, LOW );
    }

    void loop()
    {
    if ( digitalRead( PIR_PIN ) )
    {
    digitalWrite( LED_BUILTIN, HIGH );
    Keyboard.press( KEY_LEFT_GUI );
    Keyboard.press( ‘l’ );
    delay(100);
    Keyboard.releaseAll();
    delay(5000);
    digitalWrite( LED_BUILTIN, LOW );
    }
    delay(200);
    }

  27. Hi All.

    Initially may I say thanks to all that have created this project and posted incredibly great information on where to get the necessary equipment etc.

    This is my first attempt at constructing an Arduino or anything like it and definitely my first attempt at any type of coding. I have watched hours of YouTube and read Gigabytes of information and have had a great time doing so.

    However, I have come to a screaming halt with this project. I have managed to construct it and install the Arduino software and managed to flash the coding to it successfully. But unfortunately, that is where the success stops.
    I get a message as shown below via the Windows 10 64bit computer in Device Manager – Arduino Leonardo Properties.

    Device USB\VID_2341&PID_8036&MI_00\6&1b780c72&0&0000 requires further installation.

    I have tried in vain to find a solution, but I am struggling to make sense of it or come up with a way to make it work.
    I am hoping someone will have the answer for this and fingers crossed, it will be an easy simple fix for someone with extremely limited experience like me?

    The other issue I have is that the coding and explanation with the updated 2017 version as mentioned in a previous post by the creator of this project is the addition of a green and a yellow LED. I have all the LEDs and cabling etc. but am unsure as to what terminals to attach the LEDs to? I know it is blatantly obvious to some of you reading this, but I would sooner ask the obvious question avoiding the chance of damaging anything.

    Any assistance or advice to a complete novice would be gratefully be received so I can finally get this working. Many thanks in advance.

    Kindest regards.

  28. About your Windows issue, have a look at a tutorial how to make your development board works before going any further.
    For the LED, they are RX and TX LED, already present on the Arduino Micro Pro, you cannot access them directly, that’s why you need to use TXLED0. But good news, you can connect it to any GPIO and use the digitalWrite(RX_LED_PIN, HIGH); to set it HIGH or LOW

  29. Arduino: 1.8.13 (Windows 10), Board: “Sanguino, ATmega644 or ATmega644A (16 MHz)”
    C:\Users\cowbo\AppData\Local\Temp\arduino_modified_sketch_377798\sketch_sep10a.ino: In function ‘void setup()’:
    sketch_sep10a:12:3: error: ‘TXLED0’ was not declared in this scope
    TXLED0; // switch on TX green to show init/powered up (only available by macro)
    ^~~~~~
    C:\Users\cowbo\AppData\Local\Temp\arduino_modified_sketch_377798\sketch_sep10a.ino:12:3: note: suggested alternative: ‘TXEN0’
    TXLED0; // switch on TX green to show init/powered up (only available by macro)
    ^~~~~~
    TXEN0
    C:\Users\cowbo\AppData\Local\Temp\arduino_modified_sketch_377798\sketch_sep10a.ino: In function ‘void loop()’:
    sketch_sep10a:22:5: error: ‘USBDevice’ was not declared in this scope
    USBDevice.wakeupHost();
    ^~~~~~~~~
    sketch_sep10a:23:5: error: ‘Keyboard’ not found. Does your sketch include the line ‘#include ‘?
    Keyboard.press( KEY_CAPS_LOCK );
    ^~~~~~~~
    sketch_sep10a:23:21: error: ‘KEY_CAPS_LOCK’ was not declared in this scope
    Keyboard.press( KEY_CAPS_LOCK );
    ^~~~~~~~~~~~~
    sketch_sep10a:25:5: error: ‘TXLED0’ was not declared in this scope
    TXLED0; // great hackery in this: we have to force down the TXLED
    ^~~~~~
    C:\Users\cowbo\AppData\Local\Temp\arduino_modified_sketch_377798\sketch_sep10a.ino:25:5: note: suggested alternative: ‘TXEN0’

    TXLED0; // great hackery in this: we have to force down the TXLED

    ^~~~~~

    TXEN0

    sketch_sep10a:29:5: error: ‘TXLED0’ was not declared in this scope

    TXLED0;

    ^~~~~~

    C:\Users\cowbo\AppData\Local\Temp\arduino_modified_sketch_377798\sketch_sep10a.ino:29:5: note: suggested alternative: ‘TXEN0’

    TXLED0;

    ^~~~~~

    TXEN0

    exit status 1

    ‘TXLED0’ was not declared in this scope

    What am I missing here? When I try to upload I get these errors….

  30. Yes you are correct! After doing some research [which I should have done to start with] I would have purchased the right microcontrollers LOL I ordered the correct ones with USB communication. THANK YOU!

    As soon as they get here I’ll be back on track! Great project!

  31. Well I did get the code on both the Ardunio Micros…. problem for me is the wiring… I know ground is ground but using an actual Ardunio not sure where the other two wires connect to…. Sorry to be such a pain but I’m so close LOL

  32. Well the only other thing I didn’t take into account is that I bought actual Arduino Micros and the wiring is different. Being new to this I am a bit lost. I did find the ground but the other two wires…. If you could help me out that would be great! The other thing is can I do this without changing the code? Thank you so much for helping!

  33. Simply connect the wire to the correct pin, they are only, 3 of them: GND to GND, VCC to VCC, and the PIR output to a GPIO, in this code, it’s GPIO number 10.

  34. Sorry about the double post but Thank you for help! Had to ‘play’ a little as my PIR isn’t labeled but I got it! I’ll post a picture when it’s done!

  35. I thought I could post a pictures but I’ll just have to tell you about the project.

    I’m currently running Home Assistant on a 32″ touch screen. The ‘brains’ is a mini-itx and I couldn’t hardwire a pir like I could a Pi3 so this was the best option!
    It’s running on Unbuntu with xscreensaver to blank the screen after 1 minute of no activity. I plugged this in and right off the bat it’s working like a charm!!!

    I also have a ‘mirror’ in the bathroom that mirrors the main screen but the PIR sensor is not working on that one for some reason. I need to touch the spacebar on the keyboard to show the screen :(. That one is running on an old laptop board….. not sure why it’s not working but I’m sure there’s a way to get it to work….. even if I hit the ctrl key it comes back on so that’s not the issue.

    It’s like the board isn’t accepting the ‘key stroke’ via USB.

    Thank you so much for all the help!!! My main goal is accomplished Thanks to you!!!

Leave a Reply

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