Skip to content
Carla Prados

Software & notebooks

An EMG mouse, rewritten in MicroPython

Code study · 2026

The control code of a one-week assistive-technology project, ported from an Arduino sketch to MicroPython — same gains, same thresholds, same decisions, and a much clearer view of what the original was actually doing.

What it is

In December 2025 my team built a mouse driven by the arm’s muscles: five channels of surface EMG, one to click and four to move the cursor. It ran on an Arduino Leonardo, from a 199-line sketch I wrote during that week.

Afterwards I rewrote the control code in MicroPython, for two reasons. The original was coursework, and university rules do not let me publish it. And I wanted an excuse to try MicroPython on a real problem instead of a blinking LED. The port is public; the sketch is not.

The rule for the port was to change the language, not the behaviour. Every gain, threshold, smoothing constant and cursor step is copied from the sketch. If the two versions disagree on what the cursor does, the port is wrong.

Why the board had to change

MicroPython cannot run on the Leonardo: its ATmega32U4 has 2.5 KB of RAM. The port targets a Raspberry Pi Pico, which can act as a USB mouse just as the Leonardo did. That has two consequences for the hardware:

  • Five analogue inputs. The Pico exposes three, so the channels are read through an MCP3008, an 8-channel SPI ADC. It is 10-bit like the Leonardo’s, so the thresholds carry over.
  • 3.3 V instead of 5 V. The front end was designed to output up to 5 V. Each channel now goes through a 5.1 kΩ / 10 kΩ divider, and the code scales the reading back up, so every threshold stays in the original’s volts.

The part worth reading

Every loop, each channel is read, smoothed with an exponential moving average, multiplied by a gain and compared with a threshold. In the sketch that is written out five times, once per channel. In the port it is one small class, and the loop is left with nothing but the decision:

# Click and horizontal movement exclude each other, click first;
# vertical movement is decided independently, up before down.
if ch["click"].active:
    click(mouse)
elif ch["left"].active:
    burst(mouse, cfg.BURST_X, -1, 0)
elif ch["right"].active:
    burst(mouse, cfg.BURST_X, 1, 0)

if ch["up"].active:
    burst(mouse, cfg.BURST_Y, 0, -1)
elif ch["down"].active:
    burst(mouse, cfg.BURST_Y, 0, 1)

This is the design, and in the sketch it was buried under nearly 150 lines. Two if chains, not one, is what lets the cursor move diagonally. Putting click first in its chain means a click is never also a move.

The cursor movement shows the same thing at a smaller scale. The sketch moved the cursor in an eased burst — four steps of 5 px, then four of 4 px — and wrote every step out by hand as its own move and delay, once for each of the four directions.

In the port the burst is data, BURST_X = (5, 5, 5, 5, 4, 4, 4, 4), and one function walks it in any direction. All the tuning now lives in config.py. Changing how the cursor feels means editing one tuple, not 32 Mouse.move calls.

What was wrong with the original

Porting it line by line meant reading every line, and some of them do not do what they say.

The enable input compared counts, not volts. Cursor control was meant to work only while an enable pin was high, so the user could rest without the cursor drifting. The sketch read that pin with analogRead and tested > 3. Everywhere else in the code, 3 would mean volts. Here it means 3 counts out of 1,023, about 15 mV, so almost any voltage on the pin counted as “high”, and so could the noise of an unconnected pin. The port reads it as a digital input with a pull-down resistor, which is what it always was.

Half the gains do nothing. The gain is applied after smoothing and before a fixed threshold, so the gain and the threshold only ever act as a ratio. The left channel’s gain of 3 against a threshold of 9.6155 is a threshold of 3.21 V. The right channel’s 5 and 15.842 is 3.17 V. The sketch’s comments say the gains were there “to make thresholding easier”, and during calibration they did spread the values out, but they never changed a decision. I kept them in the port so the values stay recognisable from the original.

The smoothing is defined in loops, not in time. ALPHA = 0.2 weights the newest sample, but the time between samples depends on how long the loop takes, and that changes. Serial printing slows it down. A burst blocks it for 45 ms, and a click for 50 ms, and during that time no channel is read. The filter behaves differently while the user is moving the cursor than while they are still. A fixed sampling rate would fix that, and that would be a change of behaviour, not a port, so it is not in this version.

Why I kept it

The analogue front end was the hard part of that week, and it is on the project page. The code was written fast, to work by Friday, and it did. Rewriting it was a way to find out what it actually did. The answer was mostly what I meant, with three exceptions that never showed up in the demo.

The repository holds the MicroPython port and the wiring for the Pico and the MCP3008. The original Arduino sketch stays with the coursework.