An USB Inertial Mouse with FreeIMU and Arduino Leonardo in 10 lines of code

Submitted by fabio on Mon, 2012-07-23 18:20.

Today the Arduino team officially presented the Arduino Leonardo with a video from Massimo Banzi, while it's been a while that the board has been launched and available for sale.

I really like the Arduino Leonardo and I'm already using it in some projects.. today however, I decided to make a simple little example program which shows the potential of the Arduino Leonardo.

I connected one of my FreeIMU to the Leonardo, wrote a bunch of lines of code and then, I had a simple USB inertial mouse! The code for this example is attached below.

Here is a video demonstration and a little explanation on how it works!
Kudos to the Arduino team for this great board!

AttachmentSize
FreeIMU_inertial_mouse_leonardo.ino1.14 KB

Have you gotten to get it to work with Bluetooth?

Submitted by Skyler Greenman (not verified) on Sat, 2012-08-04 06:46.

Hey Fabio, I've followed much of your work, which has helped me immensely in my school projects. I just built a similar mouse to work with the Leonardo and an ArduIMU v3. The code is working when using the USB connection; however, I can't get it to work over Bluetooth. I can transmit and receive data via BT, but I can't get the actual mouse to move. I think the mouse.move()command works through the USB connection (using the emulated VCP port) but not through the UART hardware which allows one to connect via BT (the second serial port--If I understand this all correctly). I would greatly appreciate any help you could give me--your knowledge and experience greatly surpasses mine.

One more thing

Submitted by Skyler Greenman (not verified) on Sat, 2012-08-04 08:56.

I forgot to add that when I connect via USB, my Device Manager shows the Leonardo as an HID and gives it 2 serial COM ports. But when paired and then connected via BT, it doesn't show as an HID--it just gives the 2 COM ports.

Unfortunately, it's not that

Submitted by fabio on Sat, 2012-08-04 09:22.

Unfortunately, it's not that simple. The Mouse class of the Arduino Leonardo will work only when the Leo is connected to the USB. It won't work when connected via Bluetooth. As you said, your bluetooth module creates a Serial port for you, but not a Keyboard/Mouse device.

So, in order to implement something like that you have two possibilities:

  • Get a bluetooth module with HID capabilities (eg: this one) and sort out how to program it to mimic a mouse
  • Write a small program to be run on the host computer which would decode the data coming from your Bluetooth serial port and convert it into mouse movements. I did something like that using Python with PyMouse and PySerial

Hope this helps.

Yes, thank you! That clears

Submitted by Skyler Greenman (not verified) on Sun, 2012-08-05 00:03.

Yes, thank you! That clears everything up for me. Do you have any idea why Arduino haven't implemented the Mouse class to work over serial(BT)? the only Human Interaction development anyone cares about it is wireless. Few people would actually use an interface device that is still tethered.

Inertial mouse

Submitted by Hypnotist (not verified) on Sat, 2012-07-28 15:11.

Hello, I've done similar inertial mouse adding wireless bluetooth technology: http://www.youtube.com/watch?v=iqPQRv7-3Zc
By the way, your blog is priceless, keep up the good work.

Nice!

Submitted by fabio on Sat, 2012-07-28 16:08.

Nice!

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a personal or company website insert its address in the form http://www.example.com/ .
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <small> <del> <img> <h2> <h3> <h4> <b> <video> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may insert videos with [video:URL]
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options