# Gas station without pumps

## 2014 June 29

### Soldered EKG from op amps

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

Today I decided to solder the EKG design from Instrumentation amp from op amps fine for EKG onto one of my instrumentation amp protoboards, leaving out the instrumentation amp chip—I wanted to see how much trouble it would be.  As it turned out, the build was fairly straightforward, but a little tedious. There are only dedicated spaces for 8 resistors on the board, but there are 9 resistors in the design I used, so one had to go elsewhere on the board.  I deliberately left out the low-pass filter on this implementation (eliminating one capacitor), which did not make a huge difference—I ended up with about 58µV peak-to-peak of 60Hz noise on my input signal (compared to about 40µV in the previous design with a capacitor for low-pass filtering), which is fairly small compared to the 870µV R spike or the 220µV T wave.  The 60Hz interference was large enough to interfere with the P wave and make it difficult to see whether or not there was a U wave.  Of course, these measurements were made in my bedroom/lab, which has a lot less 60Hz interference than the lab the students work in.  I’ll have to take the board into work and see how bad the interference is in that space.

Using a digital filter to remove the 60Hz noise reduced the 60Hz interference to under 100nV peak-to-peak (way lower than other noise components), producing very nice waveforms, even when sampling at 360 Hz.  I’ll probably want to include a digital filter Python script in the book so that people can see the cleaned up signals, even if there isn’t room in the course to design digital filters.

I still have to decide whether to have students do the EKG amplifier without the INA126P chip, using only op amps. Wiring up the bigger circuit takes time, and I’m not sure that 6 hours of lab will be enough time for students to debug their design and get it soldered—it took them long enough to solder the EKG with the INA126P chip, which has fewer components and fewer wires to route.  It took me quite a while to solder up the board, so it would probably take the students far too long.  Is the pedagogic value of designing and building a 2-op-amp instrumentation amp worth the time? I do want the students to end up with an EKG to take home, as it is a tangible artifact that can demo the function of.  I’m thinking that I could even drop the soldering of the pressure-sensor amp (since they don’t take home pressure sensors), and add soldering of the microphone pre-amp.  If I do that, I’ll probably want to redesign the protoboard again, making it an op-amp protoboard with no instrumentation amp slot, but with more resistor spaces.

Cutting one part that costs about $2.70 and the$1.90 thermometer might justify my switching back to the resistor assortment I used in Winter 2013:  1120 piece resistor assortment for $17.39 instead of 1280 piece resistor assortment (currently$10.65) without raising the lab fee.  Why would I want fewer resistors at a higher price? The 1120-piece assortment is 10 each of 112 values, while the 1280-piece assortment is 20 each of 64 values.  Also the 64 values don’t seem to be very repeatable from set to set, and some sets has duplicates (so only 62 or 63 different values).  The 112-value sets seem more reliably useful.  A hobbyist might be better off going one step further to the 3700-piece resistor assortment (25 each of 148 values), but I can’t justify the $31.48 price for my class. (The extra$14 would probably raise the lab fee.)

## 2014 June 26

### Instrumentation amp from op amps fine for EKG

Filed under: Circuits course — gasstationwithoutpumps @ 22:55
Tags: , , , , ,

As I mentioned in Instrumentation amp from op amps still fails, I’ve been trying to decide whether to have students build an instrumentation amp out of op amps in the circuits course.  I decided that it wouldn’t work for the pressure-sensor lab, because of the large DC offset.  One could calibrate each amplifier, either in software (by recording a a few seconds of 0 pressure difference, and subtracting a constant fit to that region from the data) or in hardware, but I’d rather they had a more straightforward experience where the DC offset was small enough to be ignored.

I conjectured that instrumentation amp built from discrete op amps would work ok for the EKG lab, though, as the EKG already has to deal with much larger input voltage offsets due to differing electrode-skin contact.  So I added a second stage  with a gain of 81 to the instrumentation amp in the previous post with a gain of 19, to get a combined gain of 1539.  I put in the high-pass filter needed to eliminate the DC offset, and a low-pass filter to reduce noise slightly (and make aliasing less of a problem).  The corner frequency is a bit high (60Hz noise is not going to be reduced much), but that may allow a better view of the fast R spike in the EKG waveform.

The EKG circuit has four modules: a virtual ground (here set to 0.5v), an instrumentation amp, a high-pass filter to eliminate DC bias, and a second-stage non-inverting amplifier with some low-pass filtering.

The amplifier worked surprisingly well. I did sometimes have trouble with 60Hz noise, but it did not seem to be any worse than the amplifier based on the INA126P. I can remove the noise by digital filtering, though I’ve only played with that by post-processing the data files, not by designing a notch filter to run in realtime on the KL25Z (something to do when I have more time).

Here are a few traces made with EKG circuit above, feeding into the PTE20-PTE21 differential input on the KL25Z board, recorded using PteroDAQ.

This is lead I (LA–RA), without filtering, showing a rather disturbingly large 60Hz noise signal.

This is lead I (LA–RA), showing how the digital filter cleans up the signal. This was Bessel bandpass filtered to 0.3Hz to 100Hz, followed by notch 57Hz–63Hz, followed by notch 117Hz–123Hz. Each filter was a 5th-order Bessel filter, applied first forward in time then backward in time (using scipy’s filtfilt function).

This is lead II (LL–RA), which for some reason had rather low noise even without filtering.

I noticed that sampling at 360Hz allowed me to see a bit more of the structure of the S and T complex than I’ve seen previously, particularly in lead II, and I can even make out a little bump of a U wave just after the T wave.

I now have to decide whether to have students do the EKG amplifier without an INA126P chip, using only op amps. The design will be fairly heavily constrained, as they’ll need to get it all working on a single MCP6004 chip, but it will justify my spending a bit more time on how instrumentation amps work.

I may redesign the blinky EKG to use a single MCP6004 chip also, which would reduce the price of that substantially.

### Instrumentation amp from op amps still fails

Filed under: Circuits course — gasstationwithoutpumps @ 17:02
Tags: , , , ,

I’ve been trying to decide whether to have students build an instrumentation amp out of op amps in the circuits course.  Currently the INA126P instrumentation amp chip that I have them use is a black box to them, even though I include an explanation on the lab handout showing how it is internally a pair of op amps and 4 resistors:

Internally, the INS126P instrumentation amp is two op amps and 4 resistors.

I won’t repeat that presentation here (there’s a condensed, early version of it in a previous blog post).  I’ve not actually lectured on the 2-op-amp design before the instrumentation-amp lab, in class, though I did manage to talk about the 3-op-amp instrumentation amp this year (a waste of time, since they did not really process the ideas).

What I was interested in today was whether the pressure-sensor lab could be done entirely with op amps, rather than with the more expensive INA126P chip.

I decided to design an amplifier with a gain of around 200 and an output reference voltage around 0.5 v (based on a 3.3v supply), using the 2-op-amp design and MCP6004 op amps. Here is what I came up with:

This is the design I came up with and built. It works, sort of.

The amplifier amplifies and seems to have about the right gain, but there is a large DC offset on the output: about 0.24V, which translates to an input offset of about 1.2mV. I checked with a multimeter, and the negative-feedback voltages are indeed about that far apart, while the inputs from the pressure-sensor bridge are less than 40µV apart. The pressure sensor sensitivity is about 80µV/kPa/V, or 264µV/kPa with a 3.3V supply. If I use the pressure sensor with a blood-pressure cuff, I’ll want to go up to about 180mmHg or 24kPa, so the sensor output should be in the range 0–6.3mV. An offset of 1.2mV is huge!

If I remove Rgain from the circuit, the output offset drops to 20.88mV, which is 1.1mV referenced to the input (close to the 1.22mV measured at the negative feedback inputs).  Further removing R2 or R4 does not change the voltage difference between the negative-feedback inputs.  In fact removing all three of Rgain, R2, and R4, so that we have two unity-gain buffers (with 180kΩ and 10kΩ feedback resistors), still leaves the negative feedback points 1.22mV apart.  Each seems to be about 0.6mV from the corresponding positive input.

The problem is that the input offset voltage of the MCP6004 op amps is only guaranteed to be between –4.5mV and +4.5mV:  I’m lucky that the input offset voltage is under 1mV!  Even the INA126P instrumentation amps that we’ve been using have an input voltage offset of up to 250µV (150µV typical). One can obviously get better instrumentation amps, but the selection in through-hole parts is limited, and I’d have to go to an instrumentation amp costing $4.25 (LT1167CN8#PBF) instead of$2.68 to get the input offset voltage down to 20µV.

I’m going to have to rewrite the section of the book on instrumentation amps, to discuss (at least briefly) offset voltages.  I had originally thought that that the signals we were looking at were big enough that the offset voltages didn’t matter. For the INA126P, a 150µV offset would be about 0.6kPa, while the 1.22mV offset I was seeing in my homemade instrumentation amp would be about 4.6kPa.

I wonder also whether I can make an EKG circuit using this 2-op-amp instrumentation amp circuit.  The EKG already has to deal with potentially large input voltage offsets due to differing electrode-skin contacts.  In fact those offsets may be over100mV, far larger than the 1.2mV from the amplifier.  I’ll have to add another stage of amplification (after a high-pass filter), but that shouldn’t be a problem. I looked at this problem a year ago in 2-op-amp instrumentation amp and Common-mode noise in EKG, and concluded then that common-mode noise would be too large, but I’m tempted to try again, using the design here with gain 19 and a second stage with a gain of around 80 (for a combined gain around 1520), as last year I rejected the idea before actually building the circuit.

### What you do in college may matter more than where you go

Filed under: Uncategorized — gasstationwithoutpumps @ 00:48
Tags: , , , , ,

Back in May, I read a blog post (Life in College Matters for Life After College) that pointed to the Gallup-Purdue Index Report 2014. I finally got the time to download the report and look at it.

The report has a rather ridiculous interpretation of copyright on its copyright page: “It is for your guidance only and is not to be copied, quoted, published, or divulged to others.” This is particularly ridiculous for a report that they are distributing for free—I think that they have a piece of boilerplate that they put on all their reports, written by lawyers who want to claim far more that copyright law really provides.  They’ve got deeper pockets than me though, so the threat is effective—I won’t directly quote them in my blog, but just summarize what I see as the main points.  If I mangle their message, they have only their own over-zealous lawyers to blame.

What the report is ostensibly about is whether college prepares students for an “engaging” job and a good life.  They were looking for whether students were engaged in their jobs and at five measures of well-being that Gallup has used in other studies: purpose, social, financial, community, and physical. They were also looking at how attached alumni were to their alma maters (which, of course, is primarily what Purdue was interested in, as that determines how much money they can extract from alumni).

Basically, they started with the assumption that the point of college is to get a “great job” and a “great life” (a debatable point, but a widely held belief).  They then tried to determine what produced these outcomes, by interviewing 30,000 graduates.  Note that they did not interview those who quit or were kicked out of college—they were only considered those that college thought had succeeded.  It might be interesting for them to look at the outcomes for those who dropped or failed out also, to see whether the things they think mattered in college also affected the students who left without a degree.  (I suspect that the effects would be even stronger, because of the higher variance in the outcomes, but guessing about sociology is not one of my strengths.)

Their main result was that it didn’t really matter much where people went to college (other than that results were consistently worse at for-profit schools)—what mattered is what they encountered there.  Having an inspiring professor who cared about them, excited them about learning, and encouraged them doubled the odds of their being engaged at work after college. Internships in which they applied their learning, multiple-term projects, and being extremely active in extracurricular activities also doubled the odds of their being engaged at work.

(They use the term “odds” rather than “probability” consistently, so I’m not sure if they mean the probability p or the odds ratio $\frac{p}{1-p}$. If p is small, these are almost the same, but the overall engagement at work for college grads was reported as 39%, so it makes a difference here.  At one point in the report they mention that 40% of students finishing in 4 years or less are engaged in their jobs compared to 34% of those who took five and a half or more years, claiming that completing in 4 years doubles the odds of engagement.  I can’t come up with any definition of “odds” that makes this more than a 30% difference.)

I think that UCSC does manage to provide some engaging faculty—most of the students I talked to in senior exit interviews had at least one faculty member who excited them about learning (but that’s fairly common—63% of graduates reported that in the Gallup-Purdue survey).  I don’t know that we do as well at providing professors who show that they care about the students or providing mentors who encourage students to pursue their dreams—those are hard to provide at scale, as they rely on matching personalities as well as having enough faculty time to spend. Indeed, in the Gallup survey only about 27% of graduates felt that professors cared about them as a person and only 22% felt they had a mentor who encouraged them, so we’re not alone in finding this difficult to supply.  I suspect that students doing senior theses get more mentoring than those doing group projects, but a lot depends on the student and whoever is supervising the work.

One thing that the Jack Baskin School of Engineering at UCSC is doing right—all the students in bioengineering, computer engineering, electrical engineering, and computer game design are required to do 2-quarter or 3-quarter-long capstone projects.  (That alone should be a 1.8× on the odds of being engaged at work, and only 32% of students in the survey reported having that experience.)  Our students do not do so well on the “extreme extracurricular activity”, though, as few engineering students feel they have time for much in the way of extracurriculars.  Internships are something that UCSC could be much better at—there is a huge industry base only 40 miles away in Silicon Valley, but students are left on their own for finding internships, and not very many do.

The two strongest predictors of engagement were not really what the college did, but what students thought about the college:  if they thought “the college prepared me well for life outside college” or that the college was “passionate about the long-term success of its students”.  These raised the odds of engagement at work by 2.6× and 2.4× respectively. Causality is not clear here, as these attitudes may have resulted from their engagement at work, rather than being causes of it.

The report is very sloppy about confounding variables:  they report that women are more engaged at work than men, and that arts, humanities, and social science majors are more engaged than science or business majors.  But they don’t seem to have done anything to determine which of the two highly correlated variables is the causal one here: gender or major.  Their sample is large enough that they should have been able to get at least a strong hint, despite the correlation.

One unsurprising result: those who took out large loans as students were much less likely to be thriving in all 5 areas of well-being than those who took out small loans or no loans. Since financial well-being is one of the areas, and large loans make it difficult to achieve financial well-being, this is hardly a surprising result.  It would have been more interesting if they had reported differences in just the other four areas—did the large loans have any effects other than the obvious financial one?  They’ve got the data, but they didn’t do the analysis (or they’re not sharing it in the free report, which seems more likely—I’m sure they’ll share it for a hefty consulting fee).

Given that there was almost no difference in well-being based on public vs. private or selective vs. non-selective colleges, the big negative correlation of large loans with well-being sounds like a strong argument to go to a college you can afford, rather than taking out large loans. (Again, the report did not attempt to look at confounding variables for the for-profit schools—how much of their poor performance was due to the large loans they encouraged their students to take out?)

The results for alumni attachment were much stronger than for well-being or job engagement, probably because the background level of alumni attachment was fairly low—only about 18% of college graduates were emotionally attached to their colleges by the criteria used in the poll.  The biggest drivers for emotional attachment were whether they felt the college had prepared them well and whether they felt it was passionate about the long term success of the students.  Again, I question the causality here—it seems likely that those who are emotionally attached are more likely to hold these beliefs, irrespective of what the college actually did.

I’m also confused by their “odds” again, where they report 48% of a group being emotionally attached as 6.1× the odds of another group where 2% are emotionally attached.  I don’t see how they computing their “odds”—it is a very odd computation indeed! Update: perhaps the odds they mean are $\frac{p(x | y)}{p(x | \neg y)}$, in which case they are comparing the 48% to some unprovided number, probably a little lower than the background 18%.  I’m still having a hard time making that 6.1.  Maybe $\frac{p(x | y)(1-p(x|\neg y))}{(1-p(x|y))p(x | \neg y)}$?  I can’t seem to make anything match their numbers.

Although the basic conclusion of the study seem reasonable to me (that what happens to you in college is more important than where you go to college, and that large loans make you miserable), the survey seems rather sloppily done, confusing correlation with causality, not attempting to disentangle confounding variables, and doing some sort of arithmetic that seems completely inconsistent so that the “odds” they report are incomprehensible. Also, they asked few questions and every question they asked seemed to have about the same effect on the odds, so I don’t know whether the survey was actually measuring anything (no negative controls).

I’d hesitate to invest money or make academic planning decisions based on this report.  I think that Purdue wasted a lot of money on a load of crap (unless they got a private report with a lot better data and analysis).

## 2014 June 22

### Loudspeaker relaxation oscillator

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

Yesterday I played around a little with a hysteresis oscillator (relaxation oscillator) using a loudspeaker as the feedback device, rather than a resistor, as shown in the schematic below:

Hysteresis oscillator with loudspeaker for feedback. Changing the value of C1 should produce different frequencies. I used 3.3v from the KL25Z board as my power supply.

I was hoping that I could get the oscillator to resonate at a predictable frequency, based on the gain or the phase shift of the voltage divider consisting of the loudspeaker and the capacitor. Of course, a relaxation oscillator that relies on the hysteresis of a Schmitt trigger is emphatically not a linear circuit, and there is no reason to think that using a loudspeaker instead of a resistor for the feedback element will simplify anything.

I did have a few questions:

• Would it oscillate? (I saw no reason it shouldn’t.)
• At what frequency would it oscillate? Was there any relationship to the gain or phase shift of the loudspeaker and C1 voltage divider?
• How stable was the frequency?
• Would it be loud enough to hear on the loudspeaker?

Before even wiring up the circuit, I ran some gnuplot scripts with my model of the loudspeaker to see what the gain and phase of the voltage divider would be:

Gain from the voltage divider, with different C1 values. Note that I’ve also added a line for an RC low-pass voltage divider, for comparison.

Phase change from the voltage divider, with different C1 values. Again I’ve added a line for an RC low-pass voltage divider, for comparison.

Note that the loudspeaker+capacitor provides a bigger phase change at high frequency and a faster rolloff in gain than an RC filter would. I was hoping that the resonant peaks at high frequency might capture the oscillator and provide a moderately stable output.

To avoid load on the oscillator from a multimeter (used to measure frequency) or oscilloscope, I buffered the output with a couple more Schmitt triggers.

C1 frequency
electrolytic measured with Fluke 8060A
470µF  27Hz
220µF  50–55Hz
47µF  265–285Hz
33µF  375–406Hz
4.7µF  3077–3401Hz
ceramic measured with Fluke 8060A
4.7µF  2.56–2.66kHz
0.1µF  116–122kHz
ceramic measured with Bitscope oscilloscope
47nF  245kHz
22nF  286kHz
10nF  587kHz
2.2nF  1.1MHz

The frequencies were not stable, but shifted unpredictably within a fairly wide range. The Fluke 8060A meter is not capable of frequency measurements above about 100kHz, so I used the FFT on the Bitscope USB oscilloscope for the higher frequencies. For the Bitscope measurements, I took whichever frequency seemed to come up most often—usually the lowest frequency.

The audio-range frequencies 27Hz to 3.4kHz were clearly audible on the loudspeaker (even annoying at the higher frequencies), and the fluctuations in frequency were audible also—they are not an artifact of the measuring equipment.

Looking at the input and output of the oscillating Schmitt trigger for a moderately large capacitor is instructive:

The green trace is the input to the Schmitt trigger—it seems to be a fairly clean triangle wave.
The yellow trace is the output—it jumps when the inverter switches, but the current limitation of the Schmitt trigger seems to keep it from being a square wave.
C1 was 33µF here, and the timebase is 2ms/div.

The current limitation on the Schmitt trigger output seems to be the controlling factor here, so we should be modeling this as a current source and capacitor, with the feedback impedance being irrelevant until it gets large enough that the Schmitt trigger output is voltage-limited instead of current-limited.

I tested this by replacing the loudspeaker by a 22Ω resistor, and got a very similar result (though without the voltage spikes from switching the loudspeaker inductance):

The nearly constant difference between the input and the output (across the 22Ω resistor) indicates nearly constant current flow.

If we look at the voltage across the resistor, we can measure the current:

The current through the 22Ω resistor is about ±18mA, though it isn’t quite as constant as I had thought from the previous plot. (This plot is 200mV/division, so about 9.11mA/division.)

With a 3.9Ω feedback resistor, I again see waveforms consistent with a ±18.5mA current limit (dropping gradually to ±15mA before it switches to the other phase). I can get the same triangle wave at the input if I use a wire for the feedback, with the output only a few mV different from the input (for a wire resistance equivalent to about 130mΩ). The period is about the same also, indicating that the current is what is determining the charging time of the capacitor, not the feedback resistor.

With a 200Ω feedback resistor, I see a little current limitation just as the output switches, but it becomes voltage-limited soon after. The period increases, as we would expect when the voltage limitation reduces the current.

So the loudspeaker in the feedback loop was not very interesting—it was behaving much like any other low-impedance feedback. At least, that is what is happening with low frequencies, where the loudspeaker is reasonably modeled as an 8Ω resistor.

With a 10nF capacitor and a 22Ω feedback resistor the oscillator oscillates at 1.27MHz, but with the loudspeaker only at 589kHz, so the loudspeaker is behaving more like a large impedance at these frequencies.  The output with the 22Ω resistor is a triangle wave, but with the loudspeaker is a square wave (with a little high frequency ringing).  I can’t see these frequencies well on the Bitscope oscilloscope, so I had to switch to my analog Kikusui COS5060 60MHz oscilloscope.  With 47Ω and no oscilloscope load, I get a frequency around 965kHz, with 22Ω around 1.13MHz, with 81Ω around 768kHz,  with 100Ω 687kHz, with 120Ω around 613kHz, with 150Ω around 526kHz, and with the loudspeaker around 590kHz.  So the loudspeaker is behaving like a 125–130Ω impedance around 590kHz, which is consistent with the measurements made of the loudspeaker impedance around that frequency. Similarly with a 3.3nF capacitor, I get 150Ω 1.23MHz, 180Ω 1.1MHz, 200Ω 1.03MHz, 220Ω 972 kHz, and loudspeaker 987kHz.  The output waveforms with the 220Ω or loudspeaker as feedback are fairly good square waves, so the oscillator feedback is voltage limited, not current limited.

So it looks like the simple analysis of the oscillator as being either a current-limited or a voltage limited output suffices for modeling the relaxation oscillator—the phase changes don’t seem to matter much, just how fast the capacitor charges or discharges, which depends mainly on the magnitude of the impedance.

Incidentally, the fluctuations in frequency for the oscillator occur with the resistors and the loudspeaker alike, and so don’t seem to be due to any physical or electrical properties of the loudspeaker.  They also occur with both electrolytic and ceramic capacitors.

Next Page »