Gas station without pumps

2014 April 15

Hysteresis lab too long

After re-reading my notes on last year’s hysteresis lab, I realized that my schedule for this week in the Revised plan for circuits labs, with both the hysteresis lab and the sampling lab in the same week was too ambitious. There was a chance that the students could do the hysteresis lab in 3 hours, but only if they already understood everything in the pre-lab assignment and worked efficiently. A lot of the students, however, only learn by repeatedly bumping into a brick wall, and don’t really have any notion of solving general problems before they encounter them in the lab, so I expected a lot of students to waste time today doing the pre-lab assignment in lab.  My expectation there was amply fulfilled.

I decided to cancel (or at least postpone) the sampling and aliasing lab, and spend both Tuesday and Thursday on the hysteresis lab. I don’t think we’ll be able to double up the labs next week, but the week after may be a little thinner, and we may be able to squeeze in the sampling and aliasing then.

Everyone got the two input thresholds for the 74HC14N Schmitt trigger (with 3.3v inputs) measured, and they all got essentially the same values.  Some of them took a long time getting there, because I did not hand them a test circuit, but asked them to come up with one themselves. One group used an adjustable bench power supply for Vin, but the rest (eventually) came up with using a potentiometer as a voltage divider and recording the input and output with the PteroDAQ software. For some, I had to do more guidance than I really liked, getting them to decompose the problem into having the Schmitt trigger as one component with a variable input and the pot as another component with a variable output. Since they had done a very similar setup for the mic lab last week, and I had explained the pot as a variable voltage divider at that time, I had expected them to instantly see how to apply it, but most did not. Still, everyone eventually got it, and I think that the ones who struggled the most now have a much solider understanding of voltage dividers and potentiometers than if I had just given them a circuit to copy.

I did get to show the PteroDAQ users a useful feature of the program—by connecting the output to PTD4 (or one of the other digital pins of port A or port D), PteroDAQ can be set to trigger whenever the output changes values.  A few sweeps of the pot past the threshold values reveals quite repeatable voltages at which the transition occurs, without having to page through a long trace of uninteresting info.

The groups then struggled with coming up with the right RC time constant for their oscillators. I’m probably going go over the calculation in class tomorrow, since I think everyone got a reasonable result, but not everyone was clear enough about their method to write it up well. I want to see clear explanations in the lab report, so I’ll go over it to help them smooth out the bumps in their explanations.

Some other things I want to do tomorrow:

  • Talk about Carol Dweck’s work on mindset, as one of the students frequently wonders aloud whether the class is too difficult for her, and some of the other students may be thinking that they “don’t have the ability”. So far as I can tell, everyone in the class has the ability to master all the material in the class—but I need to get them out of “fixed mindset” into “growth mindset” and recognize that they can do more than they credit themselves with, if they are willing to work for it.
  • Have them go over their computations of the finger-touch capacitive sensor and compare answers with each other. I want to make sure that they express their answers in standard units (like pF) and that they are careful about units (mixing mils, cm, and F/m probably confused a lot of students).
    During the lab time, I had each group come up to use my micrometer to measure a double-thickness of packing tape. I must be using a different roll of tape than in previous years, because we consistently got about 1.7mil (0.043mm) with my Imperial units micrometer (that is we measured 3.4–3.5 mil for the double thickness), while last year I had 2.2mil.  I should probably get a metric one, but I may be too cheap to spend $14 on a tool I use once a year in this class. Besides, this gave me an opportunity to tell students the difference between mil and mm, which most of them did not know. Since a lot of materials still come with thickness specifications in mil, they should at least be aware of the existence of the unit and the potential for confusion. (Several had done the prelab homework assuming 2.2mm, which would be very thick packing tape.)
  • Assign one of the voltage-divider do-now problems from last year. Perhaps this one?
    • What is the output voltage for a 3-resistor voltage divider? (I’ll draw the circuit)
    • You have sensor whose resistance varies from 1kΩ to 4kΩ with the property it measures and a 5v power supply.  Design a circuit whose output voltage varies from 1v (at 1kΩ) to 2v (at 4kΩ).

Two or three of the groups managed to get their relaxation oscillators to oscillate and measured the frequency on the digital scopes. One group got as far as adjusting the R and C values to get the frequency within the spec given in the homework (10kHz to 100kHz), and started the next step (making the capacitance touch sensor out of aluminum foil and packing tape). Lab on Thursday will consist of everyone getting the oscillators working in spec, testing the change in frequency for a finger touch (which may need some capacitor changes, as I think some are using a small R and large C, which won’t have enough frequency change with the small capacitance of a finger touch), testing the oscillator with the KL25Z boards (with my new code), and soldering up the circuits on PC boards.

Students are beginning to get the  message that when they ask me whether some result is right, my answer will be what my father taught me: “Try it and see!” When they ask me for help using the equipment or debugging when they get too frustrated, I’m more helpful, but I’m not going to check their work for them when the real world can do that so much better.  Besides, the simple models we are using are not all that accurate—even if they do a perfect job of the computation, the real-world behavior will be enough different that they’ll need to tweak the component values anyway. This is another lesson I want them to get—the real world is not as simple as the spherical-cow models used in physics classes and intro EE, but the spherical-cow models are nonetheless useful.





2014 April 14

Hysteresis lecture

Filed under: Circuits course — gasstationwithoutpumps @ 23:16
Tags: , , ,

Today’s class started with feedback on their second design reports (the electret mic lab). Everyone in the class got a “redo” on this assignment.  Some of them actually had pretty good write-ups, but I had warned them that errors on schematics or with units would trigger automatic redos, and every report had at least one serious error (like 200A, instead of 200µA, or short-circuiting the mic). I’m going to hold them to getting their schematics and units right—details matter in engineering, and they have got to develop a habit of double-checking what they write.

After a little more feedback (on how to improve their plots, for example, and little details like capitalizing “Figure 1″ or using the prepositions with voltage and current), I switched to new material on hysteresis that they’ll need for tomorrow’s lab.  I actually gave them a fairly detailed description of hysteresis in the lab handout (I wonder if anyone has read it yet?), but I covered it again anyway. I also talked about DIP vs. SMD parts (the 74HC14N chip they’ll use is in a DIP), and introduced them to a simple relaxation oscillator.  We worked through how it functioned to produce a triangle wave on the input and square wave on the output, but I did not mention the capacitive coupling from the output to the input that changes the triangle wave rather dramatically when the capacitor in the RC circuit is small.

Input and output of a Schmitt-trigger relaxation oscillator (approx 67kHz). Note that the large output step is capacitively coupled to the input, causing a small step in addition to the expected triangle wave.

Input (yellow) and output (green) of a Schmitt-trigger relaxation oscillator (approx 67kHz). Note that the large output step is capacitively coupled to the input, causing a small step in addition to the expected triangle wave.  Note, the two traces are separate sweeps and the frequency modulation by 60Hz noise is big enough that the periods are not exactly the same on the two sweeps.  (click to embiggen)

The funny step in the input is not visible if large capacitors are used, but accounts for a big part of the charge transfer for small capacitors (throwing off the RC calculations that determine period).

With a 680kΩ resistor and a 10pF capacitor, attaching a BitScope probe to the input changes the period from about 4.5µs to about 14µs. With the same resistor and a 30pF capacitor, attaching the probe changes the period from 17.5µs to 28.5µs—the change due to the input impedance of the scope makes a big difference in the behavior of the circuit. I’ll have to make sure that the students observe the effect that a scope probe has on their circuit—they’re probably still thinking of the measurements as being non-disruptive.  (They may get even bigger changes in period with standard oscilloscope probes—with the 30pF capacitor I get periods of 220µs for a 1× probe and 30µs with a 10× probe on my Kikusui oscilloscope—the BitScope input is similar to the 10× probe.)

Last year’s hysteresis oscillator lab ran quite long, but I’m hoping for better time tomorrow. I went through the behavior of the oscillator a bit more thoroughly, and I think I impressed on them the importance of doing the algebra and calculations before lab time. I also suggested how they could find the input threshold voltages using PteroDAQ at home (triggering on both rising and falling edges).

2014 April 5

Hysteresis lab on KL25Z

Relaxation oscillator used in the hysteresis lab.  The "variable capacitor" in this schematic is a person's finger and a touch plate made from aluminum foil and packing tape.

Relaxation oscillator used in the hysteresis lab. The “variable capacitor” in this schematic is a person’s finger and a touch plate made from aluminum foil and packing tape.

I spent today writing code for the KL25Z board to act as a period or frequency detector for the hysteresis lab, where they build a relaxation oscillator using a 74HC14N Schmitt trigger inverter and use it to make a capacitance touch sensor (pictures of last year’s setup in Weekend work). I had written code for the Arduino boards last year, and I started by trying to do the same thing on the KL25Z, using the MBED online development system.  The Arduino code used “PulseIn()” to measure pulse duration, and the MBED system does not have an equivalent function.  I could have implemented PulseIn() with a couple of busy waits and a microsecond-resolution timer, but I decided to try using “InterruptIn” to get interrupts on each rising edge instead.

The basic idea of last year’s code (and the first couple versions I wrote today) was to determine the pulse duration or period when the board is reset, finding the maximum over a few hundred cycles, and using that as a set point to create two thresholds for switching an LED on or off. I got the code working, but I was not happy with it as a tool for the students to use.

The biggest problem is that the touch plate couples in 60Hz noise from the user’s finger, so the oscillator output signal is frequency modulated.  This frequency modulation can be large compared with the change in frequency from touching or not touching the plate (depending on how big C1 is), so setting the resistor and capacitor values for the oscillator got rather tricky, and the results were unreliable.

I then changed from reading instantaneous period to measuring frequency by counting edges in a 1/60th-second window.  That way the 60Hz frequency modulation of the oscillator gets averaged out, and we can get a fairly stable frequency reading.  The elimination of the 60Hz noise allows me to use less hysteresis in the on/off decision for the LED, making the touch sensor more sensitive without getting flicker on transitions. The code worked fairly well, but I was not happy with the maximum frequency that it could handle—the touch sensor gets more sensitive if C1 is small, which tends to result in high frequency oscillations. The problem with the code was that MBED’s InterruptIn implementation seems to have a lot of overhead, and the code missed the edge interrupts if they came more often than about every 12µsec.  Because I was interrupting on both rising and falling edges, the effective maximum frequency was about 40kHz, which was much lower than I wanted.

To fix the frequency limitation, I replaced MBED’s InterruptIn with my own interrupt service routine for PortD (I was using pin PTD4 as the interrupt input). With this change, I could go to about 800kHz (1.6e6 interrupts per second), which is plenty for this lab.  If I wanted to go to higher frequencies, I’d look at only rising edges, rather than rising+falling edges, to get another factor of two at the high end.  I didn’t make that change, because doing so would reduce the resolution of the frequency measurement at the low end, and I didn’t think that the tradeoff was worth it here.

The code is now robust to fairly large variations in the oscillator design.  It needs a 20% drop in frequency to turn on the green LED, but the initial frequency can be anywhere in the range 400Hz–800kHz.

To make it easier for students to debug their circuits, I took advantage of having an RGB LED on the board to indicate the state of the program: on reset, the LED is yellow, turning blue once a proper oscillator input has been detected, or red if the oscillator frequency is not in range. When the frequency drops sufficiently, the LED turns from blue to green, turning back to blue when the frequency goes up again.

For even more debugging help, I output the frequency that the board sees through the USB serial connection every 1/60th second, so that a program like the Arduino serial monitor can be used to see how much the frequency is changing.  I took advantage of that feature to make a plot of the frequency as the touch sensor was touched.

Plot of frequency of hysteresis oscillator, as the touch pad is touched three times.  Note that the thresholds are very conservatively set relative to the noise, but that the sensitivity is still much higher than needed to detect the finger touches.

Plot of frequency of hysteresis oscillator, as the touch pad is touched three times. Note that the thresholds are very conservatively set relative to the noise, but that the sensitivity is still much higher than needed to detect the finger touches.

Overall, I think that the code for the KL25Z is better than what I wrote last year for the Arduino—now I have to rewrite the lab handout to match! I actually need to update two lab handouts this weekend, since week 3 will have both the hysteresis lab and the sampling and aliasing lab. Unfortunately, the features needed for those labs (trigger on rising and falling edges and downsampling) are not working in PteroDAQ yet.

Here is the code that I wrote for the frequency detector:

// freq_detector_own_isr
// Kevin Karplus 
// 2014 Apr 5

// This program is intended to be used as a "capacitive touch sensor" 
// with an external relaxation oscillator whose frequency
// varies with the capacitance of a touch.

// The program expects a periodic square wave on pin PTD4 with a frequency between 
// about 400Hz and 800kHz. (LOW_FREQ_LIMIT and HIGH_FREQ_LIMIT).
// On reset, it displays a yellow light, then measures the frequency to store as the "off" frequency.
// If the frequency is out of range (say for a disconnected input), then the light is set to red, 
//     and the off frequency checked again.
// Otherwise the LED is turned blue.
// After initialization, if the program detects a frequency 20% less than the initial freq, 
// it turns the light green, 
// turning it blue again when the the frequency increases to 90% of the original frequency.
// No floating-point is used, just integer arithmetic.
// Frequency measurements are made by counting the number of rising and falling edges
// in one cycle of the mains frequency (1/60 sec), giving somewhat poor resolution at lower 
// frequencies.  
// The counting time is chosen to that frequency modulation by the mains voltages is averaged out.
// This version of the code uses my own setup for the interrupt service routine, because InterruptIn has
// too much overhead.  I can go to over 800kHz (1.6e6 interrupts/second) with this setup, 
// but only about 40kHz (80e3) interrupts/sec with mbed's InterruptIn.

#include "mbed.h"

#define PCR_PORT_TO_USE (PORTD->PCR[4])   // pin PTD3 is the pin to use

#define MAINS_FREQ (60)     // frequency of electrical mains in Hz
#define COUNTING_TIME (1000000/MAINS_FREQ)   // duration in usec of one period of electrical mains

// off_frequency must be between LOW_FREQ_LIMIT and HIGH_FREQ_LIMIT for program to accept it
#define LOW_FREQ_LIMIT (400)
#define HIGH_FREQ_LIMIT (800000)

// on-board RGB LED
PwmOut rled(LED_RED);
PwmOut gled(LED_GREEN);
PwmOut bled(LED_BLUE);
#define PWM_PERIOD (255)  // for the on-board LEDs in microseconds

// Set the RGB led color to R,G,B  with 0 being off and PWM_PERIOD being full-on
void set_RGB_color(uint8_t R, uint8_t G, uint8_t B)

// InterruptIn square_in(PTD4);
volatile uint32_t edges_counted;

uint32_t low_freq_threshold, high_freq_threshold;  // thresholds for detecting frequency changes

extern "C"{
// interrupt routine that counts edges into edges_counted
    void PORTD_IRQHandler(void) 

// return the frequency for the square_in input in Hz
uint32_t frequency(void)
    PCR_PORT_TO_USE &= ~PORT_PCR_IRQC_MASK;  // disable interrupts on pin PTD3
    PCR_PORT_TO_USE |= PORT_PCR_ISF_MASK | PORT_PCR_IRQC(11);  // clear interrupt for PTD3, and enable interrupt on either edge
    PCR_PORT_TO_USE &= ~PORT_PCR_IRQC_MASK;  // disable interrupts on pin PTD3
    uint32_t freq=edges_counted*MAINS_FREQ/2; 
    return freq; 

int main() 
    set_RGB_color(255,255,0);   // set light to yellow
    SIM->SCGC5 |= SIM_SCGC5_PORTD_MASK; // make sure port D has clocks on
    PCR_PORT_TO_USE &= ~PORT_PCR_MUX_MASK;  // clearing the MUX field
    PCR_PORT_TO_USE |= PORT_PCR_MUX(1);     // Setting pin as GPIO
    FPTD->PDDR &= ~ (1<<4);  // make sure pin is input pin
    NVIC_EnableIRQ(PORTD_IRQn);            // enable interrupts for port D

    uint32_t off_frequency= frequency();
    while ( off_frequency<low_freq_limit ||="" off_frequency="">HIGH_FREQ_LIMIT)
    {   // timed out.  set color to red and keep trying
        printf("FREQ out of range: %luHz\n", off_frequency);
        off_frequency= frequency();
    uint32_t low_freq= 8*off_frequency/10;  // 80% of off_frequency
    uint32_t high_freq= 9*off_frequency/10;  // 90% of off_frequency
    printf("off= %luHz lo_thresh=%luHz hi_thresh=%luHz\n",off_frequency, low_freq, high_freq);
    {   uint32_t freq=frequency();
        printf("%lu Hz\n",freq);  
        if (freq < low_freq)
        {   // low_fequency found, turn LED green
        else if (freq >= high_freq)
        {   // high frequency found, turn LED blue again

2013 February 25

Tinkering lab reports show problems

Filed under: Circuits course — gasstationwithoutpumps @ 11:53
Tags: , , , ,

I thought that the tinkering lab (lab handout)had gone ok, because students had done a lot of experimenting and had all ended up with functional circuits.  But the lab reports that I graded this weekend indicated some serious problems:

  • A lot of the students made serious errors on their schematics.  Shorts, missing wires, and mislabeled parts were common. I’m making the students who had serious errors redo the report, and keep redoing it until they can produce correct schematics.  It wasn’t just the weakest students in the class making errors on the schematics—almost everyone was.
  • A lot of the students did not check their predictions of the behavior of the board when components were added between pairs of terminals.  If their initial predictions had been accurate, this would not have been a problem, but students with predictions that were way off didn’t check them.
  • There was no evidence that anyone improved their mental model of how the hysteresis oscillator worked as a result of either making the observations (which about half the class did) or thinking about the model more (which those who did not check their predictions could have done).
  • Some of the students appear to have poor technique for recording observations, as they tabulated observations that were not really consistent with how the boards behave.  Either they mis-recorded the conditions under which they did the test or they shorted together some nodes without noticing.  No one checked for consistency of the observations to see whether things that should have been nearly the same were nearly the same.

I’ll probably rant in class about wrong schematics ruining otherwise fine reports, and hope the message gets through that they have to check their work much more carefully.  They’ll really suffer on the labs that they have to solder up on the protoboards if they continue to work with such sloppy, useless schematics. I may rant about lab notebook technique also, since that will be even more important in biomolecular labs.

Next year, I’ll want to split this lab into 2, with one lab dedicated to collecting data, a class in between to analyze the data, and the next lab dedicated to doing the design.



2013 February 14

Tinkering lab

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

Today’s lab was a “tinkering lab”, using the oscillator board they made in the hysteresis lab as a component to make a light-controlled sound generator.  They did not have a specific goal, other than to use the oscillator board, to produce sound on the loud speaker, and to be able to control/change the sound by shadowing a phototransistor.

I had them try to predict the effect of adding resistors or capacitors between each pair of points on the oscillator board (+5v, GND, Cap, and OUT), and to test how many of their predictions were right.  A lot of students turned out to have such weak mental models of the oscillators that they couldn’t figure out what would happen in even the simple cases. Few of them were able to do the prelab exercise of predicting what would happen if the added components in various places. Most of them eventually remembered or figured out again how the oscillators worked, and so were able to modify them in reasonable ways, but a lot of lab time was wasted doing what was supposed to be prelab work.

A few of the students were trying to figure out what to do without even a schematic in front of them, and when they drew a schematic it did not correspond to what they told me they were trying to do.  They worry me a bit, as by this point of the quarter the students should be able to copy schematics accurately and wire from them, even if they are sometimes a bit hazy on how the circuits work.

I’m also finding that several of the students seem to be incapable of learning from written material—students with no understanding of FETs as switches seemed to understand it fairly well after I explained it—using almost the same words as I used in the lab handout.  I’m not sure I understand why the explanation given verbally seemed to work, while the same one given in writing seemed to fail.  I find it difficult to believe that they really can’t learn from written materials, so there must be a different explanation.  Too much in the handout, so that they shutdown while reading it?  Not having read the handout? (possible with some of the students, but unlikely with the ones I observed the problem in)

I rely heavily on written material to communicate both design goals and details about the parts they are using, so I really need to figure out what is going wrong with the written channel for information.  Is there a way I could write the lab handouts that would be more digestible for the students? If the difficulty absorbing information from written sources is a real one, I may have to do more lecturing, though that seems like the wrong thing to do, as engineers have to be able to read spec sheets and learn from written sources.

Only a few of the students did the systematic exploration of what components in different places and predicting/observing the effect.  I may need to reduce the emphasis on that next year, and focus their attention more on potentially useful interventions (one pair wasted a lot of time looking at the least interesting pair: connecting extra components between power and ground).

Everyone eventually managed to get loud sounds out of the loudspeaker, and to control the sounds by shadowing the phototransistor, but I think that some just copied what others had done, without completely understanding what they were doing.  I think that they all ended up with essentially the same design I had, with the phototransistor shorting out the capacitor in bright light.  Everyone did eventually get the right pFET driver for the loudspeaker, though one group worked through the other three circuits first, and found why they were less desirable. We’ll see in the lab reports whether all the groups can accurately draw their schematics and explain how their devices work.

Overall, I’m moderately pleased with how the lab went.  Students deepened their understanding of RC time constants and the hysteresis oscillator circuit, and they got a feel for “cut-and-try” engineering. But I think I need to rewrite this lab for next year, with a little more direction in which pairs of terminals are worth exploring and how to think about the effect on the oscillator.

Next Page »

The Rubric Theme. Blog at


Get every new post delivered to your Inbox.

Join 309 other followers

%d bloggers like this: