# Gas station without pumps

## 2016 February 20

### On not using kits for a design class

Filed under: freshman design seminar — gasstationwithoutpumps @ 10:07
Tags: , ,

A question came up in e-mail from a student in my freshman design seminar that I had planned to discuss in class, but I ran out of time before getting to it:

Upon determining the actual parts and part numbers for our design, its become increasingly apparent that the DIY kits neatly contain all of the parts we require. Considering this, we wanted to recap with you regarding the DIY kits from our projects. Are we allowed to purchase a kit and build our pulse monitor from it, or are we only allowed to use some parts but not the entire kit?

I answered the student after class, but I wanted to share my thinking more widely.

I like kits in many ways—I learned a lot from building Heathkits as a kid (see Thanks, Dad), but there are limits to what a kit can do for learning, particularly if people just assemble the kit with little or no attempt to understand what each part is for and how the whole thing works.

In a freshman design course, where students know very little or no electronics or programming when they start, the projects that they can reasonably tackle have to be quite simple. Most of the problems that they can solve have been solved before, and solutions by experienced engineers are easily found. Furthermore, commercial solutions are often available for less money than the parts needed to build them, due to the high cost of stocking, distributing, and shipping low-volume parts.

So students who see their role as answer getting are easily convinced to grab one of the good designs so readily available on the Internet and present it as the solution—very little work on their part and a good design, so what could be better?

The problem, of course, is that it is not the goal of the course to have a pulse monitor, an LED cube, or an ultrasonic rangefinder—none of those were part of the syllabus.  I can buy a pulse monitor for a few bucks, and for under $2 an ultrasonic rangefinder module that works better than anything the students are likely to come up. A finished RGB cube is a bit more expensive, but kits are still pretty cheap. One can buy the products the students could produce for far less time and effort than designing and making them. So the product is not the goal of the course. The goal of the course is for students to (begin to) learn to design things. This means doing things like writing specifications, drawing schematics, building prototypes, debugging prototypes, writing design reports, and doing iterative prototyping. It means learning a lot about how things work—not everything, but the specific details they need for the projects they have selected. The process of design is the core of the course, not the product of the design. Buying a kit and assembling it short circuits a lot of that design learning. You can build a kit and get it working with little or no understanding of how it works, and with no ability to modify the design. Of course, it is not the bundling of parts into a a kit that is the problem—copying a design off the Internet and buying the parts separately also does little to help students learn design. If you just copy other people’s work, then little learning takes place, and there is not much point to doing the class. Perhaps there is a little skill learning—how to use a soldering iron or an oscilloscope and how to order parts from a distributor, but not much how-to-design learning, which is the main point of the course. That is not to say that students shouldn’t be looking at designs on the web! Indeed, the freshmen do not have enough of the basic building blocks of electronics and programming to come up with their own designs de novo (though by their senior year they should be able to, if they continue into the bioelectronics or assistive technology: motor concentrations). So they need to look at solutions others have come up with, in order to know where to start on the design. But they should not just pick one design and implement it—they should be looking at several designs and trying to find common elements, figuring out what tradeoffs the different designers have made. Some of the designs come with good explanations of how they work—they should be reading those very carefully, so that they can follow the design decisions and reproduce the design from understanding. Understanding designs well enough to explain each part, how it functions, and what the effects would be of changing the part may be enough at this stage of their design learning. Part of the point of the freshman design course is to inspire them to want to learn the foundations in other courses, many of which seem to students to be irrelevant gatekeeper classes to survive by cram-and-forget techniques. If they have some idea why complex numbers are important for filter design, or why capacitors are useful, they are more likely to pay attention in those parts of math and physics classes and to retain the knowledge later on. Of course, it would be best if the math and physics classes included more of the inspiration themselves, but there are many different applications for the material, and it is difficult to find universally inspiring applications—what excites a math major, a computer science major, a physics student, or a bioelectronics student may be quite different, but the same course has to serve all of them. My role in the classroom is to provide explanations for those things that the students have trouble figuring out on their own, give them generic guidance in the design process, anticipate things they will need to know, and try to hurry them up (in the past most waited far too long before starting their projects, with the result that they get very little done). Having three different projects going on at once means that there is not enough class time for students to get everything they need from me—they must be reading on their own and trying to understand their project thoroughly, but based on the time logs they have been submitting, few in the class have been doing that—they mostly seem to be waiting for me to tell them what to do, which is not going to work for them. I had hoped that having students pick their own projects would inspire them to investigate the projects more deeply without my having to keep kicking them with assignments, but that only seems to be working for a few of the students. Some aspects of the design they can do—for example, everyone in the class should now be able to size a current-limiting resistor for an LED, and those who need amplifiers should be able to make a simple non-inverting amplifier out of an op amp. (Hmm, I should probably give a quiz on those ideas next week—maybe the students have absorbed less than I expect.) By this point, the students should be asking about those parts of the design that they don’t understand, but I’m not getting very many questions, despite starting each class asking for questions and covering student-requested material before anything that I have queued up as things students might need. A big chunk of what the class is trying to do is to nudge students away from regurgitating factoids that have been spoonfed into learning for the sake of understanding deeply enough to do things themselves—that means generating a lot of the questions themselves and seeking solutions by trying things out, and not just by looking up “the answer” or waiting for a teacher to tell them what to do and what to think. That is a hard transition for many students to make, as they have been steeped in “answer-getting” culture for the past 13 years. ## 2015 June 30 ### FG085 function generator Filed under: Circuits course — gasstationwithoutpumps @ 10:15 Tags: , , I finally had time today to assemble the FG085 function generator kit that I bought last September (it’s been a busy 9 months). I bought the kit from Sparkfun, on sale for$38 (it is now $50 from them or$47 from jyetech, who make the kit).

The assembly is fairly simple, as they’ve already done all the surface-mount soldering, leaving only the through-hole parts to solder—about 200 solder points.  I managed to make two stupid mistakes that required unsoldering—I soldered one of the 21 push buttons on the wrong side of the board and I forgot to peel the protective strip off the LCD display before soldering the BNC connector in place.  The BNC connector is strangely mounted, so that the front panel can’t be removed without unsoldering the connector.  Both errors were ones I realized right after I made them, and were fairly easy to fix.

The function generator seems pretty easy to use, though not all the user interface is intuitive, as there are well-hidden features like that pressing “.” twice allows you to change the duty cycle of the square wave.  Jyetech has pretty good documentation on-line though, including a user’s manual and a schematic for the function generator.

They do direct digital synthesis with a 2.5MHz sampling rate and an 8-bit DAC, then scale the amplitude and add an offset.  At 40Hz and below they drop the sampling frequency to 10kHz,in order to get a more precise frequency. They only claim to go to 200kHz (12.5 samples per period), but their low-pass filter at the DAC has a corner frequency of 1.426MHz (C=180pF, R=470Ω+150Ω) if their schematic is right. (Update 2015 Jun 30: that’s from Schematic_085F.pdf—from Schematic_085G.pdf, the corner frequency should be 713kHz.) So they are not really filtering down to 200kHz, and the software allows the user to enter up to 999,999Hz (in steps of 1 Hz).

I checked the amplitude of the output using my Bitscope USB oscilloscope, with the FG085 set for 5V peak-to-peak and 0V offset:

The low-pass filter in the FG085 function generator seems to be set for about 400kHz.

At the higher frequencies the waveform is far from sinusoidal (3 points per period at 833,333Hz), and the beating due to phase change if the period is not exactly a power of 2 times the sampling period makes for pretty dancing patterns on the oscilloscope, but the amplitude is hard to read. I think that the bandwidth is about 400kHz, though the distortion of the waveform is pretty bad when the number of samples is tiny, so treating the signal as a sine wave is a bit dubious above about 100kHz. Because the bandwidth is so different from what I compute from their schematic, I suspected that either their resistor value or capacitor value in the low-pass-filter is misreported. A 2kΩ resistor instead of a 470Ω resistor for R9 leading into a 180pF capacitor C12 would match the behavior better (or C13 was populated despite the schematics).  It is a little hard to figure out the size of capacitors on the PC board (they’re not labeled), but the resistor R9 does appear to be 470Ω, so I’m still a little mystified where the 400kHz corner frequency is coming from.

I don’t know for sure whether the inaccuracy in the voltage measurements at low frequency is from the frequency generator being miscalibrated or from the Bitscope’s lack of calibration, but I suspect the function generator. If the 2V peak-to-peak 1kHz signal out of my Kikusui oscilloscope is accurate (a bit questionable), then the Bitscope is only reading 1% high, so the voltage errors would be in the function generator.

I tried doing an FFT with the Bitscope, to see how much harmonic distortion there is (limited by the 8-bit resolution of both the function generator and the oscilloscope. The oscilloscope reports components at the desired frequency, but also at 2.5MHz ± the desired frequency. These two sidebands are about 40dB down, which is not too bad for an 8-bit DAC.

Incidentally, I noticed that they used a very cheap and noisy DAC—just a resistor ladder with ordinary output pins from the processor driving it. Looking at a low-frequency waveform, there seems to be a glitch of about 30mV (on a 2V peak-to-peak signal) at the 8 transitions that correspond to the top three bit positions. This sort of non-linearity error is what you would expect from the very low-quality DAC they used. On the Bitscope FFT, the errors are near the noise floor, but there is a consistent set of 9th, 11th and 13th harmonics about 50dB below the fundamental. The FFT also points out a 3rd harmonic about 40dB down from the fundamental.

I suspect that they could have made a better function generator for about the same price by using a more modern microprocessor—Freescale’s KL25Z includes a 12-bit DAC with a 550kHz bandwidth, with much better non-linearity than what they achieved—the chip is a bit more expensive, but would add at most $1 to their parts cost—it may actually be cheaper, if it replaces some of their other parts as well. They adjust the gain and offset in amplifiers after the DAC, but it looks like they just use a 100-position digital potentiometer for the gain and PWM from the ATMega168 for the offset. They monitor the output voltage (before a 22Ω output resistor, not directly at the output), and adjust the offset and voltage to get the peaks more or less right. Given that the digital pot has only 100 positions, the accuracy on that can’t be much better than 1% of the largest amplitude setting, or 0.1V. For under$50, I did not expect a super-high quality function generator, and I didn’t get one, but FG085 looks like it might be usable for a number of less critical applications. I’ll probably try doing the labs in the book using it—most of them don’t rely on a very high quality sine wave, nor a very high frequency.  The ability to set the DC offset to 0V makes this a much more useful function generator than the one built into the Bitscope or the analog Elenco FG500 that I’ve talked about before.

## 2013 July 10

### Some failed designs

For the past couple of days I’ve been exploring variations on the Blinky EKG project, looking at alternative approaches.

For example, I looked at the possibility of eliminating the most expensive part (the instrumentation amp), and decided that building my own instrumentation amp out of op amps and discrete resistors was unlikely to be reliable.  I discovered (after doing calculations for 2-op-amp and 3-op-amp designs) that 1% tolerance on the resistors would produce poor common-mode rejection. In Common-mode noise in EKG, I reported measurements of the common-mode noise with a fairly short twisted-pair connection to the EKG electrodes (close to a best-case scenario).  I concluded that the common-mode noise was way too large for using unmatched resistors to be a reasonable design, and using an integrated instrumentation amp is still a good choice.

Yesterday,  I tried turning the question around? Could I eliminate the op-amp chip?  Currently, I’m using the op amp for two things: to provide a unity-gain buffer to make the reference voltage source between the power rails, and to do second-stage amplification after a high-pass filter removes the DC offset. To eliminate the op-amp chip, I need to replace both these functions.

Replacing the unity-gain buffer seems fairly easy—I could use a low-drop-out linear regulator to generate the reference voltage instead of a voltage divider and unity-gain buffer, which would be somewhat smaller and cheaper (11¢ rather than 25¢ in 100s).  I didn’t have an LDO linear regulator at home, so I tried using a TL431ILP voltage reference instead.  Unfortunately, it provides very little current, and was unable to maintain the desired voltage when hooked up to the reference voltage input of the instrumentation amp.  I believe that a part like the LM317L would work fine, though, and I may want to test that at some point.

Removing the second-stage amplifier is more problematic.  I can set the gain of the instrumentation amp up to 2000 or 2500 easily, but any DC component in the input differential signal results in saturating the amplifier (with a 6v output range, and a 1mV AC signal, we’d need the DC bias to be less than 1mV also to avoid hitting the power rails).  I tried putting high-pass filters in front of the instrumentation amp, but with the long time constants needed to avoid filtering out the EKG signal, the filters never settled to within 1mV of each other, and the instrumentation amplifier always saturated.

So I need to keep the first-stage gain small enough to avoid saturation, which means I need a second-stage amplifier.  I could use a single op amp for the second stage and a low-drop-out regulator for the reference voltage, which would produce a cleaner output signal (since my voltage-divider-plus-unity-gain-buffer reference introduces noise from the power lines).  The MCP6001 single op amp is only 18¢ in 100s (rather than 25¢ for the MCP6002 dual op-amp), but the MCP6001 is only available as a surface-mount component, which I think is inappropriate for a first soldering project.  The MCP6001 + LM317L would cost about 4¢ more than the MCP6002.

I considered redesigning the Blinky EKG to use the LM317L voltage regulator and the MCP6002, even though half the MCP6002 would be unused, but the LM317L needs a 1.5mA load to maintain regulation, and that seems like a lot for a battery operated device—more than the op amp or the instrumentation amp use (though less than the LED when it is lit).  Even using a TL431ILPR voltage reference (10¢ in 100s) and the unity-gain buffer would only need 1mA, and would save one resistor.  There are lower-current voltage references, like the LM385, but they cost a lot more (42¢ in 100s).

The non-rechargeable CR2032 batteries I’ve been using for the Blinky EKG have about a 225 mAh capacity, and cost about 19¢ in 100s (but the design needs 2, so 39¢).  I could probably get about a 100-hour life with the present Blinky EKG design—I need to measure the current and the duty cycle of the LED to get a better estimate.

The Blinky EKG weighs about 20g (not counting the wires to the electrodes), which is a bit heavy for a pendant or brooch. Most of the weight is in the batteries, but a lighter battery would give up a lot of running time.   The smaller batteries also cost a lot more, probably because Digikey only buys them in quantities around 10,000 rather than in the millions. (From other suppliers CR2032 batteries cost about 60¢ in 100s, not under 20¢).

It has been good to fool around with the Blinky EKG design, as it has gotten me to think a bit about design issues other than the first can-I-get-it-to-work one.  I rarely get my students past that point in their thinking, and I’m not sure how I would do so, as there is always so much time pressure to cover new stuff, that they get very little time to tinker with designs.

## 2013 July 8

### Common-mode noise in EKG

Filed under: Circuits course — gasstationwithoutpumps @ 18:00
Tags: , , , , , , , ,

In 2-op-amp instrumentation amp, I said

I’ll have to make some measurements later this week to see how large the common-mode noise on the EKG signals really is. Of course, it is likely to be highly variable, depending on the electrodes and the wiring to them, but ball-park estimates would be useful.

If the AC common-mode voltage is ±200mV and we have worst-case resistor values, then we would have a ±8mV common-mode output from the instrumentation amp. With the lowest differential amplification (6.6 at Rgain=∞), a 1mV EKG signal would be smaller than the common-mode noise. Such a large common-mode voltage would easily justify the expense of the instrumentation amp chip.  (Note: large DC common-mode voltages don’t matter, as the DC-blocking capacitor I used after the instrumentation amp can eliminate them.)

If the AC common-mode voltage is only ±1 mV, then the Blinky EKG could probably work even with very poor common-mode rejection in the instrumentation amp, and building it out of op amps and discrete resistors is feasible.

So today I tried measuring the common-mode signal.  As it turns out, I had built an EKG amplifier on the protoboard that had a split Rgain resistor (two 12kΩ resistors in series).  The node between the 2 resistors should be at (Vp+Vm)/2, the common-mode voltage.  That node was fed to a unity-gain buffer, so I tried measuring the AC common-mode voltage.

I did this with two different tools: my Fluke 8060A multimeter (which measures RMS voltage) and my BitScope Pocket Analyzer, which can measure peak-to-peak voltage.  With the multimeter, I saw about 20–35 mV RMS, and with the BitScope I saw 90–130 mV peak-to-peak (depending on where my arms were relative to the wires to the electrodes).

Trace of common mode noise recorded by the BitScope Pocket Analyzer, at 20mV/division. The fundamental frequency of the noise is 60Hz.
Sorry about the black background—the manufacturers are promising a white background as an option in some future release of the software. Maybe they’ll add some labels for the x and y tick marks at the same time!

If I take the leads off the electrodes and clip them all together, then the common-mode noise drops below the level that the BitScope can measure (its noise floor is about 10mV). The multimeter also has trouble, reporting values from 0.05mV to 1.5mV. If I put a 3.3MΩ resistor between the two signal leads, and connect one of them to the Vref lead, then the multimeter reports about 13–20 mV RMS and the BitScope around 80–90 mV peak-to-peak.

The common-mode problems are definitely coming from the wiring to the electrodes.  With 130mV peak-to-peak common-mode voltage and a common-mode gain of 0.02 or 0.04, we’d have 2.6–5.2 mV of common-mode noise at the output of an instrumentation amp made with discrete resistors and op amps.  If the gain of the amplifier were around 6.6, we’d have a peak-to-peak signal of about 6.6 mV for the EKG signal, and the details of that signal would be buried in common-mode noise.

If we got lucky with the resistor values and they matched better than the specs claim (which is actually fairly likely), then the Blinky EKG would sort-of work with discrete op amps—as long as the 60Hz noise is no worse that what I saw.  Many people are likely to be in electromagnetically noisier locations than my room, so the common-mode noise is likely to be much higher sometimes, and even with lucky matching of the resistors the common-mode signal is likely to swamp the real signal.

Thus I have confirmed that an integrated instrumentation amp is needed for the Blinky EKG, and there is no point trying to design one using discrete resistors and op amps.

## 2013 July 7

### 2-op-amp instrumentation amp

Filed under: Circuits course — gasstationwithoutpumps @ 20:50
Tags: , , , , , , , ,

Last summer, I tried building an instrumentation amp using the MCP6002 op amps and external discrete resistors, and ended up with an amplifier that had terrible common-mode rejection, which is why I decided to use the INA126P instrumentation amp chip for the Blinky EKG boards and for the instrumentation amp protoboards.

I think I’d like to revisit that idea though, to see if I can make a cheaper Blinky EKG.  If I put together a Blinky EKG kit with the current design, the parts would cost about $9.30 (buying quantities of 100). The mist expensive single part is the instrumentation amp at$2.44.  If I could get the 2-op amp instrumentation amp to work using discrete components, I might be able to save most of that.  Replacing them MCP6002 dual op amp with an MCP6004 quad op amp and 4 more resistors would cost about 22¢ rather than $2.44, bring the parts cost down to around$7.07.

I could also reduce the price by using surface-mount devices (SMDs) instead of through-hole components. An instrumentation amp like the INA826 (\$1.34 in 100s) would be a good choice if I went with SMDs. But if I used SMDs, the Blinky EKG would probably have to be a finished product rather than a kit, which would add substantial manufacturing costs, especially for things like a case, which could be omitted in a kit. The idea of a blinky board is to be an easy soldering project for beginners, so I’m not sure that a pre-assembled blinky EKG has much appeal for me (which is not to say there is no market for it, just that I’m not particularly interested in designing for that market).

I looked at my old post and realized that I had miscomputed the gain for the differential signal, and never computed the gain for the common-mode signal.  If the resistors are perfectly matched, the common-mode gain is 0, but without the laser trimming that makes the instrumentation amp chips so expensive, we’re not going to get perfect matching.  The classic approach of adding trimpots takes up too much space and ends up costing almost as much as using an instrumentation amp chip.

So the rest of this post is dedicated to better understanding the 2-op-amp instrumentation amp.  I drew a schematic of a possible design, in order to have names for the parts and signals.

Schematic drawn with SchemeIt and captured as a screenshot. The native exports into PNG and PDF formats were useless, because SchemeIt messed up the Unicode character Ω. I also had to do this as a 24-bit PNG, because WordPress.com seems to mess up 8-bit PNGs (they look fine when editing, but not in the Preview.)

To analyze the circuit assuming ideal op amps (so the voltage difference between the two inputs of the op amp is 0), we need to look at the current through each resistor:

$I_{1} = (V_{out} - V_{p}) /R_{1}$
$I_{2} = (V_{p} - V_{mid}) /R_{2}$
$I_{3} = (V_{mid} - V_{m}) /R_{3}$
$I_{4} = (V_{m} - V_{ref}) /R_{4}$
$I_{gain} = (V_{p} - V_{m}) /R_{gain}$

We also have that
$I_{1} = I_{2}+ I_{gain}$ and $I_{4} = I_{3} + I_{gain}$, by Kirchhoff’s current law.

We can add to get $I_{1} + I_{4} = I_{2} + I_{3} + 2 I_{gain}$, which can be expressed as
$(V_{out} - V_{p}) /R_{1} + (V_{m} - V_{ref}) /R_{4} =$
$(V_{p} - V_{mid}) /R_{2} + (V_{mid} - V_{m}) /R_{3} + 2 (V_{p} - V_{m}) /R_{gain}$

If $R_{1}=R_{4}$ and $R_{2}=R_{3}$, then we can multiply both sides by $R_{1}$ to get
$V_{out}-V_{ref} - (V_{p}-V_{m}) = R_{1} (V_{p}-V_{m}) (1/R_{2} +2 /R_{gain})$,
or
$\frac{V_{out}-V_{ref}}{V_{p}-V_{m}} = 1+ R_{1}/R_{2} + 2 R_{1}/R_{gain}$.

For the values in the schematic above the differential gain is 6.6 + 112kΩ/Rgain.

To look at common-mode gain, it is best to solve the pair of equations for the currents I1 and I4. Being lazy, I used maple to do the algebra:

solve( { (vout-vp)/r1= (vp-vmid)/r2 + (vp-vm)/rgain, (vm-vref)/r4=(vmid-vm)/r3+(vp-vm)/rgain, \
vor=vout-vref}, {vout,vor,vmid});
simplify(taylor(subs(vp=vcomm+vdiff/2+vref, vm=vcomm-vdiff/2+vref, rhs(%[3])),vdiff));


which produced

vcomm (-r2 r4 + r1 r3)
- ---------------------- +
r2 r4

2 r1 rgain r4 + r2 rgain r4 + 2 r1 r2 r4 + 2 r1 r4 r3 + r1 r3 rgain
------------------------------------------------------------------- vdiff
2 r2 rgain r4



that is,

$V_{out}-V_{ref} = V_{common} \left(1- \frac{R_{1} R_{3}}{R_{2}R_{4}}\right) + V_{diff} \frac{R_{1}}{R_{2}}\left(1 +\frac{R_{2}}{2 R_{1}} + \frac{R_{2}}{R_{gain}} + \frac{R_{3}}{R_{gain}} + \frac{R_{3}}{2 R_{4}}\right)$,
where $V_{diff} = V_{p}-V_{m}$, and $V_{common} = \frac{V_{p}+V_{m}}{2} - V_{ref}$

We can check for some copying errors by simplifying with $R_{1}=R_{4}$ and $R_{2}=R_{3}$, where we get a common-mode gain of 0, and differential gain of $\frac{R_{1}}{R_{2}} + 1 + \frac{2 R_{1}}{R_{gain}}$. Note that the common-mode gain is independent of the value of Rgain, and depends only on the matching of the other resistors.

If R1 and R3 are 1% low, and R2 and R4 are 1% high, then the common-mode gain is 0.039. If R1 and R3 are 1% high, and R2 and R4 are 1% low, then the common-mode gain is –0.041. If you prefer thinking in decibels, the common-mode rejection with 1% tolerance resistors could be as poor as 27.8dB (compared to 80dB to 90dB for an INA126P chip).

I’ll have to make some measurements later this week to see how large the common-mode noise on the EKG signals really is. Of course, it is likely to be highly variable, depending on the electrodes and the wiring to them, but ball-park estimates would be useful.

If the AC common-mode voltage is ±200mV and we have worst-case resistor values, then we would have a ±8mV common-mode output from the instrumentation amp. With the lowest differential amplification (6.6 at Rgain=∞), a 1mV EKG signal would be smaller than the common-mode noise. Such a large common-mode voltage would easily justify the expense of the instrumentation amp chip.  (Note: large DC common-mode voltages don’t matter, as the DC-blocking capacitor I used after the instrumentation amp can eliminate them.)

If the AC common-mode voltage is only ±1 mV, then the Blinky EKG could probably work even with very poor common-mode rejection in the instrumentation amp, and building it out of op amps and discrete resistors is feasible.

Next Page »