# How to obtained the frequency of a signal coming through serial port

**URL:** <https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574>\
**Category:** Science and Measurement\
**Created:** [January 22, 2024, 9:01pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574 "2024-01-22T21:01:35Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [January 24, 2024, 8:07pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/22 "2024-01-24T20:07:48Z")

</div>

How detailed of a wave shape?  
I'm guess you need at least 10 samples per wave to have a reasonable idea of the wave shape. That comes to a 1.6 MHz ADC sampling frequency.  
If you want to push all that data to a PC real time, at 8 bit resolution, you need 1.6 MB/s transfer rate - one byte per data point. Well within reach of a USB connection, the limiting factors are likely to be the sending and receiving ends.  
Way, way out of the realm of an Arduino. Teensy, I don't know. RPi can handle the data rate but will need an external ADC to sample the signal.

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [January 25, 2024, 6:09am UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/23 "2024-01-25T06:09:30Z")

</div>

> [@engineeramjad](#):
>
> and displaying it on serial monitor. (I am using Arduino Uno)

> [@engineeramjad](#):
>
> I need frequency and wave shape.

In short that idea will not work on the Uno because of the Nyquist criterion.

---

<div class="post-metadata">

**Author:** ![engineeramjad](https://avatars.discourse-cdn.com/v4/letter/e/ce7236/32.png) [@engineeramjad](https://forum.arduino.cc/u/engineeramjad)\
**Post date:** [January 25, 2024, 11:30am UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/24 "2024-01-25T11:30:38Z")

</div>

Thank you, @johnerrington  
Could you please suggest an Arduino board that I can use to observe the frequency variations of the signal? Also, the signal has a frequency of 500 kHz instead of 166 kHz, as shown in the figure below. Thank you

 ![Capture.PNG](https://europe1.discourse-cdn.com/arduino/original/4X/6/0/4/6046df3ea6aa5cc3f0427b7b44db993487613b5a.jpeg)  
.

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [January 25, 2024, 4:11pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/25 "2024-01-25T16:11:05Z")

</div>

You COULD use an F-V converter; however then you wont see the wave shape.

Alternatively you could measure the frequency, but you would need to condition the signal to match a digital input. (post #16)

> [@engineeramjad](#):
>
> The circuit I am using consists of an opamp with a specific frequency.

Would you care to share that circuit? (post #18)

> [@How to make a schematic you can post.](https://forum.arduino.cc/t/how-to-make-a-schematic-you-can-post/675103):
>
> When you submit a question on the forum you will usually need to add a schematic (circuit diagram). The tutorial that follows will explain some ways to create a useful image; however lets start by explaining how to add the image to your post. Its easy. Having created the image all you need to do is : Save the image somewhere convenient - maybe your desktop Start your post, and place the cursor (insertion point) where you want the image to be shown; [upload image to forum] 3: click th…

> [@engineeramjad](#):
>
> ` Serial.begin(115200);`

How can you display a 500kHz signal with a baud rate of 100kHz?

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [January 25, 2024, 4:16pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/26 "2024-01-25T16:16:57Z")

</div>

As I said before an Uno or Nano can happily measure those frequencies, as long as the amplitude is within range. I've done over ten times that.

Use the signal as clock source; count the ticks for a period of time; calculate frequency.

---

<div class="post-metadata">

**Author:** ![engineeramjad](https://avatars.discourse-cdn.com/v4/letter/e/ce7236/32.png) [@engineeramjad](https://forum.arduino.cc/u/engineeramjad)\
**Post date:** [January 30, 2024, 12:05pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/27 "2024-01-30T12:05:48Z")

</div>

Hello @wvmarle .  
Could you please elaborate it a little more. for example what do you mean by using the signal as a clock source?

I am new in Arduino, so need a little more explanation.

Thank you

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [January 30, 2024, 1:03pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/28 "2024-01-30T13:03:44Z")

</div>

Full explanation and details of Timer1 settings you can find in the [ATmega328p datasheet](https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf), chapter 15, page 89.  
The clock source is explained in chapter 16, page 113.

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [January 31, 2024, 6:45am UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/29 "2024-01-31T06:45:34Z")

</div>

> [@engineeramjad](#):
>
> I need frequency and wave shape.

The OP has never yet been clear about their requirements, nor about the source of the "signal" thats maybe 150k or 500k.

Of course as you say you can measure the frequency of a **constant** signal with a uno or nano, as it only requires 2 samples per period.  
Getting the wave shape isnt so straightforward, especially if the shape or frequency vary, dpepending on the bandwidth and required accuracy.  
Displaying it in real time adds another dimension.

---

<div class="post-metadata">

**Author:** ![engineeramjad](https://avatars.discourse-cdn.com/v4/letter/e/ce7236/32.png) [@engineeramjad](https://forum.arduino.cc/u/engineeramjad)\
**Post date:** [January 31, 2024, 2:01pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/30 "2024-01-31T14:01:33Z")

</div>

Hello everyone.  
I designed a pressure sensor. The pressure sensor is connected to interfacing circuit, which is basically Opamp circuit. The interfacing circuit (opamp) output is connected to the oscilloscope, as shown below. As I bring close my finger to the sensor its waveform width as well as frequency is get changed, which I wanted, as shown in Figure 1. The sensor operating frequency is 166kHz. (An improved version is also designed which is operating at 500kHz).  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/f/0/d/f0d582a7f93c9042a1fc8cf7123de113b0d73420.png)  
Figure 1

The parameter which I needed is operating frequency and waveform, which is visible with oscilloscope. However, now my target is to replace the oscilloscope with Arduino. My initial setup is:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/e/2/2/e22d538666abc087bc84bfc137c167e016ed7d0a.png)  
Figure 2.

In the above Figure, the interfacing circuit output is going in Arduino pin A0. I wrote few lines simple code to read analog pin A0 as:

```arduino
unsigned long runcount = 0;

void setup()

{

  Serial.begin(115200);
}

void loop() {
  
    float val = analogRead(A0);
    float Voltage = val * (5.0 / 1023.0);
    Serial.println(Voltage);
    micros();
}

```

The data coming through serial monitor, which is in the form of 0,0.1,0.2,.. 0.4. Then I used processing to record the data for 1 minute. A mobile stop watch was used for 1 minute time, the processing was manually stopped as one minute is completed on mobile stopwatch. The recorded data is drawn in the MATLAB which is giving half peaks as:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/d/6/6d69f446e5279e59cb180963eeb8c7a4064febd2.png)

Figure3: All data drawn in the MATLAB.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/3/4/63446e22fb86b1a2cbeb21f71ed1fae3c39e4e60.png)  
Figure 4: zoomed view of section from 0 to 0.7 of the above figure 3.

In the zoomed view, I noticed that the waveform is not matching with the oscilloscope (look like half wave). I used findpeak function in the mutable and find all peaks in the figure 3. Then divide the number of peaks by 60 (as the collected data was for 1 minuet) which is giving to me 80.14Hz. This is look to me incorrect, I am trying to correct my code by reading blogs, but I could not figure it out. Any suggestion or guideline will be highly apricated. I am at beginner level in the Arduino, so will need a little more detail. Thank you.

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [January 31, 2024, 5:04pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/31 "2024-01-31T17:04:04Z")

</div>

For this to work you need to sample at more than twice the highest frequency of the wave.  
Any lower and you will see an "aliased" signal.  
However to see the wave shape you need to sample at a MUCH higher rate.  
its explained here

> **[skillbank: Introduction to Data conversion](https://skillbank.co.uk/SignalConversion/rate.htm)**
>
> An introduction to acquiring, sampling and measuring signals with a microcomputer

Your "sampling rate" is limited by the serial interface. You seem to be gatteing data of the form 0.xx which \_with a delimiter etc) is 5+ characters so the data rate is less than 115000/5 = 23kHz.

To be clear the limitation is the serial interface. Basically I dont see how you can do it that way, you need to use a faster interface.  
However to see anything in real time resembling the true shape for 500kHz you need to sample at or above 10MHz.

You need to think again. Or just get one of these

[https://www.amazon.co.uk/Hantek-Digital-Storage-Oscilloscope-Channel/dp/B01M74UKKC?source=ps-sl-shoppingads-lpcontext&ref\_=fplfs&psc=1&smid=A1Y0G7ZSK431JL](https://www.amazon.co.uk/Hantek-Digital-Storage-Oscilloscope-Channel/dp/B01M74UKKC?source=ps-sl-shoppingads-lpcontext&ref_=fplfs&psc=1&smid=A1Y0G7ZSK431JL)

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [February 1, 2024, 2:26pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/32 "2024-02-01T14:26:01Z")

</div>

> [@engineeramjad](#):
>
> In the zoomed view, I noticed that the waveform is not matching with the oscilloscope (look like half wave). I used findpeak function in the mutable and find all peaks in the figure 3. Then divide the number of peaks by 60 (as the collected data was for 1 minuet) which is giving to me 80.14Hz.

Which has been discussed in detail in the thread above. This latest post of yours makes me feel like you didn't read these replies. It also details why you can NOT do what you're trying to do with an Arduino.

> [@](#):
>
> I am at beginner level in the Arduino, so will need a little more detail. Thank you.

This is a very advanced project.  
Even the basic frequency counting that I pointed out to you I would definitely call advanced level.  
Sampling and processing a 500 kHz waveform with any detail (you haven't told us ANYTHING about this waveform, how it changes and how your pressure data is encoded in it) requires something like a 5 MHz sampling rate. I call myself pretty advanced in Arduino and general electronics but I have no idea at the moment how to tackle that problem, short of buying a USB scope and do the data (waveform, frequency) processing on a PC - or maybe an RPi if you need to keep the physical size small.

---

<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:** [July 30, 2024, 2:26pm UTC](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574/33 "2024-07-30T14:26:26Z")

</div>

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

[Previous page](https://forum.arduino.cc/t/how-to-obtained-the-frequency-of-a-signal-coming-through-serial-port/1214574.md?page=1)
