June 6, 2021

Low cost mouse Jiggler with Pico or Neo Trinkey

Raspberry Pi Pico

Why?

Working from home means that my laptop can’t sleep. So I needed something that appears as a Human Interface Device (HID) and randomly sends a mouse input every three minutes or so.

Pre-requisites

You’ll need to install CircuitPython version 7.0.0 or higher first, otherwise you won’t be able to make the device identify as USB HID without rebuilding CircuitPython.

I used an Adafruit Neo Trinkey and a Raspberry Pico. Neither are expensive, I paid £6.30 and £3.60 respectively. Trinkey is a bit flashier, doesn’t need a cable, fits on a keyring and has the ability to bypass boot.py by pressing both capacitive keys.

Code

No point in having a version control system and barking oneself, so the code is on Github. Copy both boot.py and code.py to the device.

I’ll go over the Pico code here but the Neo is similar.

Make the mouse move

import usb_hid
import board
import digitalio
from time import sleep
from adafruit_hid.mouse import Mouse
from random import randint

HOW_LONG = 180    # Pause between jiggles in seconds

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

mouse = Mouse(usb_hid.devices)

def blink(times):
    for x in range(times):
        led.value = False
        sleep(0.5)
        led.value = True
        print(x)

def jiggle():
    for each in range(randint(1, 4)):
        x = randint(1, 50)
        y = randint(1, 50)
        mouse.move(x, y)
        mouse.move(-x, -y)
    sleep(HOW_LONG)

while True:
    led.value = True
    blink(3)
    jiggle()

Once we know that code is working and more importantly that it works the way you need it to then we can move on to the next step.

Make the device appear as just HID

The Pico hasn’t got an input so we can’t follow Adafruit’s advice. Once you’ve saved boot.py then you’ll need to use UF2 bootloader erase files for Pico.

I was able to avoid this with the Neo Trinkey by checking at boot time for both capacitive keys being active. Had to put in a delay too, otherwise it is difficult to time. Quite common working with embedded systems that you need to add delays.

import storage
import usb_cdc

storage.disable_usb_drive()    # Disable USB Drive
usb_cdc.disable()              # Disable REP

If it all goes wrong…

If it all goes wrong and you can’t access either device, then you can reset the flash memory.

Pico

Disconnect the Pico, hold the BOOTSEL button and keep it depressed while reinserting. When RPI-RP2 drive appears, copy the bootloader file from Adafruit. This will completely reset the Pico.

Trinkey Neo

Follow the process here.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close