Lesson 14 Arduino Capacitor Meter Code
// 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: https://youtu.be/3kUuhFtO1KM
Video based on this code: lesson-14-arduino-capacitor-meter
[adsenseyu6] |
Parts for this project are here: