// Define pin for 555 timer output
const int pulsePin = 2; // Interrupt pins on Arduino Uno are pin 2 (INT0) and pin 3 (INT1)
const float knownCapacitance = 0.000001; // 1uF known capacitance
const float knownResistorR1 = 10000; // 10kOhm known resistor (R1)
// Variables for timing
volatile unsigned long lastTime = 0; // Last interrupt time
double period=0;
void setup() {
// Start serial communication
Serial.begin(9600);
// Set the pulsePin as input
pinMode(pulsePin, INPUT);
// Attach interrupt to pulsePin (for both rising and falling edge detection)
attachInterrupt(digitalPinToInterrupt(pulsePin), handlePulse, RISING);
}
void loop() {
if (period>0)
{
float frequency = 1000000.0 / period;
Serial.println(period);
Serial.print("Frequency: ");
Serial.print(frequency);
Serial.println(" Hz");
float unknownResistance = (0.72/(frequency*knownCapacitance))-(knownResistorR1/2);
Serial.print("Unknown Resistance: ");
Serial.print(unknownResistance);
Serial.println(" ohms");
period=0;
}
delay(1000); // Wait a second before next reading
}
// Interrupt Service Routine (ISR) to handle pulse changes
void handlePulse() {
unsigned long currentTime = micros(); // Get the current time in microseconds
period=currentTime-lastTime;
lastTime=currentTime;
}