-
Notifications
You must be signed in to change notification settings - Fork 0
/
Capacitance_Meter.ino
103 lines (70 loc) · 3.1 KB
/
Capacitance_Meter.ino
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
#include <LiquidCrystal.h>
/* RCTiming_capacitance_meter
* Paul Badger 2008
* Modified by Christian Terjesen in May 2013
*/
#define analogPin 0 // analog pin for measuring capacitor voltage
#define chargePin 13 // pin to charge the capacitor - connected to one end of the charging resistor
#define dischargePin 2 // pin to discharge the capacitor
#define resistorValue 10000.0F // change this to whatever resistor value you are using
// F formatter tells compliler it's a floating point value
unsigned long startTime;
unsigned long elapsedTime;
float microFarads; // floating point variable to preserve precision, make calculations
float nanoFarads;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup(){
lcd.begin(16, 2);
pinMode(chargePin, OUTPUT); // set chargePin to output
digitalWrite(chargePin, LOW);
Serial.begin(9600); // initialize serial transmission for debugging
}
void loop(){
digitalWrite(chargePin, HIGH); // set chargePin HIGH and capacitor charging
startTime = millis();
while(analogRead(analogPin) < 648){ // 647 is 63.2% of 1023, which corresponds to full-scale voltage
}
elapsedTime= millis() - startTime;
// convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ), net 10^3 (1000)
microFarads = ((float)elapsedTime / resistorValue) * 1000;
Serial.print(elapsedTime); // print the value to serial port
Serial.print(" mS "); // print units and carriage return
if (microFarads > 1){
Serial.print((long)microFarads); // print the value to serial port
Serial.println(" microFarads"); // print units and carriage return
lcd.print((long)microFarads);
lcd.print(" microFarads");
}
else
{
// if value is smaller than one microFarad, convert to nanoFarads (10^-9 Farad).
// This is a workaround because Serial.print will not print floats
nanoFarads = microFarads * 1000.0; // multiply by 1000 to convert to nanoFarads (10^-9 Farads)
Serial.print((long)nanoFarads); // print the value to serial port
Serial.println(" nanoFarads"); // print units and carriage return
lcd.print((long)nanoFarads);
lcd.print(" nanoFarads");
}
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
}
/* dicharge the capacitor */
digitalWrite(chargePin, LOW); // set charge pin to LOW
pinMode(dischargePin, OUTPUT); // set discharge pin to output
digitalWrite(dischargePin, LOW); // set discharge pin LOW
while(analogRead(analogPin) > 0){ // wait until capacitor is completely discharged
pinMode(dischargePin, INPUT); // set discharge pin back to input
}
lcd.clear();
}