Read AC frequency with arduino

Hello, I am trying to make a program that will display AC frequency using the circuit attached bellow. So far I have done some programming but it seems that above 15kHz something is not functioning properly since tinkercad serial monitor is not printing anything. Also if my voltage is less than 5V the output will show 0Hz unless a DC off set is in place. Anyone could give me a hint or point me in the right direction?

The code I am using:
"

// 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;

void setup() {
  // Set the input pin to input mode
  pinMode(pulse_ip, INPUT);

  // Initialize the serial monitor
  Serial.begin(9600);
}

void loop() {
  
  
  // Measure the on time and off time of the pulse signal
  ontime = pulseInLong(pulse_ip, HIGH);
  offtime = pulseInLong(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.println(" Hz");
  Serial.print("On time: ");
  Serial.print(ontime);
  Serial.println(" us");
  Serial.print("Off time: ");
  Serial.print(offtime);
  Serial.println(" us");

  // Wait for half second before repeating the process
  delay(500);
}

"

For one thing, you are not measuring an AC frequency. But, thank you for properly posting code!
I guess I cannot help with tinkercad problems.

By doing this:

the code has set the pin to digital mode. It will therefore only read a high or low rather than an analog voltage level. In addition, the emulator may be treating the signal state thresholds a bit differently to a real Arduino. Sorry, can't really help with Tinkercad.

Welcome to the forum.

Why the weird circuit ? In Tinkercad I made a normal digital signal:


It does indeed stop at a higher frequency.
Let's blame Tinkercad and see if we are right :face_with_raised_eyebrow:

The Wokwi simulator does not have a function generator and can have only one Arduino board in a project. Therefor I used the toneAC library. It uses a hardware timer to output a frequency, so it does not use a interrupt.


That's better, it does not stop.
At 100kHz, the pulseIn() works better than pulseInLong(), but both are not accurate at 100kHz.
Here is the Wokwi project: FrequencyMeasurePulseIn.ino - Wokwi Arduino and ESP32 Simulator

1 Like

Thank you! The weird circuit because this is what i need to use , first diode for receiving only positive input and the Zener diode for protecting the pin against overcharging

There is nothing that pulls the signal at A1 down.

The FreqMeasure library suggests a input circuit: https://www.pjrc.com/teensy/td_libs_FreqMeasure.html
It starts with a transistor, then a digital buffer to shape the signal and optional a filter with another digital buffer.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.