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. 

Friday, February 6, 2015

Bicycle speedometer using hub dynamo to supply and as a speed signal

Idea
So I got one of this cool hub dynamos. I really like that it's quiet and efficient way of generating energy for bicycle lamps. I considered adding USB charger to my bicycle to be able to supply my mobile phone while cycling but this is not trivial topic because of low output frequencies from dynamo. Another problem is relatively low output voltage, 6V is to low to use LDO reliable, under low speed condition output voltage may be lower than 5V so eventually a step-up step-down converter would be useful. Additionally rectifying low voltages is kind of tricky, I mean you can do this easily but to do this efficiently is already a challenge because of immanent power loses in diodes (0.7V drop at 6 V input is already more than 10% loss).
As a byproduct of such consideration I drawn a simple circuit which uses LCD display and tiny micro to track frequency, which is directly proportional to the bicycle speed. I like the idea of using dynamo output voltage as a supply and a signal at the same time. You will never need to replace battery, and you don't need additional sensor on the wheel.

Circuit description


The speedometer is supplied from a hub dynamo. D1, C7 and C8 form a half-wave rectifier. C4 is ESD protection and D2 is a Zener diode which should be short-term overvoltage protection. IC1 is an LDO voltage regulator, carfuly chosen to have low quiescent current, C5 is output capacitor of the voltage regulator. Hub dynamo produces pseud-sinusoid signal with a frequency which rises with speed. At low speeds at big wheels frequency can go as low as 5Hz, and C7 and C8 have to store energy for the device.

Input waveform when supplied from signal generator.

Signal at SPEED node. Voltage limited to the value safe for microcontroler digital input. Not very "digital" but thanks to microcontrolers Schmitt input it's not a problem. Circuit supplied from signal generator.
Waveform shows signal at SPEED node. Circuit supplied from a real hub dynamo (Shimano DH-3N31-NT).

Measurement
Because of required low power consumption micro has to run with 32[kHz] clock. The micro is actually measuring period, not frequency so division is needed. Fixed point arithmetic is used with assumptions that the speedometer shows integer part of the speed with proper rounding up and precision not worse than 0.1[km/h].
to be continued
(verification of fixed point arithmetic)

Prototype
First prototype supplied from signal generator

First prototype supplied from a real hub dynamo driven by a DC motor
Software
Currently there is a github repository where you can download software and compiled.hex file. However this software doesn't show speed yet. It shows period in milliseconds before rising edges on SPEED signal. Stay tuned, new revisions are coming.

PCB
I designed a simple one-sided PCB using Cadsoft Eagle. Designing PCB with one layer only (the red top layer are wire connections which will be soldered manually) required changing association between mirocontroler pins and LCD segments. This is "layout friendly" now, software will have to compensate for this. Eagle files and gerber production files are packed along with software and can be obtained from github repository.





Todos
Add buttons
Add possibility to set up wheel circumference
Add captive touch sensor instead of mechanic switches
Add LED to back light the display while driving
Print a housing
Find a neat way of connecting the speedometer to the bicycle (electric and mechanical solution)

Housing trials
I've drawn a simplest housing ever, it consists of one part, PCB will be put in and sealed with epoxy. There is rounded part to fix the housing on the bicycle handlebar and fixing area to secure it with cable binders. Just click on "play" button to see it.

I didn't realize that this shape is actually not printable, the result can be seen here:


My buddy convinced me that using cable binders is not perfect way of holding stuff on the bicycle. Second housing will be fixed with an o-ring. I was thinking how I could make o-ring less visible while keeping model printable, but I changed strategy, if I can't make something unvisible, I will intentionally make it visible.



This is freshly printed housing with an LCD put in to if dimensions are right