Sunday, January 13, 2013

Breath Sensing 101

In this post, I'll discuss very basic breath sensing. We'll use a Freescale pressure sensor to detect how hard a player is blowing into a tube, and we'll print out the values being read. In later posts, I'll show how to convert those raw sensor values into MIDI continuous controller messages that we can use for expressive control of a MIDI synthesizer.

What you'll need:
  • A microcontroller. In my examples, I will use the Teensy 2.0 microcontroller from PJRC. You can use any Arduino-compatible microcontroller for these examples, but once we start using MIDI, it will be a lot easier if you have the Teensy, as it has MIDI-over-USB support built in. This means that you can plug the Teensy into your computer's USB port and directly drive a software synthesizer. I have a Mac so I'll use Garage Band for the demos, but, you can use any instrument "host" that allows you to run Apple or VST instruments. Hosts are available for Macs, PCs, and Linux.
    By the way, I recommend ordering the Teensy with pins pre-soldered, so that you can easily plug it into a breadboard while prototyping.
  • A USB cable for the microcontroller.
  • A Freescale MPXV4006GP (pdf spec sheet). You can order one from Mouser Electronics (direct link). They are about $13.00 in single quantities.
  • A suitable breadboard and jumper kit for connecting the components together. Maker Shed is a great place to get things like this, and also has a bundle of a breadboard and a jumper kit. For prototyping, I prefer flexible jumper wires, which Maker Shed also has.
  • Some heat-shrink tubing, which we'll use to make an initial attachment to the pressure sensor. I would recommend getting an assortment of heat shrink tubing sizes either online or at Radio Shack - you'll use it a lot if you do any serious amount of hacking.
  • Some 1/4" drip irrigation tubing - a few feet will do. This should be available at any hardware store or garden supply center. We'll slip this tubing over the heat shrink attached to the sensor, and the inside diameter of the irrigation tubing is close enough to the outside diameter of the heat shrink that it makes a decent seal, even without gluing it.
  • A few pushbutton switches for use in later projects where we'll be simulating woodwind keys and trumpet valves. I like the tactile button assortment available from SparkFun, but any momentary pushbutton switch that can plug directly into the breadboard will work. If you get a "Getting Started with Arduino" kit, available from a number of places, it will probably include something suitable.
  • Some solderless headers, which we'll use to attach legs to the Freescale sensor. Since the sensor is a surface mount device, it won't directly plug into the breadboard. However, its pins use the same spacing as the holes in the breadboard, so soldering 4 pins on each side of the sensor will allow us to plug it in.
  • Soldering iron, solder, and some basic soldering skills.
Preparing the Pressure Sensor

As I mentioned, the Freescale pressure sensor is a surface-mount device. Typically, a machine will place the part on a circuit board and use hot air to melt a solder paste that affixes the part directly to metallic pads on the board. This is cheaper to make/assemble than the older "through-hole" type of part, where legs on the part go through the circuit board and are soldered into the hole. It's getting harder and harder to find through-hole versions of components, so some companies like SparkFun provide "break-out" boards, which a small circuit board with the surface-mount component soldered to it, and standard-spaced holes which can be soldered to. For example, look at SparkFun's breakout board for the VS1033D MP3 chip. The chip has 50+ very tiny pins which would be extremely difficult to hand-solder (some people can do it - not me).

Fortunately, the Freescale sensor we're using is much simpler. In fact, even though there are 8 pins on the device, only three are used (in this drawing, the active pins are the three on the right side - pins 2, 3, and 4.

What I did was use a Helping Hands to hold the sensor and a set of  of the header pins against the sensor legs, after I flattened out the legs so they pointed straight down. I then soldered each of the 4 header pins to the sensor pin it was touching, and repeated it with the 4 pins on the other side. The result looks like this:

Try to work fast so you don't overheat the sensor electronics.

In that photo you can also see how I attached some heat-shrink tubing (the red tube) to the input port of the sensor, and then slipped some irrigation tubing over that (the black tubing). Use a lighter to heat the heat shrink tubing where it mates with the sensor's input tube so it makes a tight seal.

Now we're ready to hook things up on a breadboard (click to view a larger version):

This shows a Teensy 2.0, so if you're using some other microcontroller, hook up pin 4 of the sensor to Analog input zero.

A schematic of this circuit is:

Once that's all wired up, attach the USB cable to the controller and hook it up to your PC. Start the Arduino development environment (if you're using a Teensy, you'll also want to install PJRC's Teensyduino add-on which makes it possible to use the Teensy just like any other Arduino device from within the dev enviroment. Here is a simple sketch that will just print out the values being read from the sensor:
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0
  int sensorValue1 = analogRead(A0);
  delay(100);        // delay 100 milliseconds (1/10 of a second)

If using a Teensy, make sure the "USB Type" option in the "Tools" menu is set to "Serial". Choose the correct board type and serial port from the Tools menu, then upload the sketch to the board.

To see the values being read, choose "Serial Monitor" from the "Tools" menu and set the baud rate to 9600. You should see a series of values being printed. Without blowing the tube, I see values of about 64, which probably corresponds to ambient air pressure. Blowing into the tube, you should be able to max out the sensor, meaning it will produce a reading of close to 1023 (the maximum value that the 10-bit analog-to-digital converter on the Arduino will produce).

In the next post, I'll describe how to map these raw values to MIDI continuous controller data, and how to use that to control MIDI synthesizers.

1 comment:

  1. A very nice descriptive article on pressure sensor used for breath analysis