IntermediateCreative Technology

Motion-Controlled Music Instrument

Control pitch, volume and effects with your movements.

Time
2–3 h
Estimated cost
~£35
Hardware
ESP32-DevKitC V4 (38-pin)

Example code — not yet validated

Try it in the browser

What you will build

A gesture-controlled synthesiser in the browser, a little like a theremin. Tilt sideways to play notes up and down a musical scale, tilt forward to open a filter (brighter sound), and hold a button to make sound. Other buttons change the scale and the instrument preset.

How it works

The controller streams the same JSON as Project 11. The browser uses the Web Audio API: an oscillator makes the tone, a low-pass filter shapes it, and a gain node sets the volume. Roll picks a note from the current scale, so every position sounds in tune. Pitch moves the filter cutoff. Values are always limited ("bounded") so the sound never jumps to painful frequencies or volumes.

Input Sound
Roll (–45° … +45°) Note in scale (2 octaves)
Pitch (0° … 40° forward) Filter brightness
Button 1 (GPIO 25), held Play (gate)
Button 2 (GPIO 27) Next scale: major, minor pentatonic, blues
Button 3 (GPIO 32) Next preset: soft sine, warm saw, square lead
Button 4 (GPIO 33) Mute

Parts

Part Qty Purpose Required
XBOT Motion Controller (Project 11) 1 Input Yes
Laptop with Chrome or Edge 1 Synth engine Yes
Headphones or speaker 1 Listening Yes

Build steps

  1. Flash the XBOT Motion Controller (Project 11).
  2. Set your computer volume to about 30 % before starting.
  3. Open /projects/motion-music-instrument/play and click Start audio (browsers block sound until you click).
  4. Click Connect Controller, hold the controller level and click Calibrate.
  5. Hold button 1 and roll your wrist: notes step up and down the scale.
  6. Tilt forward while playing: the tone gets brighter.
  7. Try each scale and preset. Checkpoint: button 4 silences the sound instantly.

Try it in the browser

Interactive Preview. Without hardware, the mouse acts as the controller: horizontal position = note, vertical = brightness, hold click = play. Keys 1–4 act as the buttons. Touch works on tablets.

Code

Shared motion controller firmware and Web Serial example

Firmware: XBOT Motion Controller (Project 11), unchanged. Browser:

Download instrument.js

// instrument.js — XBOTNOW. Uses xbot-serial.js from Project 11.
import { readInput } from './xbot-serial.js';

const SCALES = {
  major: [0, 2, 4, 5, 7, 9, 11],
  minorPentatonic: [0, 3, 5, 7, 10],
  blues: [0, 3, 5, 6, 7, 10]
};
const PRESETS = [
  { wave: 'sine', q: 1 }, { wave: 'sawtooth', q: 4 }, { wave: 'square', q: 2 }
];
const ROOT_MIDI = 57;          // A3
const MAX_GAIN = 0.25;         // hard volume ceiling

let ctx, osc, filter, gain, scaleNames = Object.keys(SCALES);
let scaleIdx = 0, presetIdx = 0, muted = false, prev = 0, calib = { p: 0, r: 0 };

export function startAudio() {           // call from a click handler
  ctx = new AudioContext();
  osc = ctx.createOscillator();
  filter = ctx.createBiquadFilter(); filter.type = 'lowpass';
  gain = ctx.createGain(); gain.gain.value = 0;
  osc.connect(filter).connect(gain).connect(ctx.destination);
  applyPreset(); osc.start();
  requestAnimationFrame(tick);
}

function applyPreset() { osc.type = PRESETS[presetIdx].wave; filter.Q.value = PRESETS[presetIdx].q; }

function noteFromRoll(roll) {
  const scale = SCALES[scaleNames[scaleIdx]];
  const steps = scale.length * 2;                         // two octaves
  const pos = Math.round(((Math.max(-45, Math.min(45, roll)) + 45) / 90) * (steps - 1));
  const midi = ROOT_MIDI + 12 * Math.floor(pos / scale.length) + scale[pos % scale.length];
  return 440 * Math.pow(2, (midi - 69) / 12);
}

function tick() {
  const inp = readInput(calib);
  const down = inp.buttons & ~prev; prev = inp.buttons;
  if (down & 2) scaleIdx = (scaleIdx + 1) % scaleNames.length;
  if (down & 4) { presetIdx = (presetIdx + 1) % PRESETS.length; applyPreset(); }
  if (down & 8) muted = !muted;

  const now = ctx.currentTime;
  osc.frequency.setTargetAtTime(noteFromRoll(inp.roll), now, 0.02);
  const bright = Math.max(0, Math.min(40, inp.pitch)) / 40;          // 0..1
  filter.frequency.setTargetAtTime(300 + bright * 4000, now, 0.05);   // 300–4300 Hz
  const playing = inp.fresh && (inp.buttons & 1) && !muted;
  gain.gain.setTargetAtTime(playing ? MAX_GAIN : 0, now, playing ? 0.02 : 0.08);
  requestAnimationFrame(tick);
}

export function mute() { muted = true; if (gain) gain.gain.setValueAtTime(0, ctx.currentTime); }

Optional extension (separate page section): send the same values as Web MIDI messages to control a DAW or hardware synth.

Troubleshooting

Symptom Likely cause Check
No sound Audio not started or muted Click Start audio; check button 4 and system volume
Notes jump randomly Hand tremor at note boundaries Increase Smoothing; hold the controller with two hands
Clicks when notes change Instant frequency jumps Keep the setTargetAtTime smoothing
Sound continues when unplugged — Should not happen: stale data closes the gate; report as a bug

Safety and limitations

Start at low volume, especially with headphones. Sound never autoplays and a Mute control is always visible. Volume is capped in code (MAX_GAIN).

Checklist

Next project

Smart Posture Tracker: the same motion sensing, worn on your body.

Next project: Smart Posture Tracker