↑↓ move↵ open[] previous / next lesson? all shortcuts
Appearance
Theme
Type size
Reading

Focus mode hides the sidebars. Press f to toggle it anywhere.

Keyboard shortcuts
/
Search
[ ]
Previous / next lesson
t
Jump to the table of contents
r
Review queue
f
Focus mode
?
This panel
ProjectOne eveningAbout $5 more after Project B, $90 from an empty benchTier 1

Project C: Muscle to motion

Forearm EMG drives a servo, a game, or a cursor. The fastest reliable "control something with your nervous system" you can build.

AssumesThe safety charterSpineAnalog / mixed-signal hardwareDecoding / signal processing / ML
On this pageOne evening
  1. Two routes
  2. Electrodes
  3. Software
  4. Going further
  5. What to read now

Muscles are electrically loud. When you clench your fist, the muscles in your forearm produce a burst of activity a thousand times larger than , and two snap over them will pick it up with almost no fuss. Amplify it, smooth it into an envelope, and map the envelope to a servo angle, and you have built a one-channel controller, which is the control principle of most prosthetic hands sold today.

Predict before you look

A single motor unit fires at maybe 10 to 30 times a second. When you clench hard, what does the raw EMG look like?

Dense noise whose loudness tracks effort. Hundreds of motor units fire asynchronously, and their overlap into something that looks like noise between about 20 and 500 Hz. The information is in the envelope, not the waveform, which is why step one of every controller is to rectify and smooth.

Parts

PartWhereQtyApprox.
9 V batteries, ×4, and snap connectors
$9.69 for four Energizer cells plus $3.59 for three snap connectors (Amazon B0CR8RKQ5Q). Two batteries give you a ±9 V split supply. Never power anything touching skin from the wall or from a laptop USB port.
Amazon, any store1$13
AD8232 heart-rate monitor breakout
A complete biopotential front end on a board, and the clone comes with a cable and pads. SparkFun's version is $24.41 and has better documentation and a known-good layout. Use it to compare against your own amplifier, and to see what a good DRL looks like.
Amazon, SparkFun1$11
Arduino Nano clone (ATmega328P, CH340), ×3
A three-pack is cheaper than one genuine board and you will kill one. The official Nano Every is $14.90 for a single board if you want Arduino's support. Either has the 10-bit analog input the Web Serial plotter needs; CH340 boards work with Chrome's Web Serial.
Amazon1$13
Disposable snap ECG electrodes, bag of 50
3M Red Dot, $11.40 for fifty. Clip the alligator leads onto the snap studs rather than buying leads; a set of four 3.5 mm snap leads is $9.99 if you want them tidy.
Amazon1$12
Full-size breadboard, ×2, and jumper wires
Two 830-point boards, two 400-point boards, and 120 jumpers for $7.99. Adafruit's single premium board is $5.95 and its contacts grip better.
Amazon1$8
Hobby servo (SG90), ×2
For muscle-to-motion. Two for $4.99, which is less than one from Adafruit.
Amazon1$5
INA128 instrumentation amplifier (DIP-8), ×2
A two-pack is $9.99. TI's own INA128P is $18.99 each and the part is on a discontinuance notice, so the marketplace two-packs are grey stock; they work, but if one is noisier than the datasheet that is why. The AD620 or INA118 are interchangeable for our purposes. Get the through-hole DIP version for breadboards.
Amazon2$10
Resistor and capacitor assortment
$5.69 for 600 1% metal-film resistors, plus $9.99 for a 480-piece ceramic capacitor kit (10 pF to 10 µF, Amazon B0D2GVNDHY). 1% metal-film matters for the instrumentation amplifier's balance.
Amazon1$16
Total (prices drift; treat as a ceiling)$88

Two routes

Route 1: the amplifier you already built. Take the single-channel amplifier from Project B and change two capacitors. The high-pass moves from 0.5 Hz to about 20 Hz (0.1 µF into 82 kΩ) so that motion and the offset are gone. The low-pass moves from 35 Hz to about 500 Hz (3.3 kΩ into 0.1 µF). Reduce the total to about 500, because is bigger. Sample at 1000 Hz.

Route 2: the AD8232 breakout. SparkFun’s board has an , a circuit, and on one board. Its are set up for heart signals (0.5 to 40 Hz), so it attenuates most of the band, but the envelope still comes through well enough to drive a servo, and it takes fifteen minutes. Use this route if you want the demo tonight, and route 1 if you want to understand it.

Electrodes

How to place electrodes to do an EMGSpes Medica1:51Open on YouTubeSkin prep and snap- placement along a muscle, in under two minutes.

Two snap about three centimetres apart along the flexor muscles on the inside of the forearm, a hand’s width below the elbow, going to the two amplifier inputs. A third on the bony point of the elbow as the or reference. Clean the skin with alcohol first. Clench: the trace should explode. Relax: it should be nearly flat.

Software

On the Arduino, three lines of real work per sample. Subtract the midpoint (512), take the absolute value, and run an exponential moving average with a time constant of about 100 ms. That number is the envelope. Map it from its resting value to its clenched value onto 0 to 180 degrees, write it to the servo. Send the raw sample and the envelope out over serial as two channels so the plotter shows both.

// inside the 1 kHz sampling loop
int raw = analogRead(A0) - 512;
float rect = abs(raw);
env = env + 0.01f * (rect - env);           // ~100 ms smoothing at 1 kHz
int angle = constrain(map((int)env, restLevel, clenchLevel, 0, 180), 0, 180);
servo.write(angle);
Serial.print(raw); Serial.print(','); Serial.println((int)env);

Calibrate restLevel and clenchLevel by watching the envelope on the plotter for a few seconds relaxed and a few seconds clenched, and hard-coding the numbers. Later you will make this automatic; for now it teaches you that every biosignal system has a step.

  1. Choose a route and build or connect the amplifier. Confirm a flat trace with the shorted together.
  2. Apply three as described. Watch the raw trace: relax, clench, relax. Note the resting and clenched amplitudes in your notebook.
  3. Add the rectify-and-smooth code. Watch the envelope channel follow your clench with a small lag. Change the smoothing constant and see the lag change.
  4. Connect the servo (signal to pin 9, power from the Arduino 5 V). Map the envelope to angle. Clench slowly and watch the horn follow.
  5. Save a plot of the raw and envelope for one clench. Record the two numbers.

Going further

Two channels, flexors and extensors, and a servo that goes both ways. Drive a game instead of a servo: Python reads the serial port and presses a key when the envelope crosses a threshold. Add a second threshold for a “hard” clench and you have two commands, which is the control scheme of a two-site prosthetic hand. Then read the armband project, where eight channels and a turn this into gesture recognition.

What to read now

Why so small? has the table of how big each biosignal is; ’s place on it explains why this project was easy. The Phase 5 control project is what this becomes when it is for someone else.

Recall
Why is the first processing step in every EMG controller rectify-and-smooth?
EMG is the overlapping activity of hundreds of asynchronous motor units and looks like band-limited noise. Its information is in the envelope (loudness), not the waveform, so you take the absolute value and average.
Recall
Why can't you notch out 60 Hz from EMG the way you might from EEG?
EMG's useful band (about 20 to 500 Hz) contains 60 Hz, so a notch removes signal along with hum. Hum has to be fixed at the source.