-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimer555MonostableSketch.ino
199 lines (152 loc) · 5.95 KB
/
Timer555MonostableSketch.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
File: Timer555Monostable
Version: 0.0.6
Date: 19-Dec-2018
Revision: 16-Feb-2019
Author: Jerome Drouin
https://github.com/newEndeavour/Timer555Monostable
Capacitive &/or Resistance Meter Library for 'duino / Wiring
Capacitance &/or Resistance is derived via 555 Timer IC in Monostable mode, Resistor R1 (Cap Meter mode)
or Capacitance C1 (Res Meter mode).
Capacitance by default is expressed in NanoFarads.
Resistance by default is expressed in Ohms.
C = (a / b x T) / (1.1 x R1) ; with T in seconds and C in nF
C : Capacitance in nF
R1 : Resistance in Ohms
a = 1E9 : 1,000,000,000 nano Farads in 1 Farad (FARADS_TO_NANOFARADS)
b = 1E6 : 1,000,000 microseconds in one second (SECONDS_TO_MICROS)
R = (c / b x T) / (1.1 x C1) ; with T in seconds and C in pF
R : Resistance in Ohms
C1 : Capacitance in pF
c = 1E12 : 1,000,000,000,000 pico Farads in 1 Farad (FARADS_TO_PICOFARADS)
Credits:
- Library initially inspired by/ derived from "CapacitiveSensor.h" by
Paul Bagder & Paul Stoffregen. Thanks.
- Direct I/O through registers and bitmask (from OneWire library)
Copyright (c) 2018-2019 Jerome Drouin All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Timer555Monostable.h>
#define TRIGGER_FREQ_mS 1000
#define CAP_BASELINE 0.0 // Baseline for Empty Capacitance in nF/Enter 0.0 if not in use
#define RES_BASELINE 100 // Baseline for Empty Resistance in Ohms/Enter 0.0 if not in use
#define SAMPLESIZE_1 2 //
#define SAMPLESIZE_2 10 //
#define SAMPLESIZE_3 50 //
#define R1 240000 // in Ohms : Reference Resistance in Capacitor Meter Mode
#define C1 150.0 // in pF : Reference Capacitor in Resistance Meter Mode
//Pins
const int8_t Output555 = 0; // Connected to 555Timer IC Pin 3 (Monostable)
const int8_t Trigger555 = 1; // Connected to 555Timer IC Pin 2 (Monostable)
Timer555Monostable Timer555(Trigger555,Output555,R1,C1,CAP_BASELINE,RES_BASELINE);
int LoopCount=0;
// --- Setup -----------------------------------------------------------
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.print("\n---- Serial Started ----\n");
Serial.print("\n---- Object Versions ----\n");
Serial.print("\nBoard\t:"+Timer555.GetBoardType());
Serial.print("\nVersion\t:"+Timer555.GetVersion());
Serial.print("\nRelease\t:"+Timer555.GetReleaseDate());
Serial.print("\nTiming\t:"+Timer555.GetTimingMethod());
Serial.print("\n\n");
pinMode(Output555, INPUT); // PIN 3 = OUTPUT OF 555 CONNECTED HERE
pinMode(Trigger555, OUTPUT); // CONNECTED TO PIN 2 = TRIGGER OF 555
digitalWrite(Trigger555, HIGH); // High to Avoid triggering 555 accidentally
}
// --- Loop -----------------------------------------------------------
void loop() {
LoopCount++;
float Cap = Timer555.GetCapacitance(SAMPLESIZE_1);
float Res = Timer555.GetResistance(SAMPLESIZE_1);
//Default unit = nF
int display_factor = 1;
String display_unit_Cap = " nF";
String display_unit_Res = " Ohms";
if (Cap<1) {
display_factor = 1000;
display_unit_Cap = " pF";
}
if (Cap>1000) {
display_factor = 1/1000;
display_unit_Cap = " uF";
}
if (Cap>1000000) {
display_factor = 1/1000000;
display_unit_Cap = " mF";
}
Serial.print(LoopCount);
Serial.print(") ");
//SAMPLE SIZE 1
Serial.print("\tSamples:");
Serial.print(SAMPLESIZE_1);
Serial.print("\tCap:");
Serial.print(Cap * display_factor, 3);
Serial.print(display_unit_Cap);
Serial.print("\tRes:");
Serial.print(Res , 1);
Serial.print(display_unit_Res);
Serial.print("\tDur:");
Serial.print(Timer555.GetDuration());
Serial.print(" us");
Serial.print("\tPer:");
Serial.print(Timer555.GetAvgPeriod());
Serial.print(" us");
Serial.print("\tFreq:");
Serial.print(Timer555.GetAvgFrequency(), 3);
Serial.print(" Hz");
//SAMPLE SIZE 2
Cap = Timer555.GetCapacitance(SAMPLESIZE_2);
Res = Timer555.GetResistance(SAMPLESIZE_2);
Serial.print("\n");
Serial.print("\tSamples:");
Serial.print(SAMPLESIZE_2);
Serial.print("\tCap:");
Serial.print(Cap * display_factor, 3);
Serial.print(display_unit_Cap);
Serial.print("\tRes:");
Serial.print(Res , 1);
Serial.print(display_unit_Res);
Serial.print("\tDur:");
Serial.print(Timer555.GetDuration());
Serial.print(" us");
Serial.print("\tPer:");
Serial.print(Timer555.GetAvgPeriod());
Serial.print(" us");
Serial.print("\tFreq:");
Serial.print(Timer555.GetAvgFrequency(), 3);
Serial.print(" Hz");
//SAMPLE SIZE 3
Cap = Timer555.GetCapacitance(SAMPLESIZE_3);
Res = Timer555.GetResistance(SAMPLESIZE_3);
Serial.print("\n");
Serial.print("\tSamples:");
Serial.print(SAMPLESIZE_3);
Serial.print("\tCap:");
Serial.print(Cap * display_factor, 3);
Serial.print(display_unit_Cap);
Serial.print("\tRes:");
Serial.print(Res , 1);
Serial.print(display_unit_Res);
Serial.print("\tDur:");
Serial.print(Timer555.GetDuration());
Serial.print(" us");
Serial.print("\tPer:");
Serial.print(Timer555.GetAvgPeriod());
Serial.print(" us");
Serial.print("\tFreq:");
Serial.print(Timer555.GetAvgFrequency(), 3);
Serial.print(" Hz");
Serial.print("\n\n");
delay(TRIGGER_FREQ_mS);
}