Arduino project: USB foot-operated mouse switch

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.Arduino project USB foot-operated mouse switch 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.
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.Arduino project USB foot-operated mouse switch Schematic

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.

 

For more detail: Arduino project: USB foot-operated mouse switch


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top