Arduino code example for Mikroe C-meter click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C-meter click is installed in socket #1 | |
// of Arduino Uno click shield | |
// Pulses will be applied on pin D2 | |
int pin = 2; | |
// We compute on float | |
float period; | |
float Cx; | |
unsigned long pulse; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(pin, INPUT); | |
} | |
void loop() | |
{ | |
period = 0; | |
// We compute the average of four measurements | |
// one of the reasons I choose float | |
Serial.println("Measurements"); | |
// Measure first period | |
pulse= pulseIn(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
if (pulse ==0){ | |
Serial.println("Pulse too long. reverting to pulseInLong()"); | |
pulse= pulseInLong(pin, HIGH, 1000000000); | |
Serial.println(pulse); | |
} | |
period += pulse; | |
pulse = pulseIn(pin, LOW, 100000000UL); | |
if (pulse ==0){ | |
Serial.println("Pulse too long. reverting to pulseInLong()"); | |
pulse= pulseInLong(pin, HIGH, 1000000000); | |
Serial.println(pulse); | |
} | |
period += pulse; | |
// Measure second period | |
pulse= pulseIn(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
if (pulse ==0){ | |
Serial.println("Pulse too long. reverting to pulseInLong()"); | |
pulse= pulseInLong(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
} | |
period += pulse; | |
pulse = pulseIn(pin, LOW, 100000000UL); | |
if (pulse ==0){ | |
Serial.println("Pulse too long. reverting to pulseInLong()"); | |
pulse= pulseInLong(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
} | |
period += pulse; | |
// Measure third period | |
pulse= pulseIn(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
if (pulse ==0){ | |
Serial.println("Pulse too long. reverting to pulseInLong()"); | |
pulse= pulseInLong(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
} | |
period += pulse; | |
pulse = pulseIn(pin, LOW, 100000000UL); | |
if (pulse ==0){ | |
Serial.println("Pulse too long. reverting to pulseInLong()"); | |
pulse= pulseInLong(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
} | |
period += pulse; | |
// Measure fourth period | |
pulse= pulseIn(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
if (pulse ==0){ | |
Serial.println("Pulse too long. reverting to pulseInLong()"); | |
pulse= pulseInLong(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
} | |
period += pulse; | |
pulse = pulseIn(pin, LOW, 100000000UL); | |
if (pulse ==0){ | |
Serial.println("Pulse too long. reverting to pulseInLong()"); | |
pulse= pulseInLong(pin, HIGH, 100000000UL); | |
Serial.println(pulse); | |
} | |
period += pulse; | |
// We divide only by four, as the above code measures | |
// Th and Tl separately | |
// and one period T = Th + Tl | |
period = period / 4; | |
Serial.print("Period is: "); | |
Serial.print(period); | |
Serial.println(" us"); | |
Serial.print("Frequency is: "); | |
Serial.print(1000000/period); | |
Serial.println(" kHz"); | |
Serial.println(""); | |
Cx = period * 1000 / 77816; | |
Serial.print("Capacity is: "); | |
Serial.print(Cx); | |
Serial.println(" uF"); | |
Serial.println(""); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment