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.
On this pageOne evening
Muscles are electrically loud. When you clench your fist, the muscles in your forearm produce a burst of activity a thousand times larger than EEGElectroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. Related: Electrocorticography (ECoG)Electrocorticography (ECoG) Recording from electrodes placed on the surface of the brain under the skull, usually during epilepsy surgery evaluation; cleaner and sharper than EEG. , Magnetoencephalography (MEG)Magnetoencephalography (MEG) Recording the tiny magnetic fields produced by cortical currents with superconducting sensors in a shielded room; less blurred by the skull than EEG, and far more expensive. , Volume conductionVolume conduction The spreading of currents from a source through the conductive tissues of the head, which is why one source shows up on many electrodes and why scalp EEG is blurry. , Microvolt (µV)Microvolt (µV) One millionth of a volt. Scalp EEG is tens of microvolts; a AA battery is 1.5 million of them. . Glossary entry, and two snap electrodesElectrode The conductor that turns ion currents in tissue into electron currents in a wire; its chemistry and contact quality set the noise you live with. Related: Silver/silver-chloride (Ag/AgCl)Silver/silver-chloride (Ag/AgCl) The standard electrode material for biopotentials, because its chemistry produces a stable, low-noise, low-drift interface with salty gel. , Electrode impedanceElectrode impedance How hard it is to push current through the skin-electrode contact, typically 5 to 50 kilohms with gel and megohms dry, higher at low frequencies. , Half-cell potentialHalf-cell potential The DC voltage that appears wherever a metal meets an electrolyte; two electrodes with different half-cell potentials produce an offset that can swamp the signal. . Glossary entry 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 myoelectricMyoelectric prosthesis An artificial limb controlled by EMG from the remaining muscles. Related: Electromyography (EMG)Electromyography (EMG) Recording the electrical activity of muscles, hundreds of microvolts to millivolts, much larger than EEG.. Glossary entry controller, which is the control principle of most prosthetic hands sold today.
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 spikesSpike Shorthand for an action potential as seen by an electrode, a brief blip about a millisecond wide. Related: Action potentialAction potential The brief, all-or-nothing voltage spike a neuron fires, about a millisecond long and about 100 millivolts tall across its membrane. , Spike sortingSpike sorting Deciding which spikes on an electrode came from which neuron, by clustering their waveform shapes. , Extracellular recordingExtracellular recording Recording with an electrode near neurons rather than inside one; you see small, fast blips from nearby cells rather than the full membrane swing. . Glossary entry 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 EMGElectromyography (EMG) Recording the electrical activity of muscles, hundreds of microvolts to millivolts, much larger than EEG. Related: Myoelectric prosthesisMyoelectric prosthesis An artificial limb controlled by EMG from the remaining muscles. , Electroencephalography (EEG)Electroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. . Glossary entry controller is to rectify and smooth.
Parts
| Part | Where | Qty | Approx. |
|---|---|---|---|
| 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 store | 1 | $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, SparkFun | 1 | $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. | Amazon | 1 | $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. | Amazon | 1 | $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. | Amazon | 1 | $8 |
| Hobby servo (SG90), ×2 For muscle-to-motion. Two for $4.99, which is less than one from Adafruit. | Amazon | 1 | $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. | Amazon | 2 | $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. | Amazon | 1 | $16 |
| Total (prices drift; treat as a ceiling) | $88 | ||
Two routes
Route 1: the amplifier you already built. Take the single-channel EEGElectroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. Related: Electrocorticography (ECoG)Electrocorticography (ECoG) Recording from electrodes placed on the surface of the brain under the skull, usually during epilepsy surgery evaluation; cleaner and sharper than EEG. , Magnetoencephalography (MEG)Magnetoencephalography (MEG) Recording the tiny magnetic fields produced by cortical currents with superconducting sensors in a shielded room; less blurred by the skull than EEG, and far more expensive. , Volume conductionVolume conduction The spreading of currents from a source through the conductive tissues of the head, which is why one source shows up on many electrodes and why scalp EEG is blurry. , Microvolt (µV)Microvolt (µV) One millionth of a volt. Scalp EEG is tens of microvolts; a AA battery is 1.5 million of them. . Glossary entry 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 artifactArtifact Anything in the recording that did not come from the brain: blinks, muscle, heartbeat, movement, sweat, mains hum, electrode pops. Related: Blink artifactBlink artifact The large, slow deflection an eye blink produces in frontal channels, often ten times the size of the brain signal. , Independent component analysis (ICA)Independent component analysis (ICA) A method that unmixes multichannel data into statistically independent sources, so blinks and heartbeat can be identified and removed. , Artifact subspace reconstruction (ASR)Artifact subspace reconstruction (ASR) An automatic method that detects short high-variance bursts and reconstructs them from clean data; common in real-time pipelines. . Glossary entry and the electrodeElectrode The conductor that turns ion currents in tissue into electron currents in a wire; its chemistry and contact quality set the noise you live with. Related: Silver/silver-chloride (Ag/AgCl)Silver/silver-chloride (Ag/AgCl) The standard electrode material for biopotentials, because its chemistry produces a stable, low-noise, low-drift interface with salty gel. , Electrode impedanceElectrode impedance How hard it is to push current through the skin-electrode contact, typically 5 to 50 kilohms with gel and megohms dry, higher at low frequencies. , Half-cell potentialHalf-cell potential The DC voltage that appears wherever a metal meets an electrolyte; two electrodes with different half-cell potentials produce an offset that can swamp the signal. . Glossary entry offset are gone. The low-pass moves from 35 Hz to about 500 Hz (3.3 kΩ into 0.1 µF). Reduce the total gainGain How many times an amplifier multiplies its input; 1000× turns 50 microvolts into 50 millivolts, enough for an ADC to see. Related: Instrumentation amplifierInstrumentation amplifier A precision differential amplifier with very high input impedance and high CMRR, built from three op-amps and sold as a single chip such as the INA128 or AD8232. . Glossary entry to about 500, because EMGElectromyography (EMG) Recording the electrical activity of muscles, hundreds of microvolts to millivolts, much larger than EEG. Related: Myoelectric prosthesisMyoelectric prosthesis An artificial limb controlled by EMG from the remaining muscles. , Electroencephalography (EEG)Electroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. . Glossary entry is bigger. Sample at 1000 Hz.
Route 2: the AD8232 breakout. SparkFun’s board has an instrumentation amplifierInstrumentation amplifier A precision differential amplifier with very high input impedance and high CMRR, built from three op-amps and sold as a single chip such as the INA128 or AD8232. Related: Differential measurementDifferential measurement Measuring the difference between two electrodes rather than one electrode against ground, so anything common to both cancels., Common-mode rejection ratio (CMRR)Common-mode rejection ratio (CMRR) How much better an amplifier responds to a difference between its inputs than to a signal shared by both, in decibels; 100 dB means 100,000 times., Input impedanceInput impedance How hard the amplifier's inputs are to push current into; it must be far higher than the electrode impedance or the signal is lost in the divider.. Glossary entry, a driven-right-legDriven-right-leg circuit A third electrode fed with an inverted copy of the common-mode signal, so the body's hum is actively cancelled instead of merely rejected. Related: Common-mode signalCommon-mode signal A voltage that appears equally on both inputs of a differential amplifier, such as 60 Hz hum picked up by the whole body., Ground electrodeGround electrode The electrode that ties the body to the amplifier's zero and, in a good design, is driven to cancel common-mode hum.. Glossary entry circuit, and filtersFilter An operation that passes some frequencies and attenuates others; every filter also delays or distorts the timing of what it passes. Related: High-pass filterHigh-pass filter Removes slow drifts and DC offsets, passing everything above a cutoff; set it too high and you erase slow brain activity. , Low-pass filterLow-pass filter Removes fast content above a cutoff; the anti-alias filter is one, and so is the smoothing you apply to a noisy trace. , Notch filterNotch filter A very narrow filter that removes one frequency, usually 60 Hz mains hum; it also rings and distorts nearby content, so fix the hum at the source first. , Phase distortionPhase distortion Different frequencies being delayed by different amounts, which smears and shifts waveforms such as an ERP peak. . Glossary entry on one board. Its filtersFilter An operation that passes some frequencies and attenuates others; every filter also delays or distorts the timing of what it passes. Related: High-pass filterHigh-pass filter Removes slow drifts and DC offsets, passing everything above a cutoff; set it too high and you erase slow brain activity. , Low-pass filterLow-pass filter Removes fast content above a cutoff; the anti-alias filter is one, and so is the smoothing you apply to a noisy trace. , Notch filterNotch filter A very narrow filter that removes one frequency, usually 60 Hz mains hum; it also rings and distorts nearby content, so fix the hum at the source first. , Phase distortionPhase distortion Different frequencies being delayed by different amounts, which smears and shifts waveforms such as an ERP peak. . Glossary entry are set up for heart signals (0.5 to 40 Hz), so it attenuates most of the EMGElectromyography (EMG) Recording the electrical activity of muscles, hundreds of microvolts to millivolts, much larger than EEG. Related: Myoelectric prosthesisMyoelectric prosthesis An artificial limb controlled by EMG from the remaining muscles. , Electroencephalography (EEG)Electroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. . Glossary entry 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
Two snap electrodesElectrode The conductor that turns ion currents in tissue into electron currents in a wire; its chemistry and contact quality set the noise you live with. Related: Silver/silver-chloride (Ag/AgCl)Silver/silver-chloride (Ag/AgCl) The standard electrode material for biopotentials, because its chemistry produces a stable, low-noise, low-drift interface with salty gel. , Electrode impedanceElectrode impedance How hard it is to push current through the skin-electrode contact, typically 5 to 50 kilohms with gel and megohms dry, higher at low frequencies. , Half-cell potentialHalf-cell potential The DC voltage that appears wherever a metal meets an electrolyte; two electrodes with different half-cell potentials produce an offset that can swamp the signal. . Glossary entry 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 DRLDriven-right-leg circuit A third electrode fed with an inverted copy of the common-mode signal, so the body's hum is actively cancelled instead of merely rejected. Related: Common-mode signalCommon-mode signal A voltage that appears equally on both inputs of a differential amplifier, such as 60 Hz hum picked up by the whole body. , Ground electrodeGround electrode The electrode that ties the body to the amplifier's zero and, in a good design, is driven to cancel common-mode hum. . Glossary entry 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 calibrationCalibration The session at the start of BCI use in which the decoder is trained on this person, today, because brains and electrodes drift. Related: Transfer learningTransfer learning Using data from other people or other days to shorten calibration for a new user. . Glossary entry step.
- Choose a route and build or connect the amplifier. Confirm a flat trace with the electrodesElectrode The conductor that turns ion currents in tissue into electron currents in a wire; its chemistry and contact quality set the noise you live with. Related: Silver/silver-chloride (Ag/AgCl)Silver/silver-chloride (Ag/AgCl) The standard electrode material for biopotentials, because its chemistry produces a stable, low-noise, low-drift interface with salty gel. , Electrode impedanceElectrode impedance How hard it is to push current through the skin-electrode contact, typically 5 to 50 kilohms with gel and megohms dry, higher at low frequencies. , Half-cell potentialHalf-cell potential The DC voltage that appears wherever a metal meets an electrolyte; two electrodes with different half-cell potentials produce an offset that can swamp the signal. . Glossary entry shorted together.
- Apply three electrodesElectrode The conductor that turns ion currents in tissue into electron currents in a wire; its chemistry and contact quality set the noise you live with. Related: Silver/silver-chloride (Ag/AgCl)Silver/silver-chloride (Ag/AgCl) The standard electrode material for biopotentials, because its chemistry produces a stable, low-noise, low-drift interface with salty gel. , Electrode impedanceElectrode impedance How hard it is to push current through the skin-electrode contact, typically 5 to 50 kilohms with gel and megohms dry, higher at low frequencies. , Half-cell potentialHalf-cell potential The DC voltage that appears wherever a metal meets an electrolyte; two electrodes with different half-cell potentials produce an offset that can swamp the signal. . Glossary entry as described. Watch the raw trace: relax, clench, relax. Note the resting and clenched amplitudes in your notebook.
- 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.
- 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.
- Save a plot of the raw and envelope for one clench. Record the two calibrationCalibration The session at the start of BCI use in which the decoder is trained on this person, today, because brains and electrodes drift. Related: Transfer learningTransfer learning Using data from other people or other days to shorten calibration for a new user. . Glossary entry 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 classifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. Related: Linear discriminant analysis (LDA)Linear discriminant analysis (LDA) A simple, robust classifier that draws a straight boundary between classes; the workhorse of EEG decoding for two decades. , Support vector machine (SVM)Support vector machine (SVM) A classifier that finds the boundary with the widest margin between classes; strong on small datasets. , Cross-validationCross-validation Testing a model on data it never saw during training, repeatedly, so the reported accuracy means something. . Glossary entry turn this into gesture recognition.
What to read now
Why so small? has the table of how big each biosignal is; EMGElectromyography (EMG) Recording the electrical activity of muscles, hundreds of microvolts to millivolts, much larger than EEG. Related: Myoelectric prosthesisMyoelectric prosthesis An artificial limb controlled by EMG from the remaining muscles. , Electroencephalography (EEG)Electroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. . Glossary entry’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.