Encoder Front Page
SRS Home | Front Page | Monthly Issue | Index
Search WWW Search seattlerobotics.org

A Microphone Frequency Sensor

Eli Kolberg


One of the reasons for having a mic sensor that is able to tell what frequency is playing around, is Trinity College Fire Fighting contest bonus. (Well, a small one but it's something). Other reasons maybe relatively low cost frequency catcher, like a hand clamp or a bark or a whistle etc., a tuning device, tone code key, software filter, etc.

Fig. 1 is a schematic of such a circuit. It is based of three chips: LM386 low voltage audio power amplifier, LM2917 Frequency to Voltage converter, and TC7660 DC-TO-DC voltage converter.

eli_mic_encoder_fig1.gif (10835 bytes)

Fig. 1: Frequency to voltage converter circuit

First stage is the amplification one which takes the mic output and amplify it by a gain of several tens or so. Then comes the Frequency to voltage converter stage using the LM2917 (Click here for datasheet from National). This chip is divided to a tachometer section and an op amp section. I have used the chip here with a ground-referenced tachometer input and an internal connection between the tachometer output and the op amp non-inverting input.

Following data sheet recommendation for the resistors and capacitors, with small changes and disconnecting the output transistor collector from the op-amp inverting input, the chip's output is a linear function where for 1KHz its output is 1 Volt and for input of 2KHz is output is 2 Volts, etc. That is illustrated in Fig. 2. Note that it can not handle more then 8KHz and will output a constant voltage of 8 Volts for a frequency of 8 KHz or more.

The LM2917 need 10V for a proper operation in this mode. As I do believe that a circuit should be as simple to the user as possible, I found the TC7660 (Click here for datasheet from Microchip) dc-to-dc converter very handy. It gets 5 volts in pin 8 and following connection to pin 2 through a capacitor, and two diodes, it gives (2*5V - 2*0.7V) = 8.3V at the cathode of D2. This is less then 10V but it works well for the LM2917.

eli_mic_encoder_fig2.gif (8556 bytes)

Fig. 2: LM2917 Frequency to voltage conversion

The complete assembly of the board can be seen in Fig. 3.

eli_encoder_mic_fig3.jpg (35175 bytes)

Fig. 3: Complete assembly of the circuit from fig. 1.

Now, if you don't have analog to digital converter built in your microcontroller, you can add a simple comparator circuit, as shown in fig. 4, and calibrate it to desired frequency, so when it reaches desired frequency it will output logic '1'. Below that frequency it will output logic '0'. Input is fed from fig 1. Output and the new output is a digital one. As can be seen, I've used lm339 for the comparator. Any other one will fit.

eli_mic_encoder_fig4.gif (3167 bytes)

Fig. 4: Digital output of the circuit

Source Code

An example of 68hc12b32 assembly language is introduced in listing 1. I assume here that:

again:		movb #$50,atdctl5
wait:		brclr atdstat $80 wait
		ldaa adr7h
		cmpa #$c0
		bpl wakeup
		jmp again
wakeup:	…..			;when frquency has reached the
				;desired level, the program jumps
				;here and continue its operation.

Listing 1: 68hc12b32 assembly language for analog mic input frequency detection:

Explanation to listing 1: first row we put $50 (%01010000) into atdctl5. That means that we choose 8 conversions, single sequence, and multiple channels. All that is just to say that the processor will take one sample of each channel (bit, line) in port ad, convert it to a digital number, store it in the appropriate address (adrxh) and stop.

As we are interesed in channel 7 (mic out line is connected to), we know that after conversion is completed, we will take a look at the number residing in address adr7h.

How we know that conversion is complete? Well, there is a bit they call SCF (abbreviation for Sequence Complete Flag). When this bit becomes '1', that is the sign for completing all conversions and all resulted digital numbers are resting peacefully in their appropriate addresses.

That leads us to the second row. The instruction brclr (branch if clear), checks the address following the word 'brclr' for a bit according to mask of eight bits following the address. In our case the mask is $80 or %10000000. So it looks at the address atdstat in its leftmost bit (exact location of '1' in mask number). This bit is happens to be the SCF bit (it is the leftmost bit in the number residing at the atdstat address). If the bit value is zero, the result is true and the execution goes to the place in the program which has the same lable as the one following mask. In our case this is 'wait' lable. We can see that this exact lable is marking the beginning of the very same line.

So, the result of all this is that as long as SCF bit is cleared, the program is chasing its tale. At the moment that this bit becomes '1', the condition (clear) is no longer valid and the execuction of the program continues from the following line. Now, we are sure that the conversion result is valid and can be read from the appropriate address which is adr7h (remember that we connected our mic output to port ad channel 7).

In next step, we are uploading the number from adr7h into accumulator A. That is done to enable a comparison or a subtraction action. Indeed next line is a comparison between the number in accumulator A (which is the result of converting mic out from channel 7 depicted frequency) and a number $c0 which corresponds to our desired frequency.
Actually a substraction operation is made and ccr flags are changed accordingly. Now the processor knows if the result is positive (mic depicted frequency is higher than the desired one), zero (they are the same) or negative (mic depicted frequency is lower than the desired one).

Just a side note, compare instruction does not change accumulator value. That is not the case with subtraction, where the contest of accumulator A (or B) is changed.

Now, if the result is positive, then the program goes to wakeup and continues execution. Otherwise it goes again to check the frequency at the mic and that cycle repeats until desired frequency or above is depicted by the mic.

That can be used as a starter for a robot participating in Trinity College Fire Fighting Robots contest.
Of course if you choose to take the digital version of the circuit (figs 1 plus 4), the code is more simple and you only compare a value of an input digital line to $c0 for example.

Part list for Microphone circuit 

Item Value Qty
R1 2.2 K 1
R2 100 K 1
R3 10 K 1
R9 300 R 1
C1 0.001 m F 1
C2,C7,C8, C9 100 m F 4
C3 10 m F 1
C4 1800 pF 1
C5 0.47 m F 1
C10 0.1 m F 1
D1, D2 1N4002 2
X1 Condenser mic. 1
@U1, U2, U3 8 pin DIP 3
3 pin block terminal   1
U2 LM2917 1
U1 LM386 1
U5 TC7660 1