SHARE
TWEET

Untitled

a guest Nov 18th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define vTestPin       0
  2. #define chargePin      13
  3. #define dischargePin   11
  4. #define resistorValue  10000.0F
  5. #define ADC_RES        1023.0F
  6. #define V_HIGH         5
  7.  
  8. unsigned long startTime;
  9. unsigned long elapsedTime;
  10. float microFarads;
  11. float nanoFarads;
  12.  
  13. void setup() {
  14.   pinMode(chargePin, OUTPUT);
  15.   digitalWrite(chargePin, LOW);
  16.   Serial.begin(9600);
  17. }
  18.  
  19. void loop() {
  20.   // ---- Capacitance ----
  21.   // start charging capacitor, and record time
  22.   digitalWrite(chargePin, HIGH);
  23.   startTime = millis();
  24.  
  25.   // wait for capacitor to charge ~63% (one T = RC)
  26.   // if it takes more than 2 seconds, assume it's not a capacitor
  27.   while (true) {
  28.     float charge = analogRead(vTestPin);
  29.     elapsedTime = millis() - startTime;
  30.     if (charge > (ADC_RES * .63)) {
  31.       microFarads = ((float)elapsedTime / resistorValue) * 1000;
  32.       break;
  33.     }
  34.     if (elapsedTime > 2000) {
  35.       microFarads = 0;
  36.       break;
  37.     }
  38.   }
  39.  
  40.   // ---- Resistance ----
  41.   float vx = analogRead(vTestPin) * V_HIGH / ADC_RES;
  42.  
  43.   float ix = (V_HIGH - vx) / resistorValue;
  44.   float rx = -1; // If this isn't overwritten, we have an open circuit
  45.   if ( ix > 0) {
  46.     float rx = vx / ix;
  47.   }
  48.  
  49.   // If this was a capacitor, print its capacitance
  50.   if ((microFarads > 0.1) && (microFarads < 30)) {
  51.     Serial.println("The electronic component is a capacitor");
  52.  
  53.     // If it can be printed in microfarads, do so
  54.     if (microFarads > 1) {
  55.       Serial.print((long)microFarads);
  56.       Serial.println(" microFarads is the value of the capacitor and");
  57.  
  58.       if (microFarads > 10) {
  59.         Serial.println("falls into the category HIGH");
  60.       }
  61.       else {
  62.         Serial.println("falls into the category MEDIUM");
  63.       }
  64.     }
  65.  
  66.     // Otherwise print in nanofarads
  67.     else {
  68.       nanoFarads = microFarads * 1000.0;
  69.       Serial.print((long)nanoFarads);
  70.       Serial.println(" nanoFarads is the value of the capacitor and falls into the category LOW");
  71.     }
  72.     delay(1600);
  73.   }
  74.  
  75.   // If this was a resistor, print its resistance
  76.   else if (rx > 0) {
  77.     Serial.println("The electronic component is a resistor");
  78.     Serial.print(rx);
  79.     Serial.println(" ohms is the value of the resistor");
  80.      else {
  81.       Serial.println("Insert component");
  82.     }
  83.   }
  84.  
  85.   // Allow capacitor to discharge
  86.   digitalWrite(chargePin, LOW);
  87.   pinMode(dischargePin, OUTPUT);
  88.   digitalWrite(dischargePin, LOW);
  89.   while (analogRead(vTestPin) > 0) {
  90.   }
  91.   pinMode(dischargePin, INPUT);
  92. }
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