Gerrit Pathuis / Mbed 2 deprecated Measuring_Capacitance

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //////////////////////////////////////////
00004 // GPa@quicknet.nl
00005 /////////////////////////////////////////
00006 //
00007 // An unkwown ceramic capacitor is connected to
00008 // pin21 via a 82 kilo ohm resister, the other
00009 // capacitor terminal is connected to ground
00010 //
00011 // A ceramic capacitor is used becouse it is non polarized
00012 //
00013 //                                 ||
00014 //     pin21-----RRRRRRR-----------||------- pin1 (GND)
00015 //                            |    ||
00016 //                82k ohm     |    ?? Farad
00017 //                            |
00018 //     pin20-------------------
00019 //
00020 // Mbed pins used
00021 // Pin 20...... analog input
00022 // Pin 21...... digital out
00023 //////////////////////////////////////////
00024 // How does it work
00025 // measure voltage on pin 20
00026 // If (not zero Volt) then wait
00027 // else pin 21 goes high 3.3 Volt
00028 // Capacitor is charging
00029 // wait until 3.13 Volt being 3 Tou
00030 // Theory step response on a first order system
00031 //
00032 // 1 x Tou= 63% final value
00033 // 2 x Tou= 86% final value
00034 // 3 x Tou= 95% final value
00035 //
00036 // Tou = R x C
00037 // ==> C = Tou / R
00038 //
00039 // The measuring results are send via Teraterm to the PC screen
00040 // Note 1)
00041 // The tolerance range of a ceramic capacitor
00042 // lies between 5 and 100%, not very accure at all.
00043 // Note 2)
00044 //
00045 // Cap sizes are
00046 // m = 10^-3  (mili)
00047 // u = 10^-6  (micro)
00048 // n = 10^-9  (nano)
00049 // p = 10^-12 (pico)
00050 // f = 10^-15 (femto)
00051 /////////////////////////////////////////
00052 
00053 AnalogIn ain(p20);
00054 DigitalOut red(p21);
00055 Serial pc(USBTX, USBRX); // tx, rx
00056 Timer timer;
00057 
00058 int main() {
00059     int i;
00060     double timec;
00061     double volt;       // Voltage measurement
00062     double cx;         // capacitance
00063 
00064     pc.baud(9600);
00065     pc.format(8,Serial::None,1);
00066 
00067     for (i=0; i<2250; i +=1)  {
00068         wait_ms(10);                  // deterime the number of measurements
00069 
00070         // Wait for 0.0 volt on input
00071         red.write(0);
00072         volt = 1;
00073         while (volt > 0.0001) {     // wait for 0.0 Volt
00074             wait_us(2);
00075             volt = ain.read();
00076         }
00077         // start to measure
00078         timer.reset();
00079         timer.start();
00080         red.write(1);               // output high
00081 
00082         while (volt < 3.13) {       // wait for 3.13 Volt
00083             volt = ain.read()*3.3;
00084         }
00085         timer.stop();
00086         timec=timer.read();
00087         red.write(0);               // output low
00088 
00089         // calculations
00090         cx = (double) timec/ (3 * 82000);
00091 
00092         pc.printf("Volt %3.2f, time %9.4f msec ", volt, timec*1000);
00093         if (cx < 1 and cx >= 0.001) {
00094             cx = cx *1000;
00095             pc.printf("Cap. is %5.1f milli Farad    ", cx);
00096         } else if (cx < 0.001 and cx >= 0.000001) {
00097             cx = cx *1000 *1000;
00098             pc.printf("Cap. is %5.1f micro Farad     ", cx);
00099         } else if (cx < 0.000001 and cx >= 0.000000001) {
00100             cx = cx *1000 *1000 *1000;
00101             pc.printf("Cap. is %5.1f nano Farad      ", cx);
00102         } else if (cx <  0.000000001 and cx >=  0.000000000001) {
00103             cx = cx *1000 *1000 *1000 *1000;
00104             pc.printf("Cap. is %5.1f pico Farad       ", cx);
00105         } else if (cx < 0.000000000001)
00106             pc.printf("Cx = %5.1f Out of range too small! ", cx);
00107         pc.printf("\r");
00108         pc.printf("\n");
00109     }
00110 }