Arduino project: USB foot-operated mouse switch

usb-foot-operated-mouse-switch-arduino-apc-project

This foot pedal plugs into the Arduino case which plugs into your PC via microUSB cable.

Arduino microcontrollers can easily be used to power fun projects like robots and even sending tweets to Twitter but they’re versatile enough to turn up in the strangest of places – like under your feet.

We had a question from a reader about whether it was possible to create a foot-operated mouse switch to help reduce his repetitive strain injury (RSI). He needed to do a lot of mouse work but continually clicking and dragging with the left mouse button was causing strain. So the question was could an Arduino be programmed to provide a left mouse button click by using a foot pedal switch instead?

Introducing Arduino Micro

The answer is yes it can. However for the first time in this series we’ve found something the Arduino Uno board can’t do. So for this class we’re introducing the Arduino Micro a tiny microcontroller board about the size of an SD card that has all the features we need for the job. The Micro is the miniature version of the Arduino Uno follow-up called Leonardo.

What makes the Leonardo and this tiny Arduino Micro different to the Arduino Uno we’ve been using up until now is a completely different CPU. Instead of the old ATMEGA328P these newer boards use an ATMEGA32U4 microcontroller. The new chip comes with the same instruction set and 32KB of onboard flash plus it still connects to your Arduino IDE and runs the same basic code – the difference is the ATMEGA32U4 has a built-in USB controller and an extra 512 bytes of RAM. If you’ve ever wanted to make a simple USB device the Arduino Leonardo or the little Arduino Micro can make it happen.

  • Arduino Micro board: $8.20 (available at eBay)
  • Foot-switched pedal: $2.60 (available at eBay)
  • 6.35mm panel socket: $1.95 (available at Jaycar)
  • 83x54x31mm clear box: $2.95 (available at Jaycar)
  • TOTAL: $15.70

Parts needed

What we’re building in this class is essentially a foot-operated mouse button that plugs into your computer’s USB port. In terms of hardware we don’t need very much – you can shrink it down to a foot-operated switch pedal a small ABS plastic ‘jiffy’ box from Jaycar a 6.5mm panel socket and the Arduino Micro board. Buy most of the parts from eBay and you can do the whole project for under $20. You’ll also need a microUSB cable although the included with your smartphone should work just fine.

The Arduino Micro is incredibly cheap and available from a number of resellers on eBay for under $9 including shipping. Just make sure it’s the Arduino Micro you order and not the Arduino Nano – the Nano is a similar-sized board but it’s an Arduino Uno-era board that uses the old ATMEGA328P chip not the ATMEGA32U4 chip we need here. At the very least you need an Arduino Leonardo or Micro to make this project work because of the integrated USB controller.

The Arduino Micro uses the Atmel ATMEGA32U4 chip and has two rows of 13 pins.

 

The Arduino Nano can’t be used in this project and differs with two rows of 15 pins.

As for the foot switch you can find plenty of these on eBay for under $3. What surprised us is that they’re used in tattooing machines of all things and usually come with a 6.5mm TS (tip-sleeve) phono connector on the end making them ideal for our needs. You’ll need to also pick up a panel socket to match – you’ll find these at any Jaycar store (see the parts list opposite).

In the end time was tight so we decided to go with a deluxe foot switch from Jaycar that cost us $25. It’s built like a tank and can control 220VAC at up to 10A so it should take a good amount of wear and tear. The downside is the Jaycar one only comes with bare wire ends so you have to solder on the 6.35mm plug yourself. Doing this takes a little bit of patience so if you want a simpler option stick with an eBay foot pedal switch.

Deluxe Foot Switch

This $25 deluxe pedal comes with no plug requiring some DIY. eBay sells lighter pre-plugged models for under $5.

Mouse/keyboard library

The key to this project is the built-in USB controller in the ATMEGA32U4 chip coupled with two new libraries that come with the latest Arduino IDE: the ‘Keyboard’ and ‘Mouse’ libraries. What’s clever about these is that they allow us to take control of a PC’s mouse and keyboard so you can build your own keyboard and mouse-like USB peripherals that work with any computer – not just Windows computers. The libraries are designed so that you can hack into the mouse and keyboard functions of any Windows Linux or Mac OS X computer. Even better apart from just the odd key map change there’s nothing you need to change in your source code to get it working on any of these OSs.

In this project we’re using the ‘Mouse’ library to get an Arduino Micro to send left mouse button presses and clicks to the PC. As for the source code it doesn’t get much simpler.

Project code

You can download the source code from our web site at apcmag.com/arduino.htm as usual. However since it’s only 14 lines here’s the sketch for our foot-pedal operated mouse switch in its entirety.

void setup(){

pinMode(5INPUT_PULLUP);

Mouse.begin();

}

void loop(){

if(digitalRead(5) == LOW){

Mouse.press();

delay(100);

while (digitalRead(5) == LOW){

delay(10);

}

Mouse.release();

}

}

Here’s how it works. The setup procedure defines the Micro’s digital I/O pin 5 as an input. To save ourselves having to buy a 10kΩ resistor we’re using the pull-up resistor built into that (and every) Micro I/O pin. After that we initialise the ‘Mouse’ library.

We then hit the main loop procedure and it’s pretty simple: it continuously reads the input level of pin 5 and as soon as it becomes a digital ‘low’ (that is when we’re pressing the switch) we send a left mouse button press to the PC and head straight into a 100-millisecond delay. Why? We’re performing an exceptionally crude yet effective form of ‘switch debouncing’.

When you press a mechanical pushbutton switch like a foot pedal it doesn’t just instantly go from ‘off’ to ‘on’ or ‘on’ to ‘off’. For a short time it will make and lose contact hundreds of times (known as bouncing) between the two states until it settles on its new state. Our 100ms delay allows the bouncing to settle. It’s a crude form of debouncing – Arduino has a more complex method at arduino.cc/en/Tutorial/Debounce but for simplicity this works.

The simple circuit diagram of the Arduino Micro-powered mouse/keyboard controller.

Still if we just issued a mouse press whenever we detected pin 5 went low without debouncing we’d end up with hundreds of mouse presses waiting in the Windows buffer. Just to prove it take out the delay(100); line and see what happens.

Debouncing ensures we get just one trigger point from one foot pedal press and we do that here with a simple delay. Once the press has been detected and the 100ms delay has completed the code goes into a loop and remains there while the input at pin 5 remains ‘low’ (ie. the foot switch is being pressed). As soon as you take your foot off the switch pin 5 goes ‘high’ which triggers the end of the while loop and the sketch issues the Mouse.release() code telling Windows the mouse button has been let go.

The idea here is that you can use the foot switch the same way as the left mouse button: straight click or click and drag. The proof is in the pudding – we programmed our Arduino Micro with this exact code and it worked a treat.

Building the project

If you decide to go with the Jaycar foot switch it comes with no connector on the end and because the cable is designed to carry up to 220VAC at 10A it’s almost as thick as your little finger which means you’ll need a 6.5mm phono plug to anchor the cable into. The Jaycar foot switch has three wires creating two modes: normally open (NO) and normally closed (NC). You want the NO connection so use the white and green wires in the cable as shown in our circuit diagram.

Soldering the wires into the solder points on the plug is a little tricky but it’s good practise if you haven’t done it before. Just remember to slide the plug shroud on the cable before you start soldering and keep the soldering neat so the shroud will screw back down over the plug after you solder the cable. Once that’s done put your Arduino Micro board into a small clear plastic jiffy box.

Because the Arduino Micro is so small there are no screw holes. The quick option for anchoring the board is to use three layers of thick double-sided tape to hold it to the case lid with a small channel cut out on the end of the box to allow the microUSB port some air. We used some basic wood files here – anything more will cut through the ABS plastic far too quickly.

Just two wires to a 6.35mm phone socket is all the Arduino Micro needs.

On the other side of the case we put in the 6.5mm panel socket and soldered two wires from the socket tabs to the ground (GND) pin and pin 5 on the Arduino Micro via a couple of PCB pins. It doesn’t matter which wire goes to which pin as long as you connect that socket to these two pins. Just be careful when soldering the PCB pins to the board – don’t take too long and don’t use too much solder. The wire we used is just standard light-duty hook-up wire.

After that screw the lid into the box chassis and then plug the foot switch into the box and the box into your PC’s USB port with a microUSB cable. Arduino Micro boards come with a basic flashing LED sketch preinstalled so the yellow LED should start flashing once a second as soon as it gets power.

Programming the Micro

Fire up the Arduino 1.0.5 IDE and from the top menu select ‘Tools > Serial Port’ and choose the COM port that matches your Arduino Micro (you’ll find that Windows 7/8 automatically starts downloading the driver software for the board as soon as you plug it in). After that select ‘Tools > Board > Arduino Micro’. Now you can type in the code shown here or download the sketch from our web site. There are no libraries you need to add – everything else for this project is already included in the latest Arduino IDE. Once you’ve programmed the sketch in that’s it – it’s good to go. The microUSB port supplies power and handles mouse button commands just like a real mouse.

The Arduino Micro is much smaller than the Uno and includes a USB controller.

Project alternatives

What’s really cool about this project is you can easily expand it as the Arduino Micro can handle up to 16 foot pedals. It’d take a fair bit more hardware and coding than we’ve included here clearly but it’s basically more of the same thing in both cases.

Controlling your PC’s mouse isn’t the only thing our basic project can do – it can also issue keyboard key presses. So what can you do with that? One thing we’ve thought of is a data logger; instead of logging data to a device you can log text straight to a word processor or text editor. Use the Arduino Micro (or larger Leonardo) to measure inputs and based on the result you can send key text to the keyboard which ends up as text on your page.

Another idea from APC editor Dan Gardiner is a ‘boss key’ which automatically shows the desktop on any Windows computer.

Boss key project code

The code for creating a ‘show desktop’ boss key is only slightly different from our foot-operated mouse switch. We’re using exactly the same hardware and detecting when the foot switch pulls pin 5 low. As soon as it does this time it sends the Windows key and D key presses to the PC which activates the ‘show desktop’ keyboard shortcut on Windows. It even works if you’re in the Windows 8 Modern UI – it’ll bring you back to the traditional Windows desktop. Here’s the code.

 

char winKey = KEY_LEFT_GUI;

void setup(){

pinMode(5INPUT_PULLUP);

Keyboard.begin();

}

void loop(){

if(digitalRead(5) == LOW){

Keyboard.press(winKey);

Keyboard.press(‘d’);

delay(50);

Keyboard.releaseAll();

delay(500);

}

}

 

The ‘Keyboard’ library can work with basically all keys on a standard 101-key keyboard including Ctrl Shift and Windows keys. The first line in this code assigns the code KEY_LEFT_GUI to the character variable winKey – KEY_LEFT_GUI is the left Windows key while KEY_RIGHT_GUI is the right Windows key. Feel free to modify the sketch to send other keyboard shortcuts or add in more foot switches and create even more shortcuts.

Better still if you play games using keyboard commands you can use a foot switch to activate those keys. While I haven’t tried it there’s no reason why you couldn’t create accelerator and brake foot pedals for your favourite car racing games. Again because the Arduino Micro is tapping straight into the Windows mouse and keyboard APIs your Windows apps don’t need to know anything about the Arduino Micro board – it all just happens automatically.

Other ideas

How about designing and building your own mouse? Sure you can buy them for 10c at any computer store but the ‘Mouse’ library gives you the commands to control the mouse buttons as well as movement through the command Mouse.move(xValyValwheel) . The positions are relative to the current mouse cursor location although you could create your own mouse that runs via a joystick or even encoded wheels.

You could even create a basic X/Y plotting device that works via inputs from two LDRs (light-dependent resistors) or thermistors for example and plots the relative light or temperature levels in those directions. Ultimately you can control the mouse movement based on analogue or digital inputs connected to the Arduino Micro board. It’s just limited by your imagination.

Remember this!

The ‘Mouse’ and ‘Keyboard’ libraries literally take over the keyboard and mouse functions of your PC so you must ensure that you release any keyboard keys or mouse buttons as soon as possible and only activate them based on some input check or change. Otherwise you may end up locking up your computer.

Finding more information

The Arduino team has a code reference library at arduino.cc/en/Reference/MouseKeyboard with full details on the instructions available in both ‘Keyboard’ and ‘Mouse’ libraries along with a few basic examples. You should find enough information there to whet your appetite.

The complete source code for our foot-operated ‘show desktop’ boss key.