Papers in Physics, vol. 10, art. 100006 (2018) Received: 5 January 2018, Accepted: 20 April 2018 Edited by: A. Mart́ı, M. Monteiro Reviewed by: P. Jeanjacquot, École Normale Supérieure de Lyon – Institut Français de l’Éducation, France Licence: Creative Commons Attribution 4.0 DOI: http://dx.doi.org/10.4279/PIP.100006 www.papersinphysics.org ISSN 1852-4249 Smartphone audio port data collection cookbook Kyle Forinash,1∗ Raymond Wisman1† 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 smart- phone is a 4-pole jack connecting to some exter- nal 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 collec- tion methods are quite similar. In each circuit, a variable resistor connects the speaker audio out- ∗E-mail: kforinas@ius.edu †E-mail: rwisman@ius.edu 1 Indiana University Southeast, 4201 Grantline Road, New Albany, Indiana 47150, USA. Table 1: Audio connections to 4-pole jack. Tip Left speaker 1 Right speaker 2 Ground 3 Microphone Figure 1: Temperature measurement circuit. 100006-1 Papers in Physics, vol. 10, art. 100006 (2018) / K. Forinash et al. Figure 2: Time measurement circuit. put to the microphone input; the speaker output is a wave which peak amplitude at the microphone input varies with the variable resistor, greater re- sistance resulting in lower peak amplitude. Fig- ure 3 illustrates the effect of a photoresistor on the microphone signal amplitude while a moving ob- ject momentarily blocks and reduces the illumina- tion 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 illus- trated in Fig. 4. 3. Detect the significant elements of the micro- phone input. Significant for timing applica- tions, wave peaks can be detected by the sim- ple method described in Fig. 4. 4. Convert the significant elements to corre- sponding data. For example, by recording a 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 favor- ably with the predicted time of 0.451 s. Figure 4: Speaker output of a 4410 Hz sine wave sam- pled at 44100 samples per second at the microphone, producing the highlighted 10 samples per wave. Simple peak detection is possible by comparing three consecu- tive samples (x0, x1, x2) where: 0 < x0 < x1 > x2 > 0 the wave peak occurs at sample x1. 100006-2 Papers in Physics, vol. 10, art. 100006 (2018) / K. Forinash et al. range of peak amplitudes produced by the cir- cuit of Fig. 1 and the corresponding temper- atures, 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. For 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 in- creases 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 ampli- tude drop, and in the case where one photoresistor is illuminated differently than another producing a different amplitude result for each, a peak am- plitude threshold must be determined. Above the threshold a gate is considered closed (exited) and below the threshold a gate is considered open (en- tered). To determine the threshold, the peak ampli- tude is initially calibrated by illuminating (closing) all gates and incrementally increasing the speaker volume until the microphone peak amplitude ap- proaches its maximum, M. After fixing the volume for M, the microphone peak amplitude for the cir- cuit 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 ob- ject enters or exits a gate is: TA=(M+L)/2, the amplitude midpoint between M and L. An ampli- tude 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 practi- calities of timing a moving object reduces to: 1. Choose a photoresistor to measure illumina- tion. 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 de- scribed 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 sam- ples per second or one sample every 0.000023 s (equals 1 sample/44100 samples/second). Accurate timing between events is simply a matter of count- ing 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 be- low. The time between entering two gates is then: t = g2 − g1 44100 samples second , (1) where t=time between Gate 1 and 2, g1 = Gate 1 entry sample number, and g2 = 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 ob- ject entering the first photoresistor (PR1) at time 6.2041 s and again at time 6.6607 s when enter- ing the second photoresistor (PR2). The mea- sured time of 0.456 s = 20110 samples/44100 sam- ples/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 multi- ple gates. Figure 5 presents the times recorded 100006-3 Papers in Physics, vol. 10, art. 100006 (2018) / K. Forinash et al. Figure 5: Sample timing results from GateTiming ap- plication. during multiple runs through two gates. The Date/UTC.ms column 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 view- ing option. Table 2 lists the times as recorded by the application of a block falling one meter. The variation of measurements is primarily due to 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 lit- tle 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 rel- ative 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 ap- plications are possible, including our own Android Table 2: Times of a block falling one meter. Time [s] 0.453 0.457 0.471 0.451 0.448 Mean 0.456 applications: AudioTime+ [4], a general purpose tool for signal display and analysis that can exam- ine the behavior of a variable resistor in the cir- cuit of Fig. 1 and 2, GateTiming [3], to measure the time an object passes between or through a se- ries of gates, DCVoltmeter [5], measurement of 0- 10VDC via voltage-to-frequency conversion of mi- crophone input, then converting the frequency to a corresponding voltage value; a simple, active volt- age 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. [1] K Forinash, R Wisman, Smartphones - Exper- iments with an External Thermistor Circuit, The Physics Teacher 50, 566 (2012). [2] K Forinash, R Wisman, Photogate Timing with a Smartphone, The Physics Teacher 53, 234 (2015). [3] R Wisman, K Forinash, Mobile Science - GateTiming, December 2017. Google Play: https://play.google.com/store/apps/details? id=edu.ius.rwisman.gatetiming [4] R Wisman, K Forinash, Mobile Science - AudioTime+, November 2013. Google Play: https://play.google.com/store/apps/details? id=edu.ius.audiotimeplus [5] R Wisman, K Forinash, Mobile Science - DCVoltmeter, January 2016. Google Play: https://play.google.com/store/apps/details? id=edu.ius.dcvoltmeter 100006-4