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(); 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
  sensor_previous_state = sensor_current_state;
2017 update: lightguide for green and orange LEDs:
Update: LED lightguides from the scrap box
And voila!
Quite nice, discrete and works very well, for about 10€/$.

39 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?

  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

  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?

  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.

  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!

  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 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);

    void loop()
    if (digitalRead(sensorPin) == 1)
    TXLED1; // Turn on TX LED to indicate movement
    Mouse.move(0, -0);
    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?


  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.

  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):


    #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 ); KEY_LEFT_GUI ); ‘l’ );
    digitalWrite( LED_BUILTIN, LOW );

Leave a Reply

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