# Detecting frequency of square wave

**URL:** <https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572>\
**Category:** Sensors\
**Created:** [May 30, 2022, 6:11pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572 "2022-05-30T18:11:55Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaja](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zaja](https://forum.arduino.cc/u/zaja)\
**Post date:** [May 30, 2022, 6:11pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/1 "2022-05-30T18:11:55Z")

</div>

> [@Detecting Microsecond Pulse Duration](https://forum.arduino.cc/t/detecting-microsecond-pulse-duration/697522/10):
>
> Why did you switch from digital to analog reads? If your input signal reliably rises to the logic high state of the device you're using, I'd go back to digital read. I may not be understanding the whole purpose of your code, but I'd try something like: void setup() { &nbsp;unsigned long startTime = 0; &nbsp;unsigned long stopTime = 0; } void loop() { &nbsp;while (digitalRead(7)) { &nbsp;//wait for low on pin 7 &nbsp;} &nbsp;while (!digitalRead(7)) { //wait for high on pin 7 &nbsp;} &nbsp;startTime = micros(); &nbsp; &nbsp; //high detec…

I am using the above linked code to measure frequency of a square wave input to one of the digital pins of an Arduino Uno. The program works perfectly to detect accurate frequencies of square wave signals generated using a function generator. The issue comes when I use my hardware generated square wave and the same program starts giving infinite frequency values. For reference, both square wave signals look identical on an oscilloscope.

Would anybody have any thoughts on what I could be missing here?

The scope shows the two identical waveforms:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/d/8/d/d8d382c77b521d5dd29a1d61e87eaafc4cbb59ea.jpeg)

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [May 30, 2022, 6:19pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/2 "2022-05-30T18:19:11Z")

</div>

> [@zaja](#):
>
> both square wave signals look identical on an oscilloscope

Also when you zoom in on the transients?

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [May 30, 2022, 6:22pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/3 "2022-05-30T18:22:29Z")

</div>

> [@zaja](#):
>
> infinite frequency values

That would be a cycle of zero length. Perhaps there is a coding problem.

```arduino
void setup() 
{
  unsigned long startTime = 0;  
  unsigned long stopTime = 0; 
}

```

This 'setup()' does nothing because it only initializes variables that are local to setup();

Missing: `pinMode(7, INPUT);`

How long are your cycles? Each digitalRead() takes about 6 microseconds and 'micros()' counts by 4.

---

<div class="post-metadata">

**Author:** ![zaja](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zaja](https://forum.arduino.cc/u/zaja)\
**Post date:** [May 30, 2022, 6:38pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/4 "2022-05-30T18:38:23Z")

</div>

Thanks for your response, John.

You are right. I had to do a global initialization for startTime and stopTime to run the code. I also added pinMode(7,INPUT) to the setup() but still keep seeing 62500 as the frequency which makes me think the time period it's recording is 0. I am using a 0.79Hz square wave as an input.

I noticed that it starts giving the right output for higher frequencies (~1kHz) but not able to do so for lower frequencies that I need it to work for (0.5Hz-1Hz).

---

<div class="post-metadata">

**Author:** ![zaja](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zaja](https://forum.arduino.cc/u/zaja)\
**Post date:** [May 30, 2022, 6:38pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/5 "2022-05-30T18:38:57Z")

</div>

Yes, they are identical

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 30, 2022, 6:56pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/6 "2022-05-30T18:56:08Z")

</div>

> [@zaja](#):
>
> I am using the above linked code to measure frequency

No, you're not, because that code cannot compile.

So, where is your code?

---

<div class="post-metadata">

**Author:** ![zaja](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zaja](https://forum.arduino.cc/u/zaja)\
**Post date:** [May 30, 2022, 7:00pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/7 "2022-05-30T19:00:19Z")

</div>

Sorry this is my first time posting. Hope this format works:

unsigned long startTime;  
unsigned long stopTime;  
float time\_period;  
float frequency;

void setup() {  
Serial.begin(9600);  
startTime = 0;  
stopTime = 0;  
pinMode(2, INPUT);  
}

void loop() {  
// Serial.println(digitalRead(2));

while (digitalRead(2)) { //wait for low on pin 2

}  
while (!digitalRead(2)) { //wait for high on pin 2

}

startTime = micros(); //high detected, set start time

while (digitalRead(2)) { //wait for low on pin 2

}  
stopTime = micros(); //low detected, set stop time

time\_period = 2\* (stopTime - startTime);  
frequency = 1000000/time\_period;

Serial.println(frequency);

}

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 30, 2022, 7:03pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/8 "2022-05-30T19:03:00Z")

</div>

> [@zaja](#):
>
> Hope this format works:

Nope.  
Use code tags.

---

<div class="post-metadata">

**Author:** ![zaja](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zaja](https://forum.arduino.cc/u/zaja)\
**Post date:** [May 30, 2022, 7:03pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/9 "2022-05-30T19:03:33Z")

</div>

```cpp

unsigned long startTime;
unsigned long stopTime;
float time_period;
float frequency;

  
void setup() {
  Serial.begin(9600);
  startTime = 0;
  stopTime = 0;
  pinMode(2, INPUT); 
}

void loop() {
 // Serial.println(digitalRead(2));
  
  while (digitalRead(2)) { //wait for low on pin 2

  }
  while (!digitalRead(2)) { //wait for high on pin 2

  }

  startTime = micros(); //high detected, set start time

  while (digitalRead(2)) { //wait for low on pin 2

  }
  stopTime = micros(); //low detected, set stop time

  time_period = 2* (stopTime - startTime);
  frequency = 1000000/time_period;
  
  Serial.println(frequency);
   
  
}

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [May 30, 2022, 7:13pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/10 "2022-05-30T19:13:15Z")

</div>

> [@zaja](#):
>
> still keep seeing 62500 as the frequency which makes me think the time period it's recording is 0.

Don't you mean 16? 62500 Hz is a 16 microsecond cycle time.

0.79 Hz should be a duration of 1,265,822 microseconds.

The "(Warning: untested and incomplete armchair code fragment. Worth what you paid for it...)" that you quoted above only measures the high part of the wave. If it's a square wave then you would just need to divide the frequency by 2. If it is a pulse wave you will be off by some unknown factor.

Here is the code with some deficiencies corrected:

```arduino
unsigned long startTime = 0;
unsigned long stopTime = 0;

void setup()
{
  Serial.begin(115200);
  delay(200);

  pinMode(7, INPUT);

  Serial.println();
  Serial.println("Starting");
}

void loop()
{
  while (digitalRead(7) != LOW) //wait for low on pin 7
  {
  }

  while (digitalRead(7) == LOW) //wait for high on pin 7
  {
  }

  startTime = micros(); //high detected, set start time

  while (digitalRead(7) != LOW) //wait for low on pin 7
  {
  }

  while (digitalRead(7) == LOW) // wait for end of low on pin 7
  {
  }

  stopTime = micros(); // low detected, set stop time

  unsigned long elapsedTime = stopTime - startTime;
  Serial.print("Duration in microseconds: ");
  Serial.println(elapsedTime);

  Serial.print("Frequency in Hz: ");
  Serial.println(1000000.0 / elapsedTime);

  delay(3000); // Display every 3 seconds
}
```

---

<div class="post-metadata">

**Author:** ![zaja](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zaja](https://forum.arduino.cc/u/zaja)\
**Post date:** [May 30, 2022, 7:18pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/11 "2022-05-30T19:18:17Z")

</div>

Thanks, John. I just tried this code out. This also works perfectly for the 0.79Hz square wave generated using a function generator but not for the one generated using the hardware ☹

If I increase the frequency to 1kHz, it starts working for hardware generated square wave too. I wonder if it's something related to my sampling rate that might be incorrect?

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [May 30, 2022, 7:44pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/12 "2022-05-30T19:44:24Z")

</div>

> [@zaja](#):
>
> This also works perfectly for the 0.79Hz square wave generated using a function generator but not for the one generated using the hardware ☹

My best guess is the "the hardware" is faulty in some way. Does "the hardware" share a Ground with the Arduino?

---

<div class="post-metadata">

**Author:** ![zaja](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zaja](https://forum.arduino.cc/u/zaja)\
**Post date:** [May 30, 2022, 7:46pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/13 "2022-05-30T19:46:36Z")

</div>

Yes it shares a ground. If I look at the serial data coming in from the hardware, that's also a clean square wave. Not sure what's wrong here

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 26, 2022, 7:46pm UTC](https://forum.arduino.cc/t/detecting-frequency-of-square-wave/997572/14 "2022-11-26T19:46:55Z")

</div>

Closed on November 26, 2022, 7:46pm UTC

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