Saturday, January 26, 2013

Metal detector part 2, the LM386

OK, I made some mistakes in the last post, so let's take a step back and revisit parts of it.
 Last time I wrote that we wanted a low impedance in the connection between the two oscillators. I also calculated the impedance in that node at 20Hz, since that's the mixed frequency we will listen to when we look for treasure.
 Wrong! The frequency we are mixing is around 400kHz, the lower frequency comes in the next node. So we need to make new impedance calculations to match the pull up/pull down network.
 But there is more. I did try to connect the two oscillators with large capacitors, but suddenly the two oscillators where oscillating together. What's happened? I'm not completely sure about the mechanism, but having a small impedance at the emitter will load the emitter significantly, compared to the 22k\(\Omega\) resistor.  Loading the emitter in the oscillator is bad, since it would remove the small charge injection into the oscillation tank. We need the opposite, just like the original EDN article, which uses 1.2pF capacitors. I found a couple of 2.2pF capacitors, which will do. This gives us
\[|Z|   = \frac{1}{2 \pi 400\text{kHz} 2.2\text{pF}} = 181\text{k}\Omega\]. Pull up/pull down resistors with a parallel resistance of 1.8M\(\Omega\) should suffice.
 Now comes the next problem. The audio amplifier we are building needs to have a high impedance input, but BJT's are usually hard to design as high very high impedance emitter followers. I asked an experienced colleague about the problem and he suggested that I look in the data sheet of the audio amplifier that EDN use, to see if it shows how the transistors in the IC connected. Great idea!
 Look at the data sheet page two. At the inputs (pin 2 and 3) you can see cascaded transistors. Connecting transistors this way is called a Darlington connection and it gives a very high input impedance, (\(h_{fe1}\cdot h_{fe2}\cdot R_e\)), and a very high gain. This is what we will try out as the first part of our audio amplifier. But now that we are at it, we might as well see if we can understand the rest of the LM386.
 The Darlington inputs are connected to a differential amplifier, the two mirrored npn's below the input stage. It amplifies the difference between the two inputs. The diff amplifiers output goes to a push-pull amplifier. This one has a transistor connected to ground (straight below the two diodes), that compensates for temperature induce gain change in the the rest of the amplifier. The lower amplifier stage has a Sziklai connected stage instead of a single transistor. It basically has the same function as the Darlington connection, it gives a higher input impedance and higher gain. If you want to know more about these circuits, I highly recommend reading chapter two of Horowitz & Hill(1989).
 By the way, does anyone know what the circle with the arrow, at the top of the diagram, means? My guess would be some kind of diode. In Horowitz & Hill(1989), page 93, there is a resistor in that position.  
This is it for now. I'll report back when I have tried the described circuit change and the Darlington emitter follower.

Horowitz, P., & Hill, W. (1989). The art of electronics. Cambridge university press.

Wednesday, January 16, 2013

Metal detector using Colpitt oscillators

This is a project I found through It's an old EDN article describing a very simple metal detector I thought I'd use treasure hunting with one of my daughters.

Oscillator circuits

 The basic principle is to use two oscillators. At least one of them should use an inductor as a part of the oscillator tank. The easiest design is a Colpitt's oscillator, since it only use one simple inductance together with a couple of capacitors. An LC circuit would oscillate spontaneously if it weren't for the resistance in the coil, capacitors, wires etc. To compensate for the resistance we use a transistor to give a small charge push in every oscillation. Here's an LTSpice simulation of such a circuit:

Colpitt oscillator

The output of the oscillator is taken from the emitter, and if you look at the simulated currents in the transistor, you will see some strange oscillations. These come from the base going above the collector for a short period of time. They will probably not be present in the real circuit due to internal resistance in the various elements of the circuit (this will of course be checked in reality..)

The oscillator frequencies depends on the inductance of the coil. By winding a big air coil, the frequency of the oscillator will change when the surrounding permeability changes according to this formula:
\[L = \mu k\]
where L is inductance, \(\mu\) is permeability and k is a constant depending on the physical shape of the coil.

Frequency mixing

Frequency is typically around 400kHz, so we need to convert it down to audio levels if we want to use a loudspeaker as indicator. This is done using a local oscillator with a fixed frequency. The two oscillator outputs are connected via capacitors, like in the schematic below. We also need to pull the out signal somewhere, not to leave output floating. The pull up/pull down resistors are chosen to have a total resistance of 75k\(\Omega\) (you will see why when we design the audio amplifier). We can see the capacitors as output from one stage and the resistors as inputs to the next stage. One wants a low output impedance connected to a high input impedance to avoid loading the oscillator. The impedance of the capacitors are less than 7.5k\(\Omega\) according to the formula \(|Z| = \frac{1}{2 \pi f C} \approx 4000 \Omega\) at our lowest required frequency 20Hz. The capacitors and resistors make a high pass RC-filter, so we also need to check that the filter doesn't filter out sound above 20Hz. The cut-off frequency is: \(f_c = \frac{1}{2 \pi R C} \approx 1 \)Hz.
Simplified mixer circuit

 So, what will the output of this setup be? Let the capacitors be complex impedance's:
\[z_1 = \frac{-j}{2 \pi f_1 C_1}\]
\[z_2 = \frac{-j}{2 \pi f_2 C_2}\]
 The current through the resistor is the sum of the current from the capacitors.
The voltage drop over R is
\[V_{out} = R \cdot I_R = R (I_{C1}+I_{C2})\]
The current from the capacitors will be:
\[I_{C1} = \frac{V_1}{z_1} = \frac{-j\cdot V_1}{2 \pi f_1 C_1}\]
\[I_{C2} = \frac{-j\cdot V_2}{2 \pi f_2 C_2}\]
\[V_{out} = R \left (\frac{-j\cdot V_1}{2 \pi f_1 C_1}+\frac{-j\cdot V_2}{2 \pi f_2 C_2}\right)\]
Since we are dealing with real voltages, the output voltage will be
 \[|V_{out}| = R \left (\frac{\sin(2 \pi f_1 t)}{2 \pi f_1 C_1}+\frac{\sin (2 \pi f_2 t)}{2 \pi f_2 C_2}\right )\]
When adding two sine waves, we can use this identity:
\[\sin(\alpha)+\sin(\beta) = 2 \sin\left (\frac{\alpha + \beta}{2}\right ) \cos\left (\frac{\alpha - \beta}{2}\right )\]
 Without actually calculating the amplitude of the resulting wave ( which we are less interested in) we can see that the resulting wave will have both the added and the subtracted  frequencies of the two original waves. It will look for example like this,
20 beats per second from a 400.00kHz and a 400.02KHz sine wave.
 By using a local oscillator with a frequency very close to the variable oscillator, we can generate a frequency component in the audible range that is proportional to the permeability close to the search coil.
 The first step is to build the oscillators  and trim them so they are close enough to each other. The second step will be building an audio amplifier to amplify the mixed signal. In the EDN article, they use a dedicated audio amplifier chip. I will build the amplifier from transistors, since that is one of the purposes of this project, to learn about transistor (BJT) electronics.

Sunday, January 13, 2013

Boost regulator efficiency meter

This is a project I've been working on from time to time for a while now, but now it's time to wrap it up and document it.  The whole thing started with me building a couple of boost regulators for charging the cell phone (inspired by Minty Boost). I used one regulator from LT and one from TI, and I wanted to compare their energy efficiency.
Boost regulator from LT (LT1302)
Boost regulator from TI (LM2700MT-ADJ)
 To do this one needs to measure power on both sides of the regulator and present the fraction of the output to the input. This was done using a PIC 16F690, which has plenty of IO's and a 10 bit ADC with several channels. Measurements can be done using four ADC channels and the result can be presented using LED's on the rest of the IO's.
Voltage measurements where done by connecting the 3 volt regulator side directly to an ADC channel. Since the PIC is thought to be running from 4.5V battery power, this will be the absolute upper limit of what the ADC can measure. The output of the regulator is 5V, so that voltage was connected to the ADC through a voltage divider.
 Current is measured by measuring the voltage drop over a milliohm resistor in series with the load. We don't want the resistor to load the circuit to much, but a small resistor mean a small voltage drop, so we will use an operational amplifier as a differential amplifier to amplify the drop before it is measured with the ADC.
 I generally try to use scrapped or salvaged parts and I had two milliohm resistors lying around at work, one 0.1 Ohms and one 0.02 Ohms, so I used those together with a couple of rail-to-rail op-amps (Microchips
MCP6271) that I bought. I started out experimenting with some old LM741's, but they needed a few volts both over and above their maximum output, which did not fit well with my ambition to power the circuit with three batteries.
Lab setup. 
Finished board. 
 The gain of the differential amplifier was chosen so that a current of 2A would give an output signal of around 4 volts. The schematic of the circuit can be found here. The PCB was made using double sided toner transfer, which didn't work all that good. Also, I mixed up the power terminals of the op-amps (fixed in the schematic above), so there were quite a few straps made.
 The PIC source code can be found here. The code does the job as long as one is only interested in comparing regulators. The code is only calibrated against one ADC channel at a time and test measurements showed an efficiency of under 50% for both of the regulators, which seems a bit low. It would probably be better to use two known power inputs and calibrate efficiency against those.
 Calibration was done using a known current source. By knowing the current ,\(I_{in}\), and reading the ADC binary output \(I_{bin}\), we can get the conversion factor (k) if we have the linear relationship \(I_{in}=k \cdot I_{bin}\). By making a few measurements of this kind we can use the very smart website to get a linear fit with the measured data. Here's one of the calculations (actually for a voltage input, but the principle is exactly the same) using the command: linear fit {{1023,5.44},{511,2.7},{255,1.33},{127, 0.65}}. In the end, what we will calculate in matter of ADC outputs is \(E=(k_{out-I} \cdot I_{out-bin} \cdot k_{out-V} \cdot V_{out-bin})/(k_{in-I} \cdot I_{out-bin} \cdot k_{in-V} \cdot  V_{out-bin)}\), and hopefully we can group all k's into a nice, single, integer (0.6 in our case, not so nice...). The observant reader will notice that there is also an offset term missing in \(I_{in}\), which is actually there in the Wolfram Alpha output. I haven't looked into this yet, but for now I will ignore this since I actually don't a whole lot of accuracy and the offset is pretty small. A complete error analysis of the measurement system would be interesting but time consuming, maybe I'll come back to that on a rainy day. (By the way, I used MathJax to display mathematical equations...)
 Sample rate of the ADC's are adjusted to what looks stable on the output LED's, which is quite a lot slower than what could be achieved. An algorithm for more samples than LED updates would probably be good. Let's see the present code as work in progress, shall we?
 This is a topic that can be explored deeply, both the measurements aspect and the regulator aspect. Right now, however, I'm eager to get my hands on a couple of other projects, so the efficiency meter will be put on the shelf for a while. I'm sure I'll come back to it in the future though...