#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,20,4);
const int OUT_PIN = A2;
const int IN_PIN = A0;
const float IN_STRAY_CAP_TO_GND = 24.48;
const float IN_CAP_TO_GND = IN_STRAY_CAP_TO_GND;
const float R_PULLUP = 34.8;
const int MAX_ADC_VALUE = 1023;
void setup()
{
pinMode(OUT_PIN, OUTPUT);
pinMode(IN_PIN, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop()
{
pinMode(IN_PIN, INPUT);
digitalWrite(OUT_PIN, HIGH);
int val = analogRead(IN_PIN);
digitalWrite(OUT_PIN, LOW);
**if (val < 1000)**
** {**
** pinMode(IN_PIN, OUTPUT);**
** float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - **
**val);**
lcd.setCursor(0,0);
lcd.print("Scale: 1pF-1nF");
lcd.setCursor(0,1);
lcd.print(capacitance,3);
lcd.setCursor(14,1);
lcd.print("pF");
}
**else**
** {**
** pinMode(IN_PIN, OUTPUT);**
** delay(1);**
** pinMode(OUT_PIN, INPUT_PULLUP);**
** unsigned long u1 = micros();**
** unsigned long t;**
** int digVal;**
** do**
** {**
** digVal = digitalRead(OUT_PIN);**
** unsigned long u2 = micros();**
** t = u2 > u1 ? u2 - u1 : u1 - u2;**
** } **
** **
** while ((digVal < 1) && (t < 400000L));**
** pinMode(OUT_PIN, INPUT); **
** val = analogRead(OUT_PIN);**
** digitalWrite(IN_PIN, HIGH);**
** int dischargeTime = (int)(t / 1000L) * 5;**
** delay(dischargeTime); **
** pinMode(OUT_PIN, OUTPUT); **
** digitalWrite(OUT_PIN, LOW);**
** digitalWrite(IN_PIN, LOW);**
** float capacitance = -(float)t / R_PULLUP / log(1.0 - (float)val / (float)MAX_ADC_VALUE);**
lcd.setCursor(0,0);
lcd.print("Scale: 1pF-1nF");
if (capacitance > 1000.0)
{
lcd.setCursor(0,1);
lcd.print(capacitance / 1000.0, 2);
lcd.setCursor(14,1);
lcd.print("uF ");
}
else
{
lcd.setCursor(0,1);
lcd.print(capacitance);
lcd.setCursor(14,1);
lcd.print("nF");
}
}
while (micros() % 1000 != 0);
}
This is quite basic programming so it looks like you are just beginning with Arduino.
Just to start it off:
The first line is an 'if' statement. If the variable 'val' has a value of less than 1000 the code between the braces '{' '}' is executed.
The pinMode function can set a pin as INPUT, OUTPUT etc. In this case. the rather oddly named IN_PIN is defined as an output pin and can be set in the program (sketch) by the digitalWrite() function.
The question was about the use of "double stars".
what about these lines?
else
{
pinMode(IN_PIN, OUTPUT);
delay(1);
pinMode(OUT_PIN, INPUT_PULLUP);
unsigned long u1 = micros();
unsigned long t;
int digVal;
do
{
digVal = digitalRead(OUT_PIN);
unsigned long u2 = micros();
t = u2 > u1 ? u2 - u1 : u1 - u2;
}
while ((digVal < 1) && (t < 400000L));
pinMode(OUT_PIN, INPUT);
val = analogRead(OUT_PIN);
digitalWrite(IN_PIN, HIGH);
int dischargeTime = (int)(t / 1000L) * 5;
delay(dischargeTime);
pinMode(OUT_PIN, OUTPUT);
digitalWrite(OUT_PIN, LOW);
digitalWrite(IN_PIN, LOW);
float capacitance = -(float)t / R_PULLUP / log(1.0 - (float)val / (float)MAX_ADC_VALUE);
No, sir. It's for the line with double star. I was trying to bold the lines but that didn't happen instead double star appeared.
Its a bit impolite to post code without saying where it came from. (Ideally with a link, like this)
https://www.circuitbasics.com/how-to-make-an-arduino-capacitance-meter/
Perhaps you are trying to understand HOW the capacitance is meassured and calculated, rather than what the code instructions (eg if(val<1000) do?
If so I'd suggest you refer back to the excellent and detailed information provided.
Sorry, I did not know about that. Thank you for letting me know.
I was just trying to understand how the mentioned part of the code works.
It looks like you really need to start with some elementary Arduino tutorials to get a basis for understanding that program.
Some of the statements and functions are pure C++ and you will find these by a Google type search. For example, the if
statement as in if (val < 1000)
Other statements or functions are specific to "Arduino", for example digitalWrite()`` as in
digitalWrite(IN_PIN, HIGH);`. These are in the Arduino reference manual:
Measuring how fast a capacitor charges or discharges through a known resistor will allow you to calculate the capacitance. It takes longer to charge/discharge a larger capavitor.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.