Monday, February 16, 2015

NTC temperature sensor connected over microphone input

Simple measurement
The idea is to measure resistance by connecting it to smartphone, to the microphone port to be precise. The simplest implementation would be something like this:
R1 and R2 form a voltage divider which dumps a signal from headphone output while microphone input records and measures amplitude. If one of resistors is known then second value can be found by comparing known headphone output amplitude with that recorded on a microphone input. C1 and C2 are capacitors which are always at the output and the input of a sound card. C1 is cutting off DC component so, without C1 there would be constant current flow through a headphones, even when playing back silence. The unnamed resistor to VCC is providing DC supply current for a microphone, C2 is not letting this DC current flow into ADC converter.
However
This circuit has got a huge disadvantage: it requires calibration with every smartphone you would like to connect it to. Additionally, I am not sure if headphone output doesn't change it's amplitude with temperature. Calibration seems to be not elegant solution so I though of using both headphone outputs and conducting a ratiometric measurement. Assuming that both headphone channels (left and right) provide the same amplitude if driven with the same signal, the influence of absolute amplitude could be eliminated from the equation.

Ratiometric measurement
||here will be a circuit diagram||
In this circuit there are two voltage dividers, R1 and R2 is dumping a signal from a left channel while R3 and R4 from a right channel. Microphone input measures sum of both divided voltages. Measurement is conducted by driving the left channel with a sinus signal, recording input amplitude, turning left channel off while the right on, recording input amplitude and doing calculations on recorded amplitudes.
Three of four resistors are known, the fourth is measured. To simplify calculations it is assumed that R2 and R4 resistors are the same while R1 and R3 are reference resistance and measured resistance.
to be continued...

.
Stimulus signal

For some measurement I needed a specially prepared .wav file with sinus wave interleaving between right and left channel. Octave manage to prepare that file but can't manage playback under windows 7 so I will have to use Audacity...

bps = 16;           % bits per sample
sps = 48000;         % sample rate [samples/s]
freq = 1000;         % frequency of the tone [Hz]
nsecs = 2;           % number of seconds of the audio file
interleaving = 1;      % duration of "pack" on a channel [s] 

nsamples = sps*nsecs;
interleavingSamplesNum = interleaving * sps;

time = linspace(0, nsecs, nsamples);
wave = sin(time*2*pi*freq)';

window1 = repmat([ones(1,interleavingSamplesNum), zeros(1,interleavingSamplesNum)],1,(nsamples/(2*interleavingSamplesNum)));
window1 = window1';

window2 = repmat([zeros(1,interleavingSamplesNum), ones(1,interleavingSamplesNum)],1,(nsamples/(2*interleavingSamplesNum)));
window2 = window2';

wavwrite([wave.*window1, wave.*window2], sps, bps, 'stimulus.wav')

temperatures=[];

for i=1:10
%playback prepared file and record microphone input simultaneously
i
system("play.exe stimulus.wav -t waveaudio", [], "async");
recordCommand=sprintf("SoundRecorder.exe /FILE recorded%d.wav /DURATION 0000:00:02",i);
system(recordCommand);
recordedFilename=sprintf("recorded%d.wav",i);
%recordedFilename=sprintf("recorded.wav");
y=wavread(recordedFilename);
deleteComand=sprintf("del recorded%d.wav", i);
system(deleteComand);
a=(sum(y(20000:30000).^2)/(30000-20000))^(1/2);
b=(sum(y(60000:70000).^2)/(70000-60000))^(1/2);
ratioResults=a/b;
resistanceNTC=ratioResults*10000;
temperature=ntc(resistanceNTC)-273;
temperatures=[temperatures, temperature];
%ratioResults=[ratioResults a/b]
endfor

I'd like to measure resistance value in circuit:
if three of four resistors are know, and L-OUT and R-OUT can be driven with prepared waveform while MIC-IN records amplitude, than the fourth resistor value can be calculated. I prepared a trivial protype to check if such measurement is really possible on my PC. Octave generates a wave file, Audacity plays it back and records microphone input, Octave analyses collected data...
Of course, there is many limitations like AC coupling of mic input which forms actually a high pass filter, limited amplitude of microphone input signal, asymmetry between channels, clever estimating parameters of recorded sinus wave... but, let's give it a try. This is my prototype, four resistor can be put into DIL socket to ease changing values. One jack connector is for microphone input, another for headphones stereo output.
The measurement 1
I set up the prototype with following values
R2=R4=1k
R1=10k
R3=22k
Connected my oscilloscope to find out that the cheap Chinese USB audio card:


 is actually providing around 2.23V DC into headphones output. This fact is not a problem for my measurement but can damage headphones. Anyway, here comes a waveform recorded by microphone input. The envelope of "packs" of sinus is surprising. I will do some further measurements to understand if DAC is not giving constant output or if microphone input is so bad quality. Probably these are some transitions because of microphone input capacitor. Next step will be to analyse recorded values to see if measured amplitude corresponds to expected amplitude...
Signal recorded by microphone input when stimulus signal has been played back.

Temperature logged in my room overnight. Celsius degrees, x axis are samples, not seconds. 

No comments:

Post a Comment