Wednesday 28 December 2016

Poor Man's Digital PGA - Part 2 of 2

No testing is complete until you try the final configuration. In the earlier post, I had simply grounded the extra resistor R7 to switch in the higher gain. Figure 1 below shows a capture of the signal at its base gain of 2.

In this capture, all extra AVR resistors R4, R6 and R7 are in the tri-state condition. It can be seen in Figure 1 that some noise exists on the output. Counting the bumps, there appears to be 20 per cycle amounting to about 100 kHz. The AVR device here is operating at about 1 MHz, using its built-in RC oscillator.

Figure 1. Noise on PGA Output
For use with the MSGEQ7, this should present no difficulty since the switched capacitance filters will eliminate this. This may present a difficulty when used in other audio work, however.

The good news is that this noise is reduced as the AVR resistors get switched in for higher gains.

You can watch an AVR switch between four gains here:

  https://youtu.be/iqPBaHtLFI0

Thanks for reading!


Monday 26 December 2016

Poor Man's Digital PGA

Playing around with the MSGEQ7 chips it was readily apparent that the audio input level was critical to its graphic equalizer display. There have been many analog AGC (Automatic Gain Control) designs used over the years. Some designs using an opamp and a FET can be constructed but can be difficult to get right for students without an oscilloscope.

With an eye to reproducible circuits for readers, I was looking for a single-ended opamp design that was digitally controlled. Digital circuits tend to be easier to manage with the minimum of equipment. So I got to wondering if I could manage this with the use of the LM358 (or the LV rail-to-rail part MC33202).

I wondered if a Digital PGA (Programmable Gain Amplifier) could be constructed with a minimum of parts. Figure 1 shows a typical AC coupled non-inverting amplifier design. Because it is single ended, it needs the voltage divider consisting of R1 and R2. If VCC is 5 V, then the junction between them will be VCC/2 or 2.5 V.  C1 AC couples the signal into the non-inverting input of IC1. The feedback circuit is however a bit unusual because the gain for DC and AC signals differ.
Figure 1. AC Coupled Non-Inverting Amplifier
Those who know/remember their opamp theory, know that a non-inverting amplifier's gain is set by the ratio:

Av = (R5/R3) + 1

But in this circuit the capacitor C2 blocks DC current. The feedback current through R5 is so small, it behaves as if the output was wired directly to the inverting input. The net effect is that the Vout = V- = V+ for DC. The resistor R3 has no DC path to ground and as a result, can have no DC effect on the output.

Yet, the AC gain is however, still affected by R3. So the AC gain of the Figure 1 circuit is indeed:

Av = (500k/500k) + 1 = 2

It is well known that either R5 or R3 has to change to affect a change in this gain calculation. The good thing about this circuit is that the DC gain is not affected by resistance changes as long as R5 remains reasonable.

To change the AC gain, we could change out R5 using some FET switches but this proves inconvenient. Likewise R3 is inconvenient for switching resistors in and out. But if you stare at Figure 1 long enough, you have to start asking yourself "wouldn't it be nice if we could move R3 to ground?" Then you could easily add or subtract resistances to change gain. It turns out that we can!

Figure 2 shows the circuit modified so that R3 has one grounded leg and the capacitor C2 is moved above it. In this case R3 and C2 are simply in series and can be moved around.

Figure 2. AVR Controlled PGA
With R3 grounded, we can easily add resistances in parallel with it. R4 if placed in parallel with R3, is equivalent to:

R3 || R4 ~= 83k

This change alters the gain:

Av = (500k/83k)+1 ~= 7

So with the simple act of adding in R4 in parallel with R3, we've digitally changed the AC gain from 2 to 7. R4, R6 and R7 were chosen to be 10% tolerance resistances giving the following selection of gains:


ValueR3GaindBV
0500,0002.06.0
183,3337.016.9
242,96212.622.0
330,05117.624.9
421,07324.727.9
517,40529.729.5
614,54935.431.0
712,70140.432.1

With all resistors enabled by the AVR device, the maximum AC gain is about 32 dBV (40). With none of the added resistors connected in, the gain reduces to a minimum of 2 (6 dBV).

AVR Digital Control

The digital controls coming from the ATmega328P (Figure 2) must set the outputs of PD5, PB6 and PB7 along these lines:
  1. To enable a resistor, the port must be configured as an output port and have the level set to zero (low).
  2. To disable a resistor, the port must be configured as an input port and have the pull-up resistor disabled (thus achieving a tri-state condition).
Note that a tri-stated AVR input here is never left floating. It will assume the potential of the high side of R3, which will be ground effectively. R3 never switches out of the circuit.

The 'mega ports in Figure 2 were chosen to avoid some otherwise usable ports. You can of course change the ports to meet your own needs.

Function Generator Test

Figure 3 shows the scope traces of the 5 kHz function generator (channel 2) and the IC1A output in channel 1. Here it can be seen that the gain is 2.

Figure 3. R4, R6 and R7 disconnected (off), gain of 2

Figure 4 shows R4 grounded to enable it in parallel with R3. The gain is nearly 7 (422mV/64.3mV=6.56).

Figure 4. R4 || R3, gain near 7
Watch the gain change between four settings, driven by an ATmega328P:

  https://youtu.be/iqPBaHtLFI0

Thanks for reading!

Monday 19 December 2016

MSGEQ7 7 Band Graphic Equalizer Chip



I had bought a couple of MSGEQ7 chips in 2014 from eBay with plans to do something with them. So recently I found myself a bit of idle time and mounted one on the breadboard to try it out. I used the circuit straight out of the MSI datasheet (Figure 1):
Figure 1. MSGEQ7 Circuit
I had a little trouble getting this to work correctly, so I scoped pin 8, knowing this is not going to be accurate but should indicate if the RC oscillator was running at all or not (Figure 2). The trace indicated about 102 kHz, which is probably higher with the 10x scope probe removed. 

Figure 2. Trace of pin 8 (Oscillator)
The problem was that I was getting an output (pin 3), like that of Figure 3. This was with no signal applied.


Figure 3. Incorrect outputs (pin 3)
After trying several things including gain and frequency adjustments of the function generator, I decided to try the other chip (I always buy two for this reason!) This chip swap yielded the type of results I was looking for (Figure 4):

Figure 4. MSGEQ7 finally works (near 60 Hz)
The tallest signal represents the first frequency bucket which represents about 63 Hz (from the documentation). This will vary somewhat depending upon the internal RC oscillator.  I used a AVR generated oscillator to strobe this thing and used the scope's sync to drive the reset. You can see two complete traces in the middle with partials at the ends. Unfortunately the Rigol internal hardware frequency counter didn't like the low signal levels. The software frequency measurement likewise didn't grok the low function generator signal levels either.

Figure 5 shows the result of the function generator at 160 Hz. I found that the signal generator (B&K Precision Model 3050) needed to be on the 0 dB output range at about 10%. The chip was fussy about the input level going from no output to all high when the signal went out of range.

Figure 5. 160 Hz
 The next bucket is 400 Hz, shown in figure 6.
Figure 6. 400 Hz
 Figure 7 shows the 1 kHz bucket activated.
Figure 7. 1 kHz
 Setting the function generator to 2.5 kHz for the next bucket (Figure 8).
Figure 8. 2.5 kHz
 Figure 9 shows 6.25 kHz.
Figure 9. 6.25 kHz
 Finally figure 10 shows the last of the seven buckets activated with 16 kHz.
Figure 10. 16 kHz
The idea behind the chip seemed cool. I had planned to drive it from an ATmega328P, hooked up to a Raspberry Pi. But having tested it out I quickly realized that this circuit is much too finicky for a "simple project". What it needs is a circuit ahead of the MSGEQ7 with AGC to keep the signal within acceptable limits. Otherwise the MSGEQ7 will simply disappoint.

The MSGEQ7 is a CMOS chip. I was aware of this and took my usual care handling it. But the first chip may have been static damaged on the breadboard. If you choose to breadboard it, I recommend that you make all the connections without the chip and plug it into the breadboard last. This is the first damaged CMOS chip I've had in quite a long time (it might even have been damaged as part of the shipping process).

You can read more about the MSGEQ7 chip from this PDF file:

  https://www.sparkfun.com/datasheets/Components/General/MSGEQ7.pdf

Thanks for reading.

Tuesday 6 December 2016

No iMac? Build a iRasp3

I've wanted to try this for a long time with the Raspberry Pi 3. The Pi 3 has built-in WiFi, making it a natural for an iMac-like terminal unit. I've held off because of work on my new book, which is will be out early next year (shameless plug here: http://www.apress.com/us/book/9781484224052).

Now that the Pi 3 is free, I just had to repurpose an old Viewsonic VG2230wm monitor with a Pi on the back (Figure 1).

Figure 1. iRasp front view.
I received the monitor some time ago from a friend without a stand. I eventually junked another monitor which had a stand that could be adapted to the Viewsonic. You can see it crudely attached in Figure 2.

Figure 2. Note the adapted stand for the monitor. Raspberry Pi 3 is mounted on the back with plastic spacers, using wood screws in the plastic casing.

The Pi is mounted on top of four plastic spacers, made from a pen cut with a small pipe cutter. The screw holes were carefully drilled over an area known to have some breathing space underneath. The monitor internals are covered in a metal shield, so I wasn't too worried about hurting anything (I had also been inside before).

The Pi 3 also has four USB ports. So even though I need two for keyboard and mouse, I still have two available. If needed, there is also a wired ethernet port.

I made sure to orient the GPIO header strip towards the bottom. This will allow me to use a longer ribbon cable down to the Cobbler-T for breadboard work.

Someday I hope to replace keyboard and mouse with wireless peripherals, since the Pi 3 also sports Bluetooth. In the meantime, I'll be looking for a shorter HDMI and audio cable.

Thanks for reading.

Monday 5 December 2016

ESR Project Build - Part 6 of 6


This build is not done until the unit is assembled in its cabinet. I thought this last step would be a slam dunk. But I was terribly mistaken!

The W2AEW design called for a 5 volt regulator. Since the parts used didn't require 5V specifically, I tested it using a 6.5V switching supply that I had handy. After all the wiring was done, I merrily built this 7805 circuit on a tiny proto PCB assuming all would be well. Of course, once it was all nicely soldered into place the unit didn't function!  I could not get the meter to deflect with the leads shorted.  Wah, wah, wha... epic fail!

I soon discovered that if I raised the voltage, things returned to normal. So I tested it on a variable supply and found that it returned to normal over 6V. Being too lazy to rework the PCB much, I simply put a pair of diodes in the ground circuit to raise the output to 6.2V (Figure 1). If I was less lazy, I would have run a filter cap over the diodes also, but found that I could get away without.
Figure 1. 7805 regulator circuit used (for 6.2V)
My BNC test connector cannot be grounded, so I found a piece of saved orange crate plywood, painted it black and drilled a hole for it. I eventually got the unit pretty much done without the external case but ran into another problem (more about this later).

Figure 2 shows the internals of the unit -- large enough for an electrical outlet for the wall wart which delivers about 10 VDC without a load. 10V incidentally, is too much voltage for low ESR readings. So a regulator was needed.

Figure 2. Internals of the ESR unit. The PCB hanging down has the 7805 regulator.
The problem was the Fine/Coarse switch that I added to the front panel. When that switch was on Fine, a 47 ohm resistor was placed in parallel with the 10 ohm, to reduce the resistance ever so slightly from 10 ohms to 10||47=8.25 ohms. This provides wider spaced low resistance readings on the meter.

The problem turned out to be that the old switch I had used had an on resistance of several ohms! This drove me batty until I finally took the meter and read the switch resistance. So even with the 47 ohm resistor switched in, it ended up having no effect. After rummaging through the junk box again and measuring the on resistance this time, the issue was resolved with a better switch.

Figure 3 shows the unit re-assembled in the Sencore transistor tester case.

Figure 3. Assembled ESR meter. Zero pot at left, BNC for test leads in middle, power switch at upper right and Coarse/Fine switch below the power switch. BNCs at bottom are NC.

I suppose the lessons of the day includes:

  • Don't assume the last part of the build will be trivial
  • Don't assume switches will all measure 0 ohms, especially old ones!
  • Test with the intended supply voltage up front.
Thanks for reading.