Emulate a USB Mouse using Circult Playground Express Accelerometer and Circuit Python

in programming •  8 months ago

2018-06-09 16.17.17.jpg

Previously we built a prank to demonstrate HID over USB but this project will be a more realistic real-world use.

Arduino and Raspberry Pi

Again, if you are an Arduino or Raspberry Pi fan you could still do the same thing, but you will need two buttons, resistors and an accelerometer. The Circuit Playground Express from Adafruit just has these on board, making it easier.

Ther very same accelerometer sensor breakout is available for Arduino and Raspberry Pi and works over I2C, just the same! It is even 3.3v. Find the Arduino C++ library here

How it works

We will control the mouse on our PC by tilting the CPX and using the two board-mounted buttons as left and right click. The onboard LED lights up when buttons are pressed.


Get the full code in this Gist

# for delay
import time

# for mouse emulation
from adafruit_hid.mouse import Mouse

# the cpx
import board

# buttons
from digitalio import DigitalInOut, Direction, Pull

# accelerometer
import adafruit_lis3dh

# i2c
import busio

# set up the on-board LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

# right button
rbutton = DigitalInOut(board.BUTTON_A)
rbutton.direction = Direction.INPUT
rbutton.pull = Pull.DOWN

# left button
lbutton = DigitalInOut(board.BUTTON_B)
lbutton.direction = Direction.INPUT
lbutton.pull = Pull.DOWN

# mouse
mouse = Mouse()

# accelerometer
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
lis3dh.range = adafruit_lis3dh.RANGE_8_G

# infinite loop
while True:

    # if either button is pressed, light up the LED
    if (rbutton.value) or (lbutton.value):  # button is pushed
        led.value = True

        # if it is the right button, emulate right-click
        if rbutton.value:

        # if left, left-click
        if lbutton.value:
    # otherwise, LED off
        led.value = False

    # get the acceleration
    x, y, z = lis3dh.acceleration

    # log to plotter
    print((x, y, z))

    # if tilted right, move right
    if (x > 1):

    # ... move left
    elif (x < 0):

    # up
    if (y > 1):

    # down
    elif (y < 0):

    # otherwise blank

    # tiny delay then loop!

Does it work?

Yes, with practice!

It is amazingly sensitive, to the point where the USB cable can add just enough tilt when the device is at rest on the desk.

Maybe making the tilt start at a more aggressive angle would help that with some testing.

The point however is made - the CPX can be used to create your own input device, and that is pretty cool!


Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Interesante y muy atractiva la publicación..

I didn't know it was capable of that.

Might be fun to make a midi instrument out of it.

Congratulations This post has been upvoted by SteemMakers. We are a community-based project that aims to support makers and DIYers on the blockchain in every way possible.

Join our Discord Channel to connect with us and nominate your own or somebody else's posts in our review channel.

Help us to reward you for making it ! Join our voting trail or delegate steem power to the community account.

Your post is also presented on the community website www.steemmakers.com where you can find other selected content.

If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.

Hi @makerhacks!

Your post was upvoted by utopian.io in cooperation with steemmakers - supporting knowledge, innovation and technological advancement on the Steem Blockchain.

Contribute to Open Source with utopian.io

Learn how to contribute on our website and join the new open source economy.

Want to chat? Join the Utopian Community on Discord https://discord.gg/h52nFrV