Raspberry Pi Pico Mouse Jiggler

by DMSCreates in Circuits > Raspberry Pi

940 Views, 2 Favorites, 0 Comments

Raspberry Pi Pico Mouse Jiggler

DIY Raspberry Pi Pico Mouse Jiggler

This is a great beginner, no-solder tutorial for getting started with a Raspberry Pi Pico. I wanted to create a mouse jiggler to keep my laptop from going to sleep while I was deep in problem-solving mode. I could just change the power settings, but where's the fun in that?

I'll be showing two different methods for how to create a mouse jiggler. One is a no-code solution provided by someone else. The other is one you can write yourself.

Feel free to watch the video tutorial, or follow along below.

Supplies

  1. Raspberry Pi Pico (or clone board)
  2. Appropriate USB cable to connect it to your machine

You'll also need the following for Method 2

  1. Thonny IDE
  2. CircuitPython (you'll need the uf2 file)
  3. adafruit_hid library (download the library bundle for your version of circuit python and extract it to find the right folder)

Method 1 - No Coding

pico.png
  1. Download the latest release (jiggler.uf2) from the releases page on https://github.com/argilo/pico-jiggler
  2. Plug in the Raspberry Pi Pico while holding the "BOOTSEL" button.
  3. Drag the jiggler.uf2 file into the "RPI-RP2" USB mass storage device that appears.

That's it, the pi will reboot and flash itself and you should see it start jiggling away.


Method 2 - Write It Yourself

Make sure you've downloaded and extracted circuit python and the library bundle mentioned in the supplies section. Then:

  1. Plug in the Raspberry Pi Pico while holding the "BOOTSEL" button.
  2. Drag the CircuitPython uf2 file into the "RPI-RP2" USB mass storage device that appears.
  3. Copy the adafruit_hid folder (from the library bundle) to the lib folder on the CIRCUITPY drive that appears.

Now we'll need to use Thonny to do the coding:

  1. Open Thonny
  2. Go to Tools > Options
  3. Select the "interpreter" tab
  4. Set the interpreter to CircuitPython with port set to automatic and click OK

Now Thonny should be connected to your Raspberry Pi Pico

  1. Click on File > Open and open code.py on the CircuitPython device (the Raspberry Pi Pico)
  2. Delete the existing code

Now to actually code the device, we'll want to write the following:

import usb_hid
from adafruit_hid.mouse import Mouse
from time import sleep

These lines import the usb_hid library, Mouse emulation, and the sleep command. Then we'll want to add something like the following:

m = Mouse(usb_hid.devices)

while True:
       m.move(-5, 0, 0)
       sleep(0.5)
       m.move(5, 0, 0)
       sleep(0.5)
       m.move(0, -5, 0)
       sleep(0.5)
       m.move(0, 5, 0)
       sleep(0.5)

That code does the following:

  1. Create an object (called 'm') to control the virtual mouse
  2. Create a loop that will run forever and perform the following actions:
  3. Move the mouse 5 pixels to the left
  4. Wait half a second
  5. Move the mouse 5 pixels to the right
  6. Wait half a second
  7. Move the mouse down 5 pixels
  8. Wait half a second
  9. Move the mouse up 5 pixels
  10. Wait half a second
  11. Repeat

You can test your code by running the program in Thonny.

Save the code onto the device and it should be good to go.

Finally, if you want to stop the CIRCUITPYTHON device from appearing every time you plug the Pico in, you can create a new file called boot.py and add the following to it:

import storage
storage.disable_usb_drive()

Thonny will still be able to open files from the device if you want to make changes while it's plugged in.

There are lots of ways to improve the code, for instance creating a parameterized jiggle function or creating multiple modes that can be operated by buttons, but I'll leave those as exercises to be completed by you, if you're interested!

Good luck and happy coding.