Lesson 14 Arduino Capacitor Meter Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
// By Roee Bloch Capacitor-Meter //All right Reserved // Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com // // This source is subject to the Roee Bloch License. // Please see the License.txt file for more information. // All other rights reserved. // // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE. // // </copyright> // <author>Roee Bloch</author> // <email>roeebloch@walla.co.il</email> // <date>June 2015</date> /* Part of the CODE is LCD Taken from: Library originally added 18 Apr 2008 by David A. Mellis library modified 5 Jul 2009 by Limor Fried (http://www.ladyada.net) example added 9 Jul 2009 by Tom Igoe modified 22 Nov 2010 by Tom Igoe Formulas in use are: R(measured) = (RKnown(VCC-Vmeasured))/VMeasured V(measured) = ((ADC VALUE)/1024)*VCC Ciruit is this: Connect CRL Pin (Digital pin 2)-> 1K Resistor -> Analog in 1 -> Capacitor to measure Positive || -> Negative capacitor pin to GND */ #include <LiquidCrystal.h> #define analog_in 1 // define analog input pin in use #define digital_ctrl 2 // digital pin control that will charge and discharge capacitor #define VCC 5 //define VCC for calculation #define R_known 1000 // define Reference resistor in use, I have found this value is very good to many measurements, but if you have another resistor you may change this value according #define display_time 5 // how many seconds show the value before remeasuring float V_measured, limit, calc ; int analog_read; char flag = 0, flag2 = 0; int t1, t2, t3, t4; //timers to use // initialize the library with the numbers of the interface pins LiquidCrystal lcd(8, 9, 4, 5, 6, 7); void setup() { Serial.begin(57600); Serial.println("Starting"); pinMode(digital_ctrl, OUTPUT); lcd.begin(16, 2); lcd.print("Capacitor Meter "); digitalWrite(digital_ctrl, LOW); // starting discharge } void loop() { flag = 0; digitalWrite(digital_ctrl, LOW); mycalc(); while (V_measured > 0.1) { digitalWrite(digital_ctrl, LOW); // starting discharge Serial.println("Discharging Capacitor"); lcd.print("Disch. Capacitor"); lcd.setCursor(0, 1); Serial.println("Please wait...."); mycalc(); } // discharge complete myclear(); lcd.setCursor(0, 1); lcd.print("Charging now.."); digitalWrite(digital_ctrl, HIGH); // starting to charge capacitor limit = 0.6321 * VCC; // value of 1 TAU = R*C t1 = millis(); mycalc(); while (V_measured < limit) { // reading again and waiting to charge for 63% mycalc(); } // charge complete t2 = millis(); myclear(); lcd.setCursor(0, 1); Serial.println("COMPLETE"); lcd.print("COMPLETE"); calc = float(t2 - t1) / float(R_known); if (calc < 1) { calc = 1000 * calc; flag = 1; } Serial.println("Capacitor is:"); if (flag == 1) { Serial.print(float(calc), 3); Serial.println(" uF"); myclear(); lcd.setCursor(0, 1); lcd.print("C= "); lcd.print(float(calc), 3); lcd.print(" uF"); delay(display_time*1000); } else { Serial.println(float(calc), 6); Serial.print(float(calc), 3); Serial.println(" mF"); myclear(); lcd.setCursor(0, 1); lcd.print("C= "); lcd.print(float(calc), 3); lcd.print(" mF"); delay(display_time*1000); } Serial.println(t2); //for debug on serial port Serial.println(t1); //for debug on serial port delay(2000); // delay before next reading mycalc(); t3 = millis(); while (V_measured > VCC - 0.1) // not inserted new capacitor yet { mycalc(); t4 = millis(); if (flag2 == 0) { Serial.println("waiting"); flag2 = 1; } if ((t4 - t3) > (display_time*1000)) // after display_time seconds testing again { Serial.println("Testing Again"); lcd.setCursor(0, 1); lcd.print("Testing Again"); delay(1000); break; } } flag2 = 0; } void myprint(float x, String param) { int myprint; myprint = (x); Serial.print(param); Serial.print(" is:"); Serial.println(myprint); } void myclear() // clear all second line { lcd.setCursor(0, 1); lcd.print(" "); } void mycalc() { analog_read = analogRead(analog_in); V_measured = (analog_read) * VCC; // this is voltage *1024 since we did not divide by 1024 V_measured = V_measured / 1024.0; } |
Video to Theory of this Circuit: arduino-capacitor-meter-theory
Video based on this code: lesson-14-arduino-capacitor-meter
[adsenseyu6] |
Parts for this project are here: