Advertisement
Electgpl

ARDUINO - Capacimetro pF nF uF

Feb 3rd, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. //Capacimetro Digital
  2. //Desde 0.000pF hasta 1000uF
  3. const int OUT_PIN = A4;
  4. const int IN_PIN = A0;
  5. const float IN_STRAY_CAP_TO_GND = 24.48;
  6. const float IN_CAP_TO_GND = IN_STRAY_CAP_TO_GND;
  7. const float R_PULLUP = 34.8;  
  8. const int MAX_ADC_VALUE = 1023;
  9. void setup(){
  10.    pinMode(OUT_PIN, OUTPUT);
  11.    pinMode(IN_PIN, OUTPUT);
  12.    Serial.begin(9600);
  13. }
  14. void loop(){
  15.    pinMode(IN_PIN, INPUT);
  16.    digitalWrite(OUT_PIN, HIGH);
  17.    int val = analogRead(IN_PIN);
  18.    digitalWrite(OUT_PIN, LOW);
  19.    if(val < 1000){
  20.       pinMode(IN_PIN, OUTPUT);
  21.       float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - val);
  22.       Serial.print("Capacitor = ");
  23.       Serial.print(capacitance, 3);// for the best precision
  24.       Serial.println("pF ");
  25.    }else{
  26.       pinMode(IN_PIN, OUTPUT);
  27.       delay(1);
  28.       pinMode(OUT_PIN, INPUT_PULLUP);
  29.       unsigned long u1 = micros();
  30.       unsigned long t;
  31.       int digVal;
  32.       do{
  33.          digVal = digitalRead(OUT_PIN);
  34.          unsigned long u2 = micros();
  35.          t = u2 > u1 ? u2 - u1 : u1 - u2;
  36.       }
  37.       while((digVal < 1) && (t < 400000L));
  38.       pinMode(OUT_PIN, INPUT);  
  39.       val = analogRead(OUT_PIN);
  40.       digitalWrite(IN_PIN, HIGH);
  41.       int dischargeTime = (int)(t / 1000L) * 5;
  42.       delay(dischargeTime);  
  43.       pinMode(OUT_PIN, OUTPUT);  
  44.       digitalWrite(OUT_PIN, LOW);
  45.       digitalWrite(IN_PIN, LOW);
  46.       float capacitance = -(float)t / R_PULLUP
  47.                         / log(1.0 - (float)val / (float)MAX_ADC_VALUE);
  48.       Serial.print("Capacitor =");
  49.       if(capacitance > 1000.0){
  50.          Serial.print(capacitance / 1000.0, 2);
  51.          Serial.println("uF ");
  52.       }else{
  53.          Serial.print(capacitance, 2);
  54.          Serial.println("nF ");
  55.       }
  56.       while (millis() % 1000 != 0);
  57.    }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement