# Gas station without pumps

## 2015 June 3

### Crude theremin

On Monday I again had 2 lectures: one in Banana Slug Genomics on the Burrows-Wheeler Transform and FM index, and one in applied electronics finishing up the EKG preparation and starting into optional material.

The BWT and FM-index lecture was a bit rough though I had prepped on the material for a fair amount of time over the weekend.  I think that the written presentations I’d used to prep from (which included the Li and Durbin paper on BWA) were better than my lecture, so I ended up pointing the students to them as well, hoping that my lecture would at least make them more willing to click through and read the paper.

In the electronics class, I provided some feedback on the lab reports (reminding students once again how to figure out the DC bias resistor for an electret microphone).  Over the weekend I had finally caught up on the grading, so of course Monday saw a huge stack of redone work being turned in—I did the prelab grading Monday night, but I did not have the heart to start on the redone work last night or tonight.

After the feedback, and questions from the class on the EKG lab, I gave the EKG demo (which had worked fine before cycling up the hill).  Needless to say, it did not work when I hooked everything up in front of a live audience.  But I turned this into a teachable moment by explaining what I thought the problem was (bad electrode connections, because touching the reference wire produced a change in the output), and getting out three new electrodes, adding a a little electrode gel, sticking them on next to the ones I was already wearing, and moving the clips over.  Luckily, that was the problem, so I was able to demonstrate debugging, even with no lab equipment.

The EKG demo concluded the required material for the course, and so I asked students for suggestions on what I should cover next.  I suggested the electronics of the nanopore and nanopipette labs, and asked them for other ideas. One student wanted me to explain how theremins work, and a couple of others agreed that would be an interesting topic.  So I finished Monday’s lecture on the nanopore electronics (basically telling them it was just a transimpedance amplifier, but engineered for high gain and very low noise, and talking a bit about the copper boxes around the stations to reduce capacitive coupling), and told them I’d try to talk about theremins on Wednesday.

Tuesday’s lab went fairly smoothly, but I was kept busy helping students debug their designs—most often the problem was miswiring, but some students had electrodes that weren’t making good contact.  I had on a set of working electrodes, so that we could swap the leads to my electrodes to see if the problem was the electrodes or their boards.  A lot of the students had a lot more 60Hz noise pick up (even with my electrodes) than I was getting, and I could not always determine why. In some cases it was simply long wires on the breadboard.  They’ll be soldering up their EKGs on Thursday, and the wiring should be much more compact there.

Tuesday night I read up on a bunch of analog circuits relevant to Theremin design, and in today’s class I presented

• a block diagram for a theremin (two oscillators—one with an antenna, a mixer, and a low-pass filter to pass only the difference frequency)
• I showed them a rather crude theremin that they could implement from what they already knew: two relaxation oscillators using Schmitt triggers, an XOR gate, and an RC low-pass filter. (Only the XOR gate was new to them.)
• I then talked about mixers, presenting a ring modulator (which they probably didn’t understand, as we’ve not done transformers except in passing), a simple two-resistors plus a diode non-linear mixer, and a synchronous decoder using a switch to change an op amp gain from +1 to –1.  I mentioned a couple of Wikipedia articles they could look up for other mixer designs (Gilbert Cell and frequency mixer).
• I then introduced the notion of a sine-wave oscillator as a loop with gain exactly 1 and a phase change of 2πn for some integer n.
• The first oscillator I showed them was a Wien oscillator using an op amp, for which I derived that at $\omega RC = 1$ the phase change of the positive feedback was 0 and the gain was 1/3, so we could set the positive gain to 3 to satisfy the requirements.  I mentioned the first Hewlett-Packard oscillator, and the trick of using an incandescent light bulb to control the gain and avoid clipping.  I showed them a plot of the amplitude and phase change for the feedback circuit, using gnuplot functions that they are familiar with from their own modeling.
• Next I showed them an LC tank circuit, and derived the infinite impedance resonance $\omega^2 LC = 1$.
• I then got to a Colpitts oscillator, showing them a possible circuit using an op amp, and how to model it as cascaded voltage dividers using gnuplot.  I showed them that the phase change of the feedback was 180° and that the phase change moved away from 180° very fast.  I was running out of time, so I briefly mentioned the Hartley oscillator and the Clapp oscillator, but didn’t really go into them.

On Friday, I plan to cover one-transistor amplifiers (common emitter with emitter degeneration and common collector—I probably won’t do common base), as those seem to be popular in theremin designs.

Tonight, since I was not willing to face grading, I decided to try out the crude relaxation oscillator Theremin.  Here is the circuit I ended up with (not including the bypass capacitors):

I used the potentiometer on the second oscillator to do crude pitch matching for the two oscillators, just looking at the period on an oscilloscope. The emitter-follower amplifier was used to get enough current to drive the loudspeaker.

The crude theremin only sort-of works.  I could get high-pitched theremin-like noises from it, but when the difference frequency dropped to 500Hz, the two oscillators phase-locked and the speaker went silent (often with very irritating on-off stuttering). The phase lock occurred even though the oscillators were on different chips at opposite ends of the breadboard, and I had 10µF ceramic bypass capacitors on each Schmitt trigger package and a 470µF electrolytic on the middle of the power bus.  Everything was powered to 5V with the USB power from my MacBook Pro.

I also often got 60Hz modulation of the signal, often as a on/off square wave, when the frequency difference was close to the point where the oscillators phase locked.

I’m not sure how the oscillators are getting coupled so that they phase lock when the difference frequency is small, so I’m not sure how to fix the problem.  The 60Hz sensitivity is almost inherent in the relaxation oscillator design, since the thresholds for the hysteresis on the Schmitt trigger remain fixed, but the hand near the antenna couples in 60Hz interference.  I suppose that over the summer I should try building a theremin with Colpitts oscillators or Clapp oscillators, to see if that works any better.

## 2014 June 6

### Wrapping up Applied Circuits and PteroDAQ bug fix

Today was the last day of the Applied Circuits class, and the students were turning in their 10th lab report.  It’s all over but the grading (though I expect a few more redone assignments to be turned in on Monday).

Today’s class started with my explaining a bug that was just found in the PteroDAQ code.

Symptoms: On the EKG assignment a number of students ran into trouble in the lab with the PteroDAQ software suddenly switching from normal recording to a sawtooth waveform, then spontaneously switching back after a while.  When plotting the saved data with gnuplot, the timestamps were all messed up in the part of the recording where the sparkline had displayed a sawtooth waveform, but returned to normal when the sparkline did.  We had never seen this behavior on  the Macs, nor had we seen it previously on the Windows machines.  The one new thing in this lab is that we were using a higher sampling rate on the Windows machines than we had previously used.

Diagnosis 1: The sawtooth waveform suggested to me that the low-order part of the timestamp was being taken as the signal, and the disordered timestamps as intrusion of the data into the timestamp.  I suspected that the old, slow Windows machines were not able to keep up with the USB serial traffic, and that they were losing a byte and getting a framing error.

I suggested this diagnosis to my son, but he showed me the framing character and checksum used to detect malformed packets, and pointed out that even if a malformed packed passed the checksum, synchronization would be re-established within a packet or two. Although the behavior certainly looks like a framing error, the packets should not go out of frame consistently.

Today, after watching the Shakespeare To Go 50-minute production of Hamlet with me, my son accompanied me to the lab to observe the behavior for himself.  We got the behavior very quickly with a 300Hz sampling rate, and the error message for a checksum failure was not printed. He added a print statement in the code where the data packets were getting their checksums checked, so that we could see what data was coming in. We looked at the transition from good behavior to bad, and noted that the packets were still the right length, but the information in the packet seemed to have been shifted. So there seemed to be a framing error, but not in the USB communication.

Diagnosis 2: This shift indicated that the problem was in the software running on the KL25Z board, rather than in the software running on the Windows host.  The packets to be sent out the USB serial port are stored in a queue in the KL25Z, and I conjectured that the queue was overflowing.  Since a circular buffer is used for the queue, writing when the queue is full would overwrite data for a different packet.  Because the queue length in bytes was not a multiple of the size of the items on the queue, this overwriting would be out of phase, and we would observe periodic switching between in-frame and out-of-frame packets, which is what we were observing.

We checked the code, and found that the test for sufficient room in the buffer for another packet had not been included in the routine for adding a new event to the queue.  The code to check the remaining space had been written, but not not used yet.  A fifteen-minute bug fix, while I went to my office to get my materials for my class, confirmed that this had indeed been the problem.  He will be pushing the fix to https://bitbucket.org/abe_k/pterodaq this evening.  The fix consists of discarding any event for which there is insufficient room in the queue.  Some loss of data is unavoidable when the queue fills up, and this delays the loss as much as possible, as well as minimizing how many events are discarded.  Since the events are timestamped, losing an event will often have little or no consequence for downstream analysis or plotting.

The first part of the class was explaining the bug to the students, which I had to do in a rather handwavy fashion, since most of the class has not had programming, much less queues and circular buffers.

We then talked about a variety of different things: deadlines for redos, when the t-shirts would be available, instructor feedback forms to fill out online, differences between bioengineering programs at the various UCs, what the job market was like for bioengineers, and other general advising sorts of things. I also reviewed some of the goals of the course in terms of engineering thinking, tinkering vs. engineering by design, the usefulness of models that aren’t perfectly accurate, trying to switch them from answer-getting to keeping in mind the meaning of what they were doing, sanity checks, methodical work and lab notebooks records, and that the real world trumps any models (“try it and see!”).

Incidentally, this year’s t-shirts have that motto on the shirts:

2014 Applied Circuits t-shirt has the design only on the front, and has the class slogan above the cyberslug. (Click for high resolution image)

When the students finally ran out of questions, I had about 10 minutes left, so I introduced them to the Wien-bridge oscillator, first by reminding them of the bridge-null condition (which them almost remembered from the quiz it had been on), then deriving that $\omega RC =1$ and $R_{2} = 2 R_{1}$. I even had time to tell them about how the amplitude of the original oscillators was regulated by having a light bulb as a non-linear resistor in place of R1.

After class I attempted to help one of the students get PteroDAQ working with his laptop, but we got as far as determining that the drivers were missing for the USB serial device on the KL25Z board, and that the Windows 7 driver installation did not fix the problem before he had to go to another class. My son and I will have to look for Windows 8 driver initialization that will set things up properly for the KL25Z board—this might be tricky as we have no access to a Windows 8 machine to test putative solutions on.

## 2013 March 16

### Twenty-eighth day of circuits class

Filed under: Circuits course — gasstationwithoutpumps @ 12:29
Tags: , , ,

Yesterday’s lecture was a mish-mash of odds and ends that we hadn’t covered well previously, and questions that had come up.

I started with talking about where the EKG signal comes from.  The guest lecturer on Wednesday had done a good job of covering action potentials, but I pointed out that we were not sticking electrodes into their heart muscle cells: we had access to only the outside of the cells.  So where was the differential signal we were measuring coming from?  This had been one of my first puzzles in trying to understand how an EKG works, and they were mostly just as clueless about it as I had been.  The explanation that I settled on was looking at each of the cells as a little capacitor with a battery that charged it and a switch that discharged it, with one side accessible to us.  There is a resistance from each cell to each of the differential electrodes, and from the differential electrodes to the body reference electrode, so that the measurement electrodes act like they are in a voltage divider between the cell and the reference electrode.  The voltage difference between the differential electrodes depends on the difference in resistance from the cell to the electrodes, and the signal we see depends on the change in position of the discharge wave.  If everything depolarized at the same time, we’d see almost no difference voltage, but as the wave sweeps from left-to-right (or right-to left) we see difference voltages.    It’s not a perfect explanation of where the EKG signal comes from, but it is better than leaving them thinking that they are seeing the action potential directly.

After that a question came up about the 60Hz noise in the EKG signal, and where it came from.  I talked about the loop formed by the LA and RA wires and the body between them as an electromagnetic pickup, and how we could reduce the electromagnetic pickup by twisting the wires together more to reduce the area of the loop.  I also discussed capacitive coupling of 60Hz into the wires, reminding them of the capacitive touch sensor they had made earlier in the quarter.  We talked a bit about shielding cables and Faraday cages.  While on the subject of noise, I also mentioned the problem of microphonics in the nanopore equipment.  We have not discussed thermal noise or other problems of designing for very small signals.

Students asked where the 60Hz hum in their class-D power amplifiers came from, and I talked about ground loops and noise pickup in their power lines.  The op amps they were using had excellent power-supply noise rejection, but the voltage reference they were using (a pair of resistors as a voltage divider and a unity-gain buffer) provides an excellent path for noise in the power supply to be coupled into the amplifier.  I talked about two ways to reduce the hum: using twisted cables for the DC power, to reduce the electromagnetic pickup of 60Hz noise, and using a Zener diode reference instead of a simple voltage divider for the Vref signal.  I’m wondering whether I should add Zener diodes to the parts kit next year, or even whether I should get an adjustable voltage reference like the TL431ILP.  Either one is about 20¢ each in the small quantities that we would need.  The Zener diode is easier to explain, but the adjustable voltage reference is more versatile (the voltage is set by 2 external resistors as a voltage divider with the output of the voltage divider held at 2.5V) and has less variation in voltage with current (output impedance of 0.2Ω instead of 5–30Ω).  One minor problem with the TL431LP is that the lowest reference voltage it provides is 2.5V (using a wire from the “cathode” to the reference feedback input).  Since we are doing everything with power supplies of 5V or more, this shouldn’t be a problem for us.

After talking about noise, a question came up about the fat wires used for loudspeakers in stereo systems and whether they were shielded.  I managed to get the class to come up with the correct explanation: that the wires are fat to reduce resistance and avoid I2R power losses.  Currents to loudspeakers tend to be pretty big, since the resistance of the speaker itself is typically only 8Ω.  We talked about microphone cables being shielded (nowadays, I think that they are mostly twisted pairs inside a foil or metalized plastic shield, rather than coaxial cable) but that the tiny voltages and currents that speaker wires would pick up not mattering, since the signals were not amplified.  I also mentioned that solar panels were generally wired with fat wires also, to reduce the I2R power losses, since the voltages of the solar panels were fairly low (12V or 24V).

The students did not come up with any questions, so I pulled out one that had been asked weeks ago in lab: how sine-wave oscillators work.  The students had build square-wave oscillators with Schmitt triggers, and had used function generators, but had not seen any sine-wave oscillators.  I decided to do a classic oscillator: the Wien-bridge oscillator, since it uses the building blocks and concepts they are familiar with: a differential amplifier and two voltage dividers as a bridge circuit.  I started out with a generic bridge (just an impedance on each arm) and we got 3 formulas relating the nodes of the amplifier: $V_{out} = A(V_p - V_m)$, $V_m= V_{out} Z_1/(Z_1+Z_2)$, and $V_p = V_{out} Z_3/(Z_3+Z_4)$, from which we derived the stability condition $Z_1 Z_4 = Z_2 Z_3$.  This was all review for them, as they had had bridge nulling on a quiz.

The Wien bridge oscillator circuit. I initially gave it with just a resistor, not a light bulb, for R1, since the analysis is easier that way. The neat thing about the light bulb is that it provides an automatic gain control to set its resistance to  R2/2.

I then gave them the circuits for each arm of the bridge (just resistors on the negative feedback divider, and a series RC and a parallel RC for the arms of the positive feedback divider).  Rather than do complex impedance calculations, we just did Bode plots of the impedance of each of the RC arms, from which we could see that the voltage divider had zero output at DC and ∞ frequency, with a maximum at $1/(2\pi RC)$.  We were running out of time, so I did not derive with them that the gain of the positive voltage divider was 1/2 at that frequency, but jumped immediately to describing the use of an incandescent bulb in the negative feedback circuit to provide automatic gain adjustment (though I just waved my hands at it, not really showing how the thermal feedback mechanism worked).  I also managed to mention the historical importance of this oscillator design as the first product of Hewlett-Packard, and the start of “Silicon Valley”.

The range over which the automatic gain control with the light bulb works is determined by the range of resistance for the bulb filament. When the bulb is cold, its resistance must be less than R2/2. When the output is a sine wave with amplitude equal to the power supply, the resistance of the bulb filament must be larger than R2/2.  When the circuit is stable, the RMS voltage on the  bulb will be 1/3 the RMS voltage of the output, and the bulb filament resistance will be R2/2.  Nowadays other non-linear components are used rather than bulbs for the gain control, since bulbs suffer from microphonics and (for low frequencies) insufficient low-pass filtering (they are relying on the thermal mass of the filament to provide the low-pass filter of the automatic gain control).

On Monday, I plan to answer other questions students have, if they can come up with anything that confused them over the quarter.  If they can’t come up with any questions for me and send them to me this weekend, then maybe I’ll have to come up with some questions for them as an impromptu quiz.