Gas station without pumps

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)
{
    rled.pulsewidth_us(PWM_PERIOD-R);
    gled.pulsewidth_us(PWM_PERIOD-G);
    bled.pulsewidth_us(PWM_PERIOD-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) 
    {
       edges_counted++;
       PCR_PORT_TO_USE |= PORT_PCR_ISF_MASK;
    }
}

// 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
    edges_counted=0;
    PCR_PORT_TO_USE |= PORT_PCR_ISF_MASK | PORT_PCR_IRQC(11);  // clear interrupt for PTD3, and enable interrupt on either edge
    wait_us(COUNTING_TIME);
    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() 
{   
    rled.period_us(PWM_PERIOD);
    gled.period_us(PWM_PERIOD);
    bled.period_us(PWM_PERIOD);
    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
    
    __enable_irq();

    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
        set_RGB_color(255,0,0);
        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);
    while(1) 
    {   uint32_t freq=frequency();
        printf("%lu Hz\n",freq);  
        if (freq < low_freq)
        {   // low_fequency found, turn LED green
            set_RGB_color(0,255,0);
        }
        else if (freq >= high_freq)
        {   // high frequency found, turn LED blue again
            set_RGB_color(0,0,255);
        }    
    }
}

2013 March 23

Triangle-wave oscillator

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

One of the student suggestions from the “bar exam” was to design a protoboard for the class D amplifier, so that they could keep it as a permanent object (a more useful one than the pressure-sensor amplifier, since they don’t have any strain gauges).

I’ve been thinking about that and see a few problems:

  • The block diagram the students came up with this fall (with my guidance) calls for 3 power supplies, which makes the amplifier a bit impractical to power from a battery or wall-wart power supply.
  • The students used an external triangle-wave generator.
  • The protoboard would have to be fairly large to accommodate the FETs and inductor and enough wiring space, which would probably result in a larger board than 5cm × 5cm, and so $2/board rather than $1/board.

I’ve not given much thought yet to the power-supply problem, but I did think a little about the triangle-wave generator.  Since they only need to generate triangle waves at a single frequency (in the 60kHZ–100kHz range), it should be possible to use a hysteresis oscillator with an integrator rather than a simple RC timing circuit.  Something like the following might work:

triangle-wave-oscillator

The slew rate of the MCP6004 op amps is only 0.6 V/µs, but the triangle wave only swings from VIL to VIH, or about 1V, so the minimum period is about 3.3µs, for a maximum frequency of 300kHz. If we use another op amp to amplify the signal to make it swing almost 5v, instead of 1V, the maximum frequency would be only 60kHz.

I might want to try this circuit out this week, to see if it is worth having the students play with next year.

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 WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 251 other followers

%d bloggers like this: