Gerrit Pathuis
/
Measuring_Capacitance
Measuring the capacity of a capacitor, dumping info to PC screen
main.cpp
- Committer:
- GerritPathuis
- Date:
- 2012-01-07
- Revision:
- 0:863c3f4f452f
- Child:
- 1:d95ae9068740
File content as of revision 0:863c3f4f452f:
#include "mbed.h" ////////////////////////////////////////// // GPa@quicknet.nl ///////////////////////////////////////// // // An unkwown ceramic capacitor is connected to // pin21 via a 82 kilo ohm resister, the other // capacitor terminal is connected to ground // // A ceramic capacitor is used becouse it is non polarized // // || // pin21-----RRRRRRR-----------||------- pin1 (GND) // | || // 82k ohm | ?? Farad // | // pin20------------------- // // Mbed pins used // Pin 20...... analog input // Pin 21...... digital out ////////////////////////////////////////// // How does it work // measure voltage on pin 20 // If (not zero Volt) then wait // else pin 21 goes high 3.3 Volt // wait 2 micro seconds (the capacitor is now charging) // measure voltage on pin 20 // the Voltage increase in 2 micro seconds is a measure // for the size of the capacitor // a small capacitor will charge fast a big one slow // // Cap = current * time / measured voltage change // current = voltage over the resistor / resistor value // voltage max is 3.3 volt !!! // // The measuring results are send via Teraterm to the PC screen // Note 1) // The tolerance range of a ceramic capacitor // lies between 5 and 100%, not very accure at all. // Note 2) // Choose the charging time such that the capacitor // reaches about 3 Volt, lower you get less accurate results // higher the 3.3 is not acceptable to the analogin // // Cap sizes are // m = 10^-3 (mili) // u = 10^-6 (micro) // n = 10^-9 (nano) // p = 10^-12 (pico) // f = 10^-15 (femto) ///////////////////////////////////////// AnalogIn ain(p20); DigitalOut red(p21); Serial pc(USBTX, USBRX); // tx, rx int main() { int i, cnt; double volt_0; // first measurement double volt_1; // second measurement double volt_delta; // delta voltage double current; // max current trough resistor double cx; // capacitance to be calculated; double mmtime =20; // measure time in micro seconds pc.baud(9600); pc.format(8,Serial::None,1); for (i=0; i<2250; i +=1) { // starting at zero volt red.write(0); wait_ms(20); // deterime the number of measurements // start measurement, wait for 0.0 volt on input volt_0 = 1; cnt=0; while (volt_0 > 0.0001) { // wait for 0.0 Volt wait_us(2); cnt+=1; // wait volt_0 = ain.read(); } red.write(1); // output high wait_us(mmtime); // measuring time volt_1 = ain.read(); red.write(0); // output low // calculations volt_0 *= 3.3; volt_1 *= 3.3; volt_delta = abs(volt_1 - volt_0); current = (3.3- volt_delta/2) / 82000; // 82k resistor cx = current* mmtime/(1000 * 1000 * volt_delta); //pc.printf("Volt_0 %f, volt_1 %f, current %f ", volt_0, volt_1, current); //pc.printf("Delta voltage %f, time %f ", volt_delta, mmtime); pc.printf("cnt= %3d, Delta= %5.4f Volt ", cnt, volt_delta); if (volt_1 > 3.1) pc.printf("Out of range \n\r"); else { if (cx < 1 and cx >= 0.001) { cx = cx *1000; pc.printf("Cap. is %5.4f milli Farad ", cx); } else if (cx < 0.001 and cx >= 0.000001) { cx = cx *1000 *1000; pc.printf("Cap. is %5.4f micro Farad ", cx); } else if (cx < 0.000001 and cx >= 0.000000001) { cx = cx *1000 *1000 *1000; pc.printf("Cap. is %4.2f nano Farad ", cx); } if (cx < 0.000000001 and cx >= 0.000000000001) { cx = cx *1000 *1000 *1000 *1000; pc.printf("Cap. is %4.0f pico Farad ", cx); } else if (cx < 0.000000000001) pc.printf("Cx = %f Out of range too small! ", cx); pc.printf("\r"); // pc.printf("\n"); } } }