Skip to content

Commit b9cdbc2

Browse files
version 2
1 parent 30067ce commit b9cdbc2

File tree

6 files changed

+100
-67
lines changed

6 files changed

+100
-67
lines changed

Cap_meter_arduino/Cap_meter_arduino.ino

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ Adafruit_SSD1306 display(OLED_RESET);
2424
#define analogPin 6
2525
#define chargePin 12
2626
#define dischargePin 11
27-
#define resistorValue 10000.0F
27+
#define resistorValue 9988.0F // 10K in theory User adjust
2828

2929

3030
// Pins and vars for test 2
31-
const int OUT_PIN = A3; // pos pin
32-
const int IN_PIN = A2;
33-
const float IN_STRAY_CAP_TO_GND = 24.48;
34-
const float IN_CAP_TO_GND = IN_STRAY_CAP_TO_GND;
35-
const float R_PULLUP = 34.8;
36-
const int MAX_ADC_VALUE = 1023;
31+
const int OutPin = A3;
32+
const int InPin = A2;
33+
const float CapOne = 24.48; //user calibrate
34+
const float Res_Pullup = 34.8;
35+
const int MaxADC_Value = 1023;
3736

3837

3938
//Pins and vars for test 3
4039
const byte pulsePin = 2;
41-
const unsigned long resistance = 9830; // (10K in theory)
40+
const unsigned long resistance = 9830; // (10K in theory) user adjust
4241
volatile boolean triggered;
4342
volatile boolean active;
4443
volatile unsigned long startTime;
@@ -65,6 +64,12 @@ int test_count = 0;
6564
//*************************** SETUP ************************
6665
void setup() {
6766

67+
// Status LED
68+
pinMode(LED_BUILTIN, OUTPUT);
69+
digitalWrite(LED_BUILTIN, HIGH);
70+
delay(80);
71+
digitalWrite(LED_BUILTIN, LOW);
72+
6873
// Setup pins for button enable internal pull-up resistors
6974
digitalWrite(3, HIGH);
7075
digitalWrite(8, HIGH);
@@ -73,15 +78,22 @@ void setup() {
7378
btn_test_two.begin();
7479
btn_test_three.begin();
7580

76-
// Setup pins for testing 1 and 2
81+
// Setup pins for test 1
7782
pinMode(chargePin, OUTPUT);
78-
pinMode(OUT_PIN, OUTPUT);
79-
pinMode(IN_PIN, OUTPUT);
8083
digitalWrite(chargePin, LOW);
81-
//test3
84+
85+
// Setup pins for test 2
86+
pinMode(OutPin, OUTPUT);
87+
pinMode(InPin, OUTPUT);
88+
89+
// setup for test3
8290
pinMode(pulsePin, OUTPUT);
8391
digitalWrite(pulsePin, LOW);
84-
ADCSRB = 0;
92+
// Set up Analog Comparator
93+
ADCSRB = 0; // clear ADCSRB registers
94+
// Analog Comparator Interrupt Flag: Clear Pending Interrupt
95+
// Analog Comparator Interrupt: Enabled
96+
// Analog Comparator Interrupt Mode: interrupt on the rising edge
8597
ACSR = _BV (ACI)
8698
| _BV (ACIE)
8799
| _BV (ACIS0) | _BV (ACIS1);
@@ -91,7 +103,6 @@ void setup() {
91103
//display intial OLED screen
92104
Display_init();
93105
Serial.println("------------- CAP Meter Comms UP ------------");
94-
delay(500);
95106
}
96107

97108
//******************* MAIN LOOP *****************
@@ -113,7 +124,6 @@ void loop() {
113124
}
114125
}
115126

116-
117127
// ********************* FUNCTIONS *************************
118128

119129
//Function to handle test1
@@ -168,16 +178,16 @@ void Test_two()
168178
display.clearDisplay();
169179
display.setCursor(0, 0);
170180

171-
pinMode(IN_PIN, INPUT);
172-
digitalWrite(OUT_PIN, HIGH);
173-
int val = analogRead(IN_PIN);
174-
digitalWrite(OUT_PIN, LOW);
181+
pinMode(InPin, INPUT);
182+
digitalWrite(OutPin, HIGH);
183+
int val = analogRead(InPin);
184+
digitalWrite(OutPin, LOW);
175185

176186
if (val < 1000)
177187
{
178-
pinMode(IN_PIN, OUTPUT);
188+
pinMode(InPin, OUTPUT);
179189
// Cu = VA2 * C1 / (VA3 - VA2)
180-
float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - val);
190+
float capacitance = (float)val * CapOne / (float)(MaxADC_Value - val);
181191
Serial.print(F("Capacitance Value = "));
182192
Serial.print(capacitance, 3);
183193
Serial.print(F(" pF ("));
@@ -188,33 +198,33 @@ void Test_two()
188198
}
189199
else
190200
{
191-
pinMode(IN_PIN, OUTPUT);
201+
pinMode(InPin, OUTPUT);
192202
delay(1);
193-
pinMode(OUT_PIN, INPUT_PULLUP);
203+
pinMode(OutPin, INPUT_PULLUP);
194204
unsigned long u1 = micros();
195205
unsigned long t;
196206
int digVal;
197207
do
198208
{
199-
digVal = digitalRead(OUT_PIN);
209+
digVal = digitalRead(OutPin);
200210
unsigned long u2 = micros();
201211
// condition ? result_if_true : result_if_false
202212
t = u2 > u1 ? u2 - u1 : u1 - u2;
203213
} while ((digVal < 1) && (t < 400000L));
204214

205-
pinMode(OUT_PIN, INPUT);
206-
val = analogRead(OUT_PIN);
215+
pinMode(OutPin, INPUT);
216+
val = analogRead(OutPin);
207217

208218
//discharge
209-
digitalWrite(IN_PIN, HIGH);
219+
digitalWrite(InPin, HIGH);
210220
int dischargeTime = (int)(t / 1000L) * 5;
211221
delay(dischargeTime);
212-
pinMode(OUT_PIN, OUTPUT);
213-
digitalWrite(OUT_PIN, LOW);
214-
digitalWrite(IN_PIN, LOW);
222+
pinMode(OutPin, OUTPUT);
223+
digitalWrite(OutPin, LOW);
224+
digitalWrite(InPin, LOW);
215225

216-
float capacitance = -(float)t / R_PULLUP
217-
/ log(1.0 - (float)val / (float)MAX_ADC_VALUE);
226+
float capacitance = -(float)t / Res_Pullup
227+
/ log(1.0 - (float)val / (float)MaxADC_Value);
218228
Serial.print(F("Capacitance Value = "));
219229
if (capacitance > 1000.0)
220230
{
@@ -297,10 +307,12 @@ void Test_three(void)
297307
}
298308
}
299309

300-
// Function to handle button press start display to OLED and serial Mon
310+
// Function to handle button press start display to OLED and serial monitor
301311
void TestButton(int which_button)
302312
{
313+
digitalWrite(LED_BUILTIN, LOW);
303314
test_count ++;
315+
304316
Serial.print("testcount: ");
305317
Serial.println(test_count);
306318
Serial.print("Buttonpressed: ");
@@ -359,15 +371,15 @@ void Display_init()
359371
display.clearDisplay();
360372

361373
display.setCursor(0, 0);
362-
display.print("Test 2 center");
374+
display.print("Test 2 left");
363375
display.setCursor(0, 15);
364376
display.print("Range 18pF - 470uF");
365377
display.display();
366378
delay(1500);
367379
display.clearDisplay();
368380

369381
display.setCursor(0, 0);
370-
display.print("Test 3 Left");
382+
display.print("Test 3 top");
371383
display.setCursor(0, 15);
372384
display.print("Range 4.7nF - 180uF");
373385
display.display();
@@ -377,14 +389,15 @@ void Display_init()
377389

378390
}
379391

380-
//Function to display ready message
392+
//Function to display ready message and Status LED
381393
void OLEDready()
382-
{
394+
{
383395
display.clearDisplay();
384396
display.setTextSize(2);
385397
display.setCursor(0, 0);
386398
display.print("Ready");
387399
display.display();
400+
digitalWrite(LED_BUILTIN, HIGH);
388401
}
389402

390403
//******************* EOF *****************

README.md

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Overview
33
* Name : Cap_meter_arduino
44
* Title : Capacitance meter Arduino based microcontroller.
55
* Description : Capacitance meter for Arduino, three tests ,
6-
range 18pf to 4F , Push Button input , OLED and serial monitor output.
6+
range 18pf to 4F , Push Button input , OLED, serial monitor output and LED(onboard NANO)
77
* Author: Gavin Lyons
88
* URL: https://github.com/gavinlyonsrepo/Cap_meter_arduino
99

@@ -32,9 +32,9 @@ You will need following parts
3232

3333
> Three pushbuttons
3434
>
35-
> Resistors Test2(10Kohm and 220ohm) , Test3(10K, 3.1K 1.8K)
35+
> Resistors: Test2(10Kohm and 220ohm) , Test3(10K, 3.1K, 1.8K)
3636
>
37-
> Arduino micro-controller (tested on a NANO and UNO)
37+
> Arduino NANO micro-controller module (tested on a NANO and UNO)
3838
>
3939
> I2C 0.91" inch 128x32 OLED Display Module SSD1306 Driver IC
4040
>
@@ -50,7 +50,12 @@ Three push buttons start the three tests respectively.
5050

5151
1. Test 1 Range 1 uF to 4F.
5252
2. Test 2 Range 18 pF to 470 uF.
53-
3. Test 3 Range 0.0047 uF to 180 uF.
53+
3. Test 3 Range 4.7 nF to 180 uF.
54+
55+
The on-board LED of nano module D13 is used as Status LED.
56+
It Flashes during startup, stays on during "READY to test" state and goes off during "TESTING" state
57+
58+
The OLED displays brief help message and title screen duirng start-up
5459

5560
Each Arduino capacitance meter relies on a property of resistor capacitor (RC) circuits- the time constant.
5661
The time constant of an RC circuit is defined as the time it takes for the voltage across the capacitor
@@ -73,35 +78,45 @@ we can use the formula above in a program that will calculate the unknown capaci
7378

7479
Test 1: Range 1 uF to 4F.
7580
Uses two digital pins, one analog pin, and two resistors.
76-
One for discharge one for charge
77-
Insert a capacitor in range into terminal press button and view result on OLED or serial monitor
78-
The result gives two values: value of cap in uF and time constant to test in mS.
81+
One for discharge, one for charge.
82+
Insert a capacitor in range into terminal press button and view result on OLED or serial monitor.
83+
The Capacitor is charged, We can measure elapsed time until time constant fulfilled using analog pin and we know R
84+
value. Therefore we solve for C.
85+
The result gives two values: Capacitance value and time constant to test in mS.
86+
The user can adjust the 10K resistor value in code variable(resistorValue) to match exact value, to improve accuracy
7987

8088
![ScreenShot cap sch](https://github.com/gavinlyonsrepo/Cap_meter_arduino/blob/master/images/sch.jpg)
8189

8290
**Test 2**
8391

8492
Test 2 : Range 18 pF to 470 uF.
93+
8594
This capacitance test has the greatest range of two. It also had the highest accuracy with smaller capacitors.
8695
No resistors are needed and it only uses two analog pins(A2(neg) and A3(positive) from the Arduino.
8796
There is no extra resistor just the internal components of atmega chip.
97+
98+
C1 calibration(optional):
99+
88100
The internal analog pin capacitor to gnd (C1) varies between 20 and 30pF. unknown capacitor = Cu.
89101
The positive pin is set to 5V, the negative to 0V.
90-
VA2 = (VA3*Cu)/(C1 + Cu).
102+
VA2 = (VA3 * Cu)/(C1 + Cu).
103+
The value of C1 for any given Arduino board will have to be calibrated using a known cap for Cu,
104+
for full accuracy I set it at 24.48pF in code (CapOne) Found using equation.
105+
C1 = Cu * (VA3-VA2) / VA3, Where Va2 is ADC value reported by code and VA3 is 5v or 1023.
106+
So for a known capacitor of 103pF giving a ADC value of 801.
107+
So C1 = 103pf * (1023-801)/ 1023 = 28.48pF.
108+
The user should change this for their system to improve accuracy.
109+
Test a known cap get ADC value and then pop these values into equation above
110+
and put the C1 value into (CapOne) variable in code.
91111

92-
The value of C1 for any given Arduino board will have to be calibrated using a known cap for Cu
93-
for full accuracy I set it at 24.48pF in code(IN_STRAY_CAP_TO_GND) Found using equation.
94-
C1 = Cu *(VA3-VA2) / VA3, Where Va2 is ADC value reported by code and VA3 is 5v or 1023.
95-
so for a known capacitor of 103pF giving a ADC value of 801
96-
so C1 = 102pf * (1023-801)/ 1023 = 28.48pF.
97-
The user will have to change IN_STRAY_CAP_TO_GND for their system.
112+
Test equation:
113+
114+
Cu = VA2 * C1 / (VA3 - VA2)
98115

99-
Test a known cap get ADC value and then pop these values into equation above
100-
and put the C1 value into IN_STRAY_CAP_TO_GND variable in code.
101116
The test displays 3 values
102-
* Capacitance in Farads
117+
* Cu, Capacitance in Farads
103118
* Time constant for test in mS
104-
* ADC measurement from 0 to 1023 where 1023 is 5V
119+
* Va2 , ADC measurement from 0 to 1023 where 1023 is 5V
105120

106121
![ScreenShot cap sch 2](https://github.com/gavinlyonsrepo/Cap_meter_arduino/blob/master/images/test2sch.jpg)
107122

@@ -113,14 +128,23 @@ Test 3 Range 0.0047 uF to 180 uF.
113128
![ScreenShot cap sch 3](https://github.com/gavinlyonsrepo/Cap_meter_arduino/blob/master/images/test3sch.jpg)
114129

115130
A voltage divider is created to give a reference voltage,
131+
116132
Vout = Vin * (R5 / (R5 + R4)) , 5 * (3100 / (1800 + 3100)) = 3.163.
133+
117134
The objective here is to find the time constant τ (tau) in the equation:
135+
118136
τ = R * C Solved for C: C = τ / R
137+
119138
So we want to charge the capacitor under test with a suitable voltage (eg. 5V from an Arduino output pin) and measure how long it takes to reach 63.2% of that voltage. So we need a reference voltage of 5 * 0.632, namely 3.16V.
120-
The internal analogue comparator is just the thing for the job. We connect the reference voltage to the AIN1 pin (negative reference) and connect our capacitor to the AIN0 pin (positive reference) and then configure an interrupt on the rising edge. A value of 10K for the resistor to give a reasonably slow charge time. The value of 10K is specified in code
121-
so adjust for your resistor I have 9830 ohms, and make sure to pick accurate resistors for voltage divider.
139+
The internal analogue comparator is used for this job.:
140+
141+
1. pin 6 PD6 AIN0
142+
2. pin 7 PD7 AIN1
143+
144+
We connect the reference voltage to the AIN1 pin (negative reference) and connect our capacitor to the AIN0 pin (positive reference) and then configure an interrupt on the rising edge. A value of 10K for the resistor to give a reasonably slow charge time. The value of ~ 10K is specified in code variable (resistance).
145+
so adjust for your resistor value , I have 9830 ohms. Make sure to pick accurate resistors for voltage divider.
122146

123-
The test returns the Capacitance in nF and time constant.
147+
The test returns the Capacitance result and time constant.
124148

125149
Project Schematic
126150
----------------------------

docs/CHANGELOG.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
* version 1.0-1 May 2018
44
first release
55

6-
* version 1.1-2 October 2018
7-
added help information to init screen
8-
9-
* version 2.0-1 December 2018
10-
Added third test, third button.
11-
12-
* Version 2.1-2 Feb 2019
13-
1. Improved and Optimized various displays and test results on the OLED screen.
14-
2. Removed Adafruit OLED graphic library declaration and defines from code, was never used since testing.
6+
* version 2.0-1 Feb 2019
7+
1. Added third test, third button.
8+
2. Improved and Optimized the information display on the OLED
9+
3. Status LED implented using nano onboard
10+
4. documentation and schematics improvements

images/sch.jpg

-12.3 KB
Loading

images/test2sch.jpg

-6.29 KB
Loading

images/test3sch.jpg

-11 KB
Loading

0 commit comments

Comments
 (0)