# Gas station without pumps

## 2016 July 11

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

I decided to see whether the TSI (touch sensing input) on the Teensy boards was good enough to use as a capacitance meter. My first attempt was to use the touchRead() interface from the Teensyduino environment.

The test code was very simple:

```#define LED_PIN  (13)
#define TOUCH_PIN (0)

void setup()
{
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
pinMode(TOUCH_PIN, INPUT);
Serial.begin(115200);
}

void loop()
{
if (Serial.available())
if (c=='1')
{   int sum_touch=0;
for(int i=100; i>0; i--)
digitalWrite(LED_PIN, HIGH);
delay(20);
digitalWrite(LED_PIN, LOW);
}
Serial.println(sum_touch);
}
}
}
```

I just read the pin with touchRead 100 times, adding the results. I measured several different capacitors to find out the range of readings and to fit a function to the data.

The way the touch sensing works is that there are two hysteresis oscillators in the microcontroller, one using a fixed reference capacitance and the other using the capacitance of a pin.  The feedback in each oscillator is not a resistor, but switches between a positive and a negative constant-current source. This should give the input to the Schmitt trigger a clean triangle wave.  There are 8 different constant-current sources to choose from, and different sources can be chosen for the two oscillators.  The amount of hysteresis can also be chosen.  The pin-controlled oscillator output is divided down by a pair of counters (one that can be set to 1, 2, 4, 8, …, 128, the other to  1, 2, 3, 4, … , 32), and the number of pulses of the reference oscillator is counted for one tick of the divided-down pin-controlled oscillator.  The touchRead() function fixes which current sources, which hysteresis voltage, and which counter settings are used, so that the reading is directly proportional to the period of the pin-controlled oscillator, which in turn is directly proportional to the capacitance.

Therefore, the readings should be linear with the capacitance, but there is likely to be some parasitic capacitance that needs to be added (indeed, I get a reading of around 551.7 when the Teensy board is plugged into a bread board, with no deliberate capacitance is added).  So the model I want to fit is $y= a (C + C_{0})$.

The simple linear fit measures each capacitor within about 10% of its nominal value, which may be as accurate as the capacitors are (these were cheap ceramic assortments from China, with no specs).

To get good fits at the low end, I fit the C0 parameter only for the data points at 0pF, 1pF, and 5pF, but fit the scaling parameter over the whole range. It is not visible on the plot, but each capacitor was measured multiple times, and the variation in measurement was less than ±0.3%.  Of course, this is for averages of 100 readings, so the raw readings may vary up to ±3%—I’ve not tested for variation.  Somewhat surprisingly, the % variation seemed to be larger at the high end than at the low end—I would have expected the high end to have less variation (because of reduced quantization error).

I checked the capacitance of a small aluminum foil and packing tape touch pad, like we use in the applied electronics course.  It was about 7.05pF when not touched, and 60.8pF when touched with moderate pressure.

Because touchRead() reports 65535 for any capacitance that is too large, the maximum measurable capacitance is 65534/55.3942 – 10.1921 pF = 1173pF. If I want to measure large capacitors, I’ll need to rewrite the touchRead() code to use the TSI functions of the microcontroller more directly. I’ve started work on that, but I’ll save it for a later post. I expect to be able to measure up to at least 1µF. That code will also include translation to standard capacitance units and calibration options.

## 1 Comment »

1. […] earlier post, Capacitance meter using touchRead(), showed the beginnings of a capacitance meter, using the touchRead() routine in the Teensyduino […]

Pingback by Autoranging capacitance meter using TeensyLC | Gas station without pumps — 2016 July 13 @ 09:59

This site uses Akismet to reduce spam. Learn how your comment data is processed.