Trying to measure capacitance by turning on a digital output, with R to input pin and C to gnd, and measure the time delay. Arduino Uno.
Using pulsein (see code below) I get timeouts if R=infinity (open) or if tie the input pin low, which is expected.
But if I tie the pulsein input pin to +5V, or use a RC (100k and 0.1uF) to the pulsein pin, I get timeouts, where I expect a number :~ 'scope signals look ok to me.
If I disconnect pulsein pin and touch (giving noise) I start to get wandering readings from pulsein. Otherwise, it's always timeouts, as if pulsein gets "stuck" or needs an edge?
#include <avr/io.h>
byte SensorPin = 3; // pin "D3", PD3
byte PulseOutPin = 2; // pin "D2", PD2
byte ledPin = 13;
#define TRUE 1;
#define FALSE 0;
//************************************************************************
void setup() {
digitalWrite(ledPin,LOW) ; // led off
digitalWrite(PulseOutPin, LOW);
pinMode(PulseOutPin, OUTPUT);
pinMode(SensorPin, INPUT);
Serial.begin(57600);
}
//========================================================================
void loop() {
unsigned long duration;
const unsigned long pulsein_timeout = 100000; // in usec
const unsigned long pulse_triptime = 5000; // in usec
digitalWrite(PulseOutPin, HIGH); //
duration = pulseIn(SensorPin, HIGH, pulsein_timeout);
if (duration==0) {
Serial.println("pulse timeout");
}
else {
Serial.print("tau= "); Serial.print(duration); Serial.print(" usec");
if (duration>=pulse_triptime) {
digitalWrite(ledPin,HIGH) ; // led on
Serial.print(" * trip *");
}
else {
digitalWrite(ledPin,LOW) ; // led off
}
Serial.println();
}
digitalWrite(PulseOutPin, LOW); //
delay(1000);
}