# Read in sine signal

**URL:** <https://forum.arduino.cc/t/read-in-sine-signal/972412>\
**Category:** Programming\
**Created:** [March 23, 2022, 8:32am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412 "2022-03-23T08:32:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![thedice](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@thedice](https://forum.arduino.cc/u/thedice)\
**Post date:** [March 23, 2022, 8:32am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/1 "2022-03-23T08:32:02Z")

</div>

I'm in the process of reading a sine signal. I connected the signal to A0. If I plot the values and then insert them into Matlab, the sine curve doesn't look particularly nice. Is there a better way than delay?

```cpp

void setup()
{

}

void loop()
{
  Serial.println(analogRead(A0));
  delay(200);
  
}

```

![Sine](https://europe1.discourse-cdn.com/arduino/original/4X/4/a/1/4a15cb1a341896b7cbb09fc1462763927b331ba2.png)

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [March 23, 2022, 8:35am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/2 "2022-03-23T08:35:30Z")

</div>

Is your input signal biased and AC coupled? It needs to be to be read in correctly.

Please show the circuit of the analogue input.

---

<div class="post-metadata">

**Author:** ![thedice](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@thedice](https://forum.arduino.cc/u/thedice)\
**Post date:** [March 23, 2022, 8:41am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/3 "2022-03-23T08:41:02Z")

</div>

The signal comes from an AD9833.  
 ![MAP008](https://europe1.discourse-cdn.com/arduino/original/4X/f/3/5/f35d5317daff789a504f2015357cf5613abd3bc1.jpeg)

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [March 23, 2022, 8:47am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/4 "2022-03-23T08:47:18Z")

</div>

> [@thedice](#):
>
> AD9833

Very nice, but not what I asked.

---

<div class="post-metadata">

**Author:** ![thedice](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@thedice](https://forum.arduino.cc/u/thedice)\
**Post date:** [March 23, 2022, 8:55am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/5 "2022-03-23T08:55:52Z")

</div>

> [@Grumpy\_Mike](#):
>
> Is your input signal biased and AC coupled? It needs to be to be read in correctly.

yes it is.

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [March 23, 2022, 9:28am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/6 "2022-03-23T09:28:52Z")

</div>

According to your picture, it is a 50Hz signal. That means 20ms per cycle. But you only read once every 200ms. For a reasonably good representation, you should read at least 10 times/cycle, i.e. at least every 2ms. This also requires a high baud rate, and you should use millis() for the timing.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 23, 2022, 10:24am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/7 "2022-03-23T10:24:20Z")

</div>

the [Nyquist–Shannon sampling theorem](https://www.sciencedirect.com/topics/computer-science/shannon-sampling-theorem) states that you must sample at at least twice the highest frequency required

---

<div class="post-metadata">

**Author:** ![anon35827816](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@anon35827816](https://forum.arduino.cc/u/anon35827816)\
**Post date:** [March 23, 2022, 10:29am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/8 "2022-03-23T10:29:10Z")

</div>

> [@thedice](#):
>
> the sine curve doesn't look particularly nice

Really? It looks pretty nice to me, really. Especially given the remarks made about sampling speed; if this was sampled at 200ms intervals and it's the same signal you showed on the scope trace, it's actually pure magic that you got this outcome.

Yeah, there are a few blips near the high and the low ends of the curve that might require looking into, and there's of course a pretty hefty distortion going on, hence the question from @Grumpy_Mike what the circuitry looks like.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 23, 2022, 11:28am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/9 "2022-03-23T11:28:54Z")

</div>

a 50Hz serial has a period of 20mSec so sampling every 200mSec would sample across multiple sine waves and give someting that looks like a valid result  
often seen examples where data was displayed which missed the important high frequency components  
the 50Hz signal should be sampled at least every 10mSec

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 23, 2022, 11:56am UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/10 "2022-03-23T11:56:19Z")

</div>

Read the code.

That's a serial plot, so it is basically a quick and dirty oscilloscope, the OP must be feeding it a low frequency signal.

Or nice aliasing.

OIC @horace

> often seen examples where data was displayed which missed the important high frequency components

a7

---

<div class="post-metadata">

**Author:** ![anon35827816](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@anon35827816](https://forum.arduino.cc/u/anon35827816)\
**Post date:** [March 23, 2022, 12:05pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/11 "2022-03-23T12:05:05Z")

</div>

> [@alto777](#):
>
> That's a serial plot, so it is basically a quick and dirty oscilloscope

Yeah I know and I read the code.

> [@horace](#):
>
> a 50Hz serial has a period of 20mSec so sampling every 200mSec would sample across multiple sine waves and give someting that looks like a valid result

Good point. Although for this to be true there would have to be a very nice and convenient shift in the delay so that the entire sine wave would be neatly sampled.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [March 23, 2022, 1:07pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/12 "2022-03-23T13:07:32Z")

</div>

> [@horace](#):
>
> 50Hz serial has a period of 20mSec so sampling every 200mSec would sample across multiple sine waves and give someting that looks like a valid result

No it won't. Have you ever looked at a signal that is sampled at the Nyquist rate. It looks absolutely nothing like the original signal.

This is a commonly misunderstood thing. At that rate you can just extract the information that a signal is at that frequency but it will look nothing like it. You need at least 20 samples per cycle to make it look like the signal would on an oscilloscope.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 23, 2022, 1:18pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/13 "2022-03-23T13:18:10Z")

</div>

Unless there was…

```
a very nice and convenient shift in the delay
so that the entire sine wave would be neatly sampled

```

a7

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [March 23, 2022, 1:21pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/14 "2022-03-23T13:21:59Z")

</div>

> [@Grumpy\_Mike](#):
>
> Have you ever looked at a signal that is sampled at the Nyquist rate. It looks absolutely nothing like the original signal.

But, it does contain all the necessary information to recreate the original signal ... given ideal Lowpass filtering.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [March 23, 2022, 1:23pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/15 "2022-03-23T13:23:41Z")

</div>

> [@thedice](#):
>
> Is there a better way than delay?

Always. It's millis().

Notice that with delay, the time used to take a sample becomes part of the sampling interval. So you will be sampling longer than 200ms. Using millis properly will fix that.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 23, 2022, 1:27pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/16 "2022-03-23T13:27:01Z")

</div>

> [@Grumpy\_Mike](#):
>
> No it won't. Have you ever looked at a signal that is sampled at the Nyquist rate. It looks absolutely nothing like the original signal.

200mSec is 10 times the 20mSec period of a 50Hz signal so sampling over multiple sine wave with a small time delay could give the appreaance of a valid signal.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 23, 2022, 1:27pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/17 "2022-03-23T13:27:47Z")

</div>

Here's a 50.2 Hz sine wave sampled at 10 times a second.

```cpp
void setup() {
  Serial.begin(9600);
  Serial.println("wake up!\n");
}

void loop() {
  float xx = sin(2 * PI * 50.2 * millis() / 1000.0);

  if (!(millis() % 100)) Serial.println(xx);

}

```

> **[alias.ino - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/326928242174853714)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

Turn on the serial plotter int the lower left hand cornet of the serial output window.

a7

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [March 23, 2022, 1:30pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/18 "2022-03-23T13:30:46Z")

</div>

The correct 50Hz intervals are visible in the scope photo in reply #5. Should be a clue to the correct sampling rate. 🙂

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [March 23, 2022, 1:32pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/19 "2022-03-23T13:32:36Z")

</div>

> [@Grumpy\_Mike](#):
>
> No it won't. Have you ever looked at a signal that is sampled at the Nyquist rate. It looks absolutely nothing like the original signal.

Keep in mind, these days it seems like everyone thinks it's okay to sample without any anti-aliasing filter. 😉

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [March 23, 2022, 3:03pm UTC](https://forum.arduino.cc/t/read-in-sine-signal/972412/20 "2022-03-23T15:03:49Z")

</div>

> [@horace](#):
>
> so sampling over multiple sine wave with a small time delay could give the appreaance of a valid signal.

Absolute rubbish.

I ask again, this again. Is this book learning or have you actually done the experiment and seen the result?  
I have done this many times, the first time back in 1974. It can **not** give you anything like a visually recognisable waveform.  
Do I have to drag out my equipment and post pictures before you will get it into your head?

[Next page](https://forum.arduino.cc/t/read-in-sine-signal/972412.md?page=2)
