Introduction: Making Sort of Palatable Music With an X-Band Motion Sensor

Making music with an X-Band radar, hmmm... wouldn't that be more fun than one person should be allowed to have? It's not that hard these days, with the right tools of course. As you can see in the image that accompanies this intro, we'll use an X-Band motion sensor by www.parallax.com. In the next step we'll see what else we need. But before we go there, listen to something that was generated with this sensor. It's in the Istrian scale, which makes it slightly haunting and mysterious:

Step 1: What Do We Need

First the hardware:

Next the software:

Step 2: Building the Electronics

We're going to connect the motion sensor to the Arduino and add a LED to it as well, to show something was detected by the motion sensor. You can leave out the whole LED part, but it's always fun when something blinks.

The Parallax motion sensor we're using has four pins, but we'll use only three of them: +5V, GND and OUT. You can leave the EN pin unconnected. The +5V pin gets its power from the Arduino, and the GND (ground) is shared with the Arduino. It helps to use the red and blue strips if your breadboard provides them. First connect +5V from the Arduino to the red strip and ground from the Arduino to the blue strip. Then you can use these several times. You can check the pictures in this step to see what we mean by that.

Be sure to connect the long leg of your LED (the positive leg or anode) to the resistor and the short leg (the negative leg or cathode) to the common ground. The 220 Ohm resistor is connected to digital pin 13.

Step 3: What It Looks Like

It now looks more or less like this. In my photo I have an Arduino ethernet shield on top of my Arduino Uno, but that doesn't matter much as the Arduino pins stay the same.

Step 4: Programming the Arduino - What Are We Going to Do

It's important to understand that our motion sensor just gives us a digital signal consisting of either a 0, if nothing is detected, or a 1 if something is inside the area that's being seen by the sensor. So there is no distance measurement or variable analog value to turn into a musical note. But we would like to have some influence over the value that comes from our Arduino that's somehow related to our movement in front of the sensor. So we will count "hits" during a small window in time and report the accumulated number of hits back as a number. The speed with which we move in front of the sensor will then have at least some relation to the number that we put onto the Arduino serial bus.

Step 5: Programming the Arduino - Code

// Radar to MIDI note.
// 10.525 GHz X-Band Motion Detector (#32213) radar by www.parallax.com.
// Arduino: 1.6.2 (Mac OS X), Board: "Arduino Uno".
// Hens Zimmerman <hz37@xs4all.nl>
// June 10, 2015.

// Constants.

const int baudRate = 9600;
const int ledPin = 13;
const int maxCounter = 1000;
const int maxMIDI = 127;
const int usLoopDelay = 50;
const int xBandPin = 8;

// Initial code to run once.

void setup() 
{
  // Assign modes to pins.
  
  pinMode(xBandPin, INPUT);
  pinMode(ledPin, OUTPUT);  

  // Start serial communication to Max 7.
  
  Serial.begin(baudRate); 
}


// Loop code to run perpetually.

void loop() 
{
  // Create a small window in time during which we will count radar hits.
  // counter variable will count the amount of hits, so reset it at the start.

  int counter;
  counter = 0;

  // Start window in time.
  
  for(int idx = 0; idx < maxCounter; ++idx)
  {
      // Read value of motion sensor.
      
      int sensorValue = digitalRead(xBandPin);
      
      // Did we get a hit? Increment the counter.
      
      if(sensorValue)
      {
        ++counter;
      }
      
      // LED shows current status of sensorValue.
      
      digitalWrite(ledPin, sensorValue ? HIGH : LOW);
      
      // A small delay keeps things tidy.
      
      delayMicroseconds(usLoopDelay);
  }
 
  // Only output to serial if some motion was detected.
 
  if(counter)
  {
    // Scale to 7 bits.
    
    char output = map(counter, 0, maxCounter, 0, maxMIDI);

    // Output the number in 7 bit range.
    
    Serial.print(output);
  }
}

Step 6: Compile and Upload

You can now compile your code and upload the resulting file to the Arduino. At this point, it should already be able to detect your motion in front of it. The red LED will blink if the motion sensor sees something. The parallax sensor is quite sensitive. In fact, it can detect your pets and see through glass. There is a potentiometer on the back of the sensor to adjust the sensitivity to your situation. A small screwdriver will let you adjust this.

Check that your LED lights up if you wave in front of the motion sensor.

Step 7: Max

I've made a patch in Max that you can use to turn the data from the motion sensor into a MIDI signal. It has two modes called "drunk" and "notes". "drunk" gives the most pleasant musical results, while "notes" sounds more random.

Basically what happens is that Max regularly polls the serial port. The incoming number is already in the MIDI range [0..127], because the Arduino code in the previous steps mapped the number to this range. The "drunk" mode uses Max' [drunk] object to semi-randomly walk through the whole range of valid MIDI note values. Although you have no influence over the direction this is going, it sounds most pleasant. I have also used the number to influence the delay between generated notes. Of course you can play around with the code to make it fit your needs.

Two important notes:

  1. Choose the right serial port in the top of the Max patch. It's the one that connects to your Arduino Uno. Only when the right serial port is selected you can check the checkbox that opens the serial port.
  2. Double click the [noteout] object on the bottom of the patch and choose "from Max 1" as your option. This will route MIDI output to a virtual MIDI device called "from Max 1", We will need this in our next step.

Here is the Max patch (copy and paste it into Max via the "New from Clipboard" menu item):

----------begin_max5_patcher----------
3168.3oc6as0baaiE9YmeEXz9h8TUVB.dsod6llrSS1IdSmljsc15NcnHgkX
CEgVdIxNYx+88b.HonjkjIkEc5tS8jXKBhaem6.mi93iNYzD40h7Qjul7KjS
N4iO5jSTMgMbR0ymLZdv0gIA4ptMJUrTN42GMV+pBw0EplSjAQSBRmV+h7ha
RDp2T2hrrHQTTbyBgd4Fo5N4WqdcZ473TnCpUgV03hfhvYwoS+sLQXgdXTWO
CtI7CeLg4ZaXNl3Xh+lwLLals3H0ZC6zujNZ0Jn2C5k.a7SO5Q3uF2QrGJmO
WjVbKv+8YAKlQjWQJSyCCRDQDXgVTVrahQWQK2wvwxzmZCnziivjZA3212zC
ZhY1BxSlFJSjY5wYZ3a56YZMV8IpuCE+jCmxc43m1Jkxxc6jJyCfTMWjmGLU
bKREyuGRH6P5fsGoCf5X63Qsb.pCkhDL68JcX4LXhGKxjERDMoAy0.c1G3tj
cIDYcF4YxxIIhvj3v2QJlIHoxBArmHvFEPHoPRxEEjKdwydQk.F1zkitJSNm
bQv0D5kiLHuYVbNYYbRBYhPMKp9Gmhc+JYF4FYYFLO+mRQZnHaLQXL0f7DXc
KjojWF+dgQ8VKINUDJKSU6O98VVl4Ya3Q8nJgYMugSYF1ftLJdZ4dXByzcvX
sNdBy6hk8ZTYugSkibi.xbYlfjIBxkoA.UkLuLOF5GICL2IFuUZK6nRas7UR
7TWtAGZxETE37iqgB9fQa6oRC+LxSmIk4BRTVY56TB3UrfZ5dvBXNCBmQBRi
VwmRhyKDojnXjFlbC1DxGmJREYAEfE72GjTJxMFH9E2zowNE+15BGJ+ZW5Br
g2vdXBvEFVa6NVFlUzLpkRFm6uWa6GQW+6B1RPRcPQsuUifRMnc2Gn49COnU
ZQCJpYfebuZiWZ8Cq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E2ZcLSbKV6P+r5hOa8s2G
rXw6EY4UaEE.FMO3204TVUXLv5lpeTsQFkIdebc+UYfcTPV3rXrTgJyzYQ3Z
Gc0DOBywdVZYbUlCfU9SO5+R5rb5r
-----------end_max5_patcher-----------

Step 8: Turning Random Notes Into a Scale in Ableton Live

The note output from Max is still quite random, not very musically pleasing. This is where Ableton Live steps in!

Start Ableton Live and create a MIDI track with an instrument of your choice. Bells and plucked sounds work very well. You can add some reverb to the output of the instrument as well.

Now before the MIDI instrument you will place a MIDI effect that will force all incoming notes into a scale of your choice. You find this object under MIDI Effects and it is called "Scale". You can try out different scales and hot swap them to find the one that suits your mood. The "Phrygian Mode" is one of my favourites.

If no sound comes from Ableton Live, be sure you have record enabled your MIDI instrument (the red rectangular button should light up for that channel). Also make sure "from Max 1" is visible in your Ableton Live preferences and has Track and Remote enabled.

Now have fun performing musical moves in front of your Arduino!