Smartphone audio port data collection cookbook

The audio port of a smartphone is designed to send and receive audio but can be harnessed for portable, economical, and accurate data collection from a variety of sources. While smartphones have internal sensors to measure a number of physical phenomena such as acceleration, magnetism and illumination levels, measurement of other phenomena such as voltage, external temperature, or accurate timing of moving objects are excluded. The audio port cannot be only employed to sense external phenomena. It has the additional advantage of timing precision; because audio is recorded or played at a controlled rate separated from other smartphone activities, timings based on audio can be highly accurate. The following outlines unpublished details of the audio port technical elements for data collection, a general data collection recipe and an example timing application for Android devices.


I. Audio port technical elements for data collection
The audio port physical interface to the smartphone is a 4-pole jack connecting to some external device.Table 1 presents the American Headset Jack (AHJ) standard used by many smartphones for connections to the 4-pole jack.Figures 1 and 2 present two similar measurement circuits based upon the same resistor-capacitor network, Fig. 1 circuit for temperature measurements [1] and Fig. 2 circuit with a pair of photoresistors acting as gates for timing measurements of a moving object [2].
Although timing and temperature measures are obviously different, the circuits and data collection methods are quite similar.In each circuit, a variable resistor connects the speaker audio out-    put to the microphone input; the speaker output is a wave which peak amplitude at the microphone input varies with the variable resistor, greater resistance resulting in lower peak amplitude.Figure 3 illustrates the effect of a photoresistor on the microphone signal amplitude while a moving object momentarily blocks and reduces the illumination reaching the photoresistor -as the illumination decreases, resistance increases and peak amplitude decreases.
The microphone input amplitude is digitally sampled (44100 samples per second is common) over time as displayed in Fig. 4.Each digital value represents the speaker output amplitude as changed by the circuit, then received and digitized as the microphone input.
A recipe for data collection with Fig. 1 and 2 type circuits generally is: 1. Choose a variable resistor to measure some phenomena such as force, humidity, etc.
2. Generate a wave of fixed frequency and peak amplitude through the speaker output and sample it as the microphone input as illustrated in Fig. 4.
3. Detect the significant elements of the microphone input.Significant for timing applications, wave peaks can be detected by the simple method described in Fig. 4.
4. Convert the significant elements to corresponding data.For example, by recording a
range of peak amplitudes produced by the circuit of Fig. 1 and the corresponding temperatures, an equation fitted to that amplitude vs. temperature data can convert the circuit amplitude output to temperature.

II. Timing data collection recipe
For timing of a moving object, some form of gate is needed to detect entry and exit.Ideally, gates are binary, they only open or close with no intermediate states, unfortunately reality does not represent the ideal case.timing applications, the circuit of Fig. 2 includes one or more photoresistors (gates) connected serially so that when all are illuminated (closed) resistance is at its minimum and the peak signal amplitude input to the microphone is at its maximum; hence, when illumination of any one of the photoresistors is reduced, circuit resistance increases and the signal amplitude reduces.
Because the photoresistor resistance does not change instantaneously when illumination changes, as illustrated in Fig. 3 by the gradual peak amplitude drop, and in the case where one photoresistor is illuminated differently than another producing a different amplitude result for each, a peak amplitude threshold must be determined.Above the threshold a gate is considered closed (exited) and below the threshold a gate is considered open (entered).To determine the threshold, the peak amplitude is initially calibrated by illuminating (closing) all gates and incrementally increasing the speaker volume until the microphone peak amplitude approaches its maximum, M.After fixing the volume for M, the microphone peak amplitude for the circuit when any single gate is open (entered), L, is determined by blocking (entering) each gate in turn and recording its lowest peak amplitude.L is set to the greatest peak amplitude of any blocked gate to ensure the threshold is crossed when any gate is blocked.The maximum M and minimum L then set the upper and lower output limits of the circuit peak amplitude.The threshold amplitude, TA, the peak amplitude level that determines when an object enters or exits a gate is: TA=(M+L)/2, the amplitude midpoint between M and L.An amplitude crossing the threshold from above is defined as gate entered, while crossing it from below is defined as gate exited.
With the threshold defined, deriving timing data from the microphone audio is both straightforward and accurate.From the general recipe, the practicalities of timing a moving object reduces to: 1. Choose a photoresistor to measure illumination.
2. Generate a sine wave of 4410 Hz and volume setting at M through the speaker and sample at the microphone at 44100 samples per second which corresponds to 10 samples per wave.
3. Detect the wave peaks by the method described in Fig. 4.
4. Convert wave peaks to corresponding timing data by noting the sample number at threshold crossings.
Microphone input sampling occurs at 44100 samples per second or one sample every 0.000023 s (equals 1 sample/44100 samples/second).Accurate timing between events is simply a matter of counting samples between threshold crossings.Gates are entered when a wave peak crosses the threshold from above and the sample number of the peak is recorded at this time.Similarly, gates are exited when a wave peak crosses the threshold from below.The time between entering two gates is then: where t=time between Gate 1 and 2, g 1 = Gate 1 entry sample number, and g 2 = Gate 2 entry sample number.Figure 3 illustrates the microphone input while timing the one meter free fall of an object when entering two photoresistor gates.The amplitude of the wave input at the microphone drops as the object entering the first photoresistor (PR1) at time 6.2041 s and again at time 6.6607 s when entering the second photoresistor (PR2).The measured time of 0.456 s = 20110 samples/44100 samples/second compares favorably with the predicted time of 0.451 s.
The prescribed recipe above was followed in the design of the GateTiming [3] timing application that measures times of entry and exit at multiple gates.Figure 5 presents the times recorded during multiple runs through two gates.The Date/UTC.mscolumn is the clock time on entering the first gate, the Time column is the time between entering the first and second gate; a more detailed display of each gate entry and exit times is a viewing option.Table 2 lists the times as recorded by the application of a block falling one meter.The variation of measurements is primarily due slight differences in the height when dropped above the first gate.For example, dropping from 1/2 inch (1.27 cm) above the first gate produced a timing in the range of 0.42 s, the time is lower than the one predicted because the block is traveling a little faster through the gates (about 0.5 mph or 0.80 km/h at the first gate).Other timing variations are likely due to slight changes to the block profile relative to the gates, not always maintaining a stable orientation.

III. Conclusions
Although only timing was closely examined in this paper, a range of audio port data collection applications are possible, including our own Android , a general purpose tool for signal display and analysis that can examine the behavior of a variable resistor in the circuit of Fig. 1 and 2, GateTiming [3], to measure the time an object passes between or through a series of gates, DCVoltmeter [5], measurement of 0-10VDC via voltage-to-frequency conversion of microphone input, then converting the frequency to a corresponding voltage value; a simple, active voltage controlled oscillator circuit is required for the conversion.Other applications for data collection from a variety of sources using the basic recipe are also possible.By using this method, data can be collected from almost any sensor based on variable resistance.

Figure 3 :
Figure 3: Microphone input signal amplitude versus time graph from circuit in Fig. 2 when measuring an object falling from 1 meter.The amplitude of the wave input at the microphone drops as the object entering the first photoresistor (PR1) at time 6.2041 s and again at time 6.6607 s when entering the second photoresistor (PR2).The measured time of 0.456 s compares favorably with the predicted time of 0.451 s.

Figure 4 :
Figure 4: Speaker output of a 4410 Hz sine wave sampled at 44100 samples per second at the microphone, producing the highlighted 10 samples per wave.Simple peak detection is possible by comparing three consecutive samples (x0, x1, x2) where: 0 < x0 < x1 > x2 > 0 the wave peak occurs at sample x1.

Table 2 :
Times of a block falling one meter.