SHARE
TWEET

Arduino Nano Capacitance Meter

Braulio777 Nov 13th, 2017 (edited) 865 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Arduino Nano Capacitance Meter
  2. /from 0.000pF to 1mF
  3.  
  4. #include <LiquidCrystal.h>
  5. LiquidCrystal lcd(12, 10, 5, 4, 3, 2);
  6. const int OUT_PIN = A4;
  7. const int IN_PIN = A0;
  8. const float IN_STRAY_CAP_TO_GND = 24.48;
  9. const float IN_CAP_TO_GND  = IN_STRAY_CAP_TO_GND;
  10. const float R_PULLUP = 34.8;  
  11. const int MAX_ADC_VALUE = 1023;
  12.  
  13. void setup()
  14. {
  15.   pinMode(OUT_PIN, OUTPUT);
  16.   pinMode(IN_PIN, OUTPUT);
  17.   lcd.begin(16, 2);
  18.   }
  19.  
  20. void loop()
  21. {
  22.     pinMode(IN_PIN, INPUT);
  23.     digitalWrite(OUT_PIN, HIGH);
  24.     int val = analogRead(IN_PIN);
  25.     digitalWrite(OUT_PIN, LOW);
  26.  
  27.     if (val < 1000)
  28.     {
  29.       pinMode(IN_PIN, OUTPUT);
  30.  
  31.       float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - val);
  32.  
  33.       lcd.setCursor(0,0);
  34.       lcd.print("Capacitor =");
  35.       lcd.setCursor(0,1);
  36.       lcd.print("                ");
  37.       lcd.setCursor(0,1);
  38.       lcd.print(capacitance, 3);// for the best precision
  39.       lcd.print("pF ");
  40.    
  41.     }
  42.    
  43.     else
  44.     {
  45.       pinMode(IN_PIN, OUTPUT);
  46.       delay(1);
  47.       pinMode(OUT_PIN, INPUT_PULLUP);
  48.       unsigned long u1 = micros();
  49.       unsigned long t;
  50.       int digVal;
  51.  
  52.       do
  53.       {
  54.         digVal = digitalRead(OUT_PIN);
  55.         unsigned long u2 = micros();
  56.         t = u2 > u1 ? u2 - u1 : u1 - u2;
  57.       }
  58.      
  59.       while ((digVal < 1) && (t < 400000L));
  60.  
  61.       pinMode(OUT_PIN, INPUT);  
  62.       val = analogRead(OUT_PIN);
  63.       digitalWrite(IN_PIN, HIGH);
  64.       int dischargeTime = (int)(t / 1000L) * 5;
  65.       delay(dischargeTime);  
  66.       pinMode(OUT_PIN, OUTPUT);  
  67.       digitalWrite(OUT_PIN, LOW);
  68.       digitalWrite(IN_PIN, LOW);
  69.  
  70.       float capacitance = -(float)t / R_PULLUP
  71.                               / log(1.0 - (float)val / (float)MAX_ADC_VALUE);
  72.  
  73.       lcd.setCursor(0,0);
  74.       lcd.print("Capacitor =");
  75.       if (capacitance > 1000.0)
  76.       {
  77.         lcd.setCursor(0,1);
  78.         lcd.print("                ");
  79.         lcd.setCursor(0,1);
  80.         lcd.print(capacitance / 1000.0, 2);
  81.         lcd.print("uF ");
  82.    
  83.       }
  84.        
  85.       else
  86.       {
  87.         lcd.setCursor(0,1);
  88.         lcd.print("                ");
  89.         lcd.setCursor(0,1);
  90.         lcd.print(capacitance, 2);
  91.         lcd.print("nF ");
  92.  
  93.       }
  94.      
  95.     while (millis() % 1000 != 0);
  96. }
  97.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top