// For: https://forum.arduino.cc/t/read-ac-frequency-with-arduino/1063205
#include <toneAC.h>
// Define the input pin for the pulse signal
// Define the input pin for the pulse signal
#define pulse_ip A1
// Declare variables to store the calculated values
unsigned long ontime, offtime, freq, period;
unsigned long count = 0;
void setup() {
// Set the input pin to input mode
pinMode(pulse_ip, INPUT);
// Initialize the serial monitor
Serial.begin(9600);
toneAC( 100000UL); // output 100kHz at pin 8 and 9
}
void loop() {
Serial.print( count++);
Serial.print( ",");
// Measure the on time and off time of the pulse signal
ontime = pulseIn(pulse_ip, HIGH);
offtime = pulseIn(pulse_ip, LOW);
// Calculate the period, frequency
period = ontime + offtime;
freq = 1000000.0 / period;
// Print the calculated values to the serial monitor/change to lcd later
Serial.print("Frequency: ");
Serial.print(freq);
Serial.print(" Hz");
Serial.print(", On time: ");
Serial.print(ontime);
Serial.print(" us");
Serial.print(", Off time: ");
Serial.print(offtime);
Serial.println(" us");
// Wait for half second before repeating the process
delay(500);
}