Capmeter - Kapazitätsmessgerät mit dem AVR

Page content

Kapazitätsmessgeräte (“C-Meter”) kann man kaufen. Man kann sich aber auch eines mit einem Mikrocontroller selbst basteln.

Auf der Suche nach einem nachbaufähigem Projekt im Internet bin ich auf das Projekt von Lars Pontoppidan (http://pontoppidan.info/lars/index.php?proj=capmeter) gestoßen. Das Gerät ist mit einem AVR - Controller aufgebaut und misst Kapazitäten zwischen einigen Picofarad und einigen Tausend Microfarad. Ich beschloss nach diesem Bauvorschlag vorzugehen. Das Gerät baut allerdings auf dem ATmega8 auf und es ist kein Platinendesign mitveröffentlicht. Daher habe ich mir dort im wesentlichen nur das Grundprinzip geholt und ein ähnliches Gerät mit dem ATmega32 aufgebaut. Die Software habe ich komplett neu geschrieben.

Im folgenden zunächst Erläuterungen in deutsch, dahinter meinen englischer Text aus dem Source-Code, der auch detaillierter auf die Software eingeht.

Theorie zum Messverlauf

Die Zeit, die ein Kondensator braucht, um aufgeladen zu werden steht in einer Beziehung zur Kapazität des Kondensators. Während der Aufladung gibt es einen Bereich, indem die Ladespannung nach einer bekannten Funktion ansteigt. Aus der Steigung der Kurve kann man die Kapazität des Kondensators berechnen.

Die folgende Skizze stellt den Messvorgang genauer dar. Der Kondensator wird entladen, dann wird mit dem Laden begonnen. Die Y-Achse stellt die Spannung, die am Kondensator erreicht wurde, dar. Die X-Achse ist die Zeit.

Verlauf einer Messung. Entladung bis deutlich unter Schwellwert 1 (T1). Dann Start der Ladung des Kondensators. Messung der Zeitpunkte “Threshold 1 erreicht” und “Threshold 2 erreicht”. Aus den beiden Zeitwerten kann dann die Kapazität berechnet werden.

Der Ladevorgang startet in der Nähe von 0V, bei einem festen Spannungswert " Threshold 1". Die Ladung wird durchgeführt bis ein bestimmter weiterer Spannungswert “Threshold 2” erreicht ist. Die Zeit, die zwischen dem Durchgang durch die beiden Thresholds vergeht, wird gemessen.

Die beiden Threshold-Spannungen werden durch einen Spannungsteiler aus der Betriebsspannung +5V gewonnen. Der Spannungsteiler stellt drei Spannungen bereit.

Der Spannungsteiler für die Referenzspannungen.

Die Schaltung des Messgeräts ist in untigem Bild dargestellt.

Der zu messende Kondensator Cx wird über die Widerstände 1M8 bzw. 1K8 geladen. Die erreichte Spannung am Kondensator wird über den analogen Eingang AIN0 des AVRs gemessen. Die LED leuchtet im Original während des Messvorgangs.

Der oben erläuterte Referenzspannungsteiler wird an die analogen Eingänge ADC1..3 angeschlossen.

In blau ist der Anschluss eines Quarzes und an die Versorgungsspannung dargestellt. Wenn man ein vorhandenes Experimentierboard wie das Pollin-Board einsetzt. braucht man diese Anschlüsse nicht beschalten.

Am Port C wurde das LCD-Display (hier SEIKO 2432) angeschlossen.

Beschaltung des ATmega32

Das folgende Bild zeigt den fliegenden Aufbau des Spannungsteilers und der Lade/Messschaltung.

Erster Experimentalaufbau

Im folgenden ist beispielhaft der zeitliche Verlauf des Messzyklus dargestellt. Die Entladung erfolgt in etwa 25ms bis auf den Schwellwert 880mV des Spannungsteilers (weiße Linie). Dann beginnt die Aufladung. Ist diese (nach hier ca. 75ms) beendet, beginnt nach kurzer Zeit der Messzyklus von vorn. Bei der gewählten Kapazität wird mit einer Frequenz von ~3,8Hz die Messung wiederholt. Mit anderen Kapazitäten ergeben sich andere Zeitwerte, das Prinzip bleibt aber gleich.

Der Experimentalaufbau wurde bald auf Lochrasterplatine wiederholt. Im folgenden Bild ist die analoge Beschaltung des AVRs sowie das LCD-Display dargestellt. Der AVR selbst sitzt auf einem Pollin-Board (im Bild nicht dargestellt).

Der gemessene Kondensator hat einen aufgedruckten Wert von 6800µF. (Dieser Kondensator weicht relativ stark nach oben von seinem Sollwert ab)

Für das Capmeter wurde eine kleine Platine entworfen. Der auf der Lochraster-Platine aufgebaute Teil sollte aber weiter verwendet werden. Daher habe ich einen Entwurf gemacht, der bezüglich der verwendeten Pins zum Pollin-Board kompatibel ist, so dass die Lochraster-Platine ohne Änderung an das neue Board angeschlossen werden kann.

Entwurf der Platine mit Eagle. Übrigens meine erste Platine mit Eagle überhaupt.

Beim Belichten der Platine habe ich nur eine Transparentfolie (statt der empfohlenen übereinanderliegenden zwei oder drei) verwendet. Die Platine hat daher viele kleine Löcher im Kupfer, war nach Prüfung mit dem Ohmmeter aber dennoch verwendbar.

Einbau in ein Gehäuse: Das LCD-Display wird ins Gehäuse eingeklebt. Die Platine mit dem AVR wird mit Abstandsbolzen befestigt und die Lochrasterplatine aufrecht stehend angebracht.

Zwischenstand während des Einbaus ins Gehäuse

Erster Test im Gehäuse noch ohne Frontplatte

Im folgenden Erläuterungen zur Software. Diese sind den Sourcedateien entnommen und in Englisch.

Capacitance Meter with AVR

Introduction

This code implements a capacitance meter using an AVR and some external parts. The idea and the hardware is based on the capmeter described at: http://pontoppidan.info/lars/index.php?proj=capmeter

The capmeter is able to measure from small capacitors of some picofarads up to large ones of at least >7000microfarads. My largest Capacitor is 6800 Microfarads (which measures itself as a 7200uF value), so I cannot test it for larger values than this. I assume that even values of lets say 50000uF can be measured.

Usage

Automatic calibration

Switch the device on. The device does a self calibration at startup (displaying this state as “Calibration” at the LCD display). During self-calibration, nothing should be attached to the probes (otherwise all subsequent measurements will display wrong results). After calibration, the device enters the measurement loop.

Doing Measurements

After calibration finished, the device will just repeat measurements. If nothing is attached to the probes, the device will display “0pF”. If something is attached to the probes, it will display the state of the measurement and the last result measured. Measurement time increases with cap size, a large cap will need some seconds to be measured.

These are all things to be known for doing measurements.

Hardware

See config.h for pin assignments. This software uses 4 Pins.

  • Discharge Pin and Analog Comparator Pin (both functions on one pin)
  • Charge Pin for “high current” charging (via 1K8 resistor)
  • Charge Pin for low current charging (via 1M8 resistor)
  • LED Pin. Led lights during measurement. Not very important, because the LCD shows the same info.

There is a resistor network that creates three reference voltages for the comparator. The resistor network creates the following voltages: 0,88, 1,77, 2,65V.

For charging there are two resistors and for discharging there is one resistor.

Basic idea is to measure the time that is needed by the capacitor-in-measurement (“CX”). to be loaded from one voltage level to a higher voltage level. The time is somewhat proportional/linear to the capacity.

Time measurement is done incrementing a timer start running if the load crosses the lower threshold (voltage level) and stop running if the higher threshold is crossed.

Because for small capacities the time for loading is very short, for these the low current charging is used. For high capacities the “high current” charging is used. All measurement cycles are started assuming a small cap. The cap is loaded with low current. If a timeout is reached without having crossed the higher threshold, it is assumed that the cap is a large one. Then the cap is discharged and loaded with high current.

Theory of operation

The loading of a capacitor via a resistor is e.g. described here: http://en.wikipedia.org/wiki/RC_time_constant ( German: RC-Glied, http://de.wikipedia.org/wiki/RC-Glied)

The time constant “tau” can be calculated from

tau = R*C.

The voltage at the capacitor during loading is described by the formula:

    U(t) = Umax * (1 - e^(-t/tau))

This can be changed to

    t = -tau\*ln(1-U(t)/Umax)

Using the voltages resulting from the resistor network, the following values for t can be calculated: (All these things are taken from http://pontoppidan.info/lars/index.php?proj=capmeter, all credits go to Lars Pontoppidan. I just repeated it to understand what he was doing.)

Ti=Threshold i All Values “x” in the table should be used as “tau*x”

Ti | t for 0..Ti | t for Ti-1..Ti | T for T0..Ti —-+——————————————— 0 |0.191891 | - | - 1 |0.43261 | 0,2401 | 0.2401 2 |0.7461127 | 0.313502 | 0.554

Umax=5,04V (from a 7805 voltage regulator).

The timer creates a time value “tclocks”.

What we need is a formula capacitance = f(tclocks).

For the interested reader, to get that, please follow the next steps:

The time t actually spent in charging can be derived from tclocks using the xtal frequence of the AVR “F_CPU”:

  1. t=tclocks/F_CPU

For tau we know two things:

  1. tau*K = t , where K is 0,2401 or 0,554 from the table above
  2. tau=R*C

Using these three input formulas 0,1 and 2, we can proceed:

C=tau/R (using 2.) = t/(K*R) (using 1.) = tclocks/(K*R*F_CPU) (using 0.)

For low current charging, R=1800 Ohms. For high current charges, R=1.800.000 Ohms. (You can measure and use the real values of your resistors with a good ohm-meter to get the best results).

So we have everything to calculate C from tclocks value.

Software

The software uses the UART lib for rs232 output and the LCD Lib for LCD access. These two libs are from Peter Fleury.

The software offers some basic functions to control the ports:

  • discharge()
  • charge()
  • led()

charge allows three different ways of charging:

  • charging via high pin (high current mode, via 1K8 resistor)
  • charging via low pin (low current mode, via 1M8 resistor)
  • supporting discharge using internal pullup resistors of AVR (not really a " charge" mode)

There are several init_*() funtions for initializing different things

  • init_devices()
  • init_timer()
  • init_comparator()
  • init_vars()
  • init_uart()

There are two controlling functions

  • do_measure()
  • do_discharge()

These two functions start measuring/discharging. They do not wait until completion. Completion can be checked using state variables.

Finally there is main(), which does:

  • Initialisation
  • Calibration
  • Repeat measuring cycle
  • Print out on LCD and RS232

One measurement cycle includes the following steps:

  • Discharge cap below lower threshold
  • Charging using low current from LO to MID Threshold
  • If a result can be measured, it is print out and the cycle starts from beginning
  • If no result could be got (a timeout was reached), cap is discharged again
  • Charging using high current from MID to HI Threshold
  • If a result can be measured, it is print out and the cycle starts from beginning
  • Anyway, the cycle starts from beginning

The device does an automatic calibration at startup, i.e. starts after switching on device or after a reset. Precondition for a correct calibration: nothing is attached to the probes. A number (Default: 20) of measurements (“samples”) are taken and the average value of these “measurements of nothing” is calculated and stored. This stored value is then used for subtracting it from the value obtained from the later measurements.

In fact, calibration eliminates the capacity created by the board, the probe cables etc.

During the calibration, displays second line shows the text “Calibration…”.