---
# System prepended metadata

title: Notes with an arduino and an oscilloscope

---

# Analyzing sound notes with an arduino and an oscilloscope


This showcase is for showing different frecuencies that can be outputed as sound. This is for showcasing squared waves and properties like period and frequency. 

This is don with a Rohde&Scwarz HMO1202 Series Oscilloscope so I'm refering to specific buttons on that oscilloscope but this should be also helpful for other analog or electronic oscilloscopes.


![image](https://hackmd.io/_uploads/BJiRyfXFee.png)
_A4 440Hz image_

### Preparation

Materials needed:

* Oscilloscope
* Arduino connected to the power
* Speaker/buzzer (Better with a switch so it can be switched on/off at will)
* Wires

The schematics is the following:

![image](https://hackmd.io/_uploads/ryP7-Dztgx.png)

This is the connection live with the oscilloscope (With no buzzer)

![image](https://hackmd.io/_uploads/H1bfF-mKll.png)

For this connection is better to have a breadboard. 

#### Code

This is a code that just outputs some sound in the digital pin 8, changes its frequency and then stops in a loop. You can change the pin or the time using the define at the beginning.

```C++=
#define TIME 3000 // 3 seconds
#define PIN 8

void setup() {
//empty
}

void loop() {
  tone(PIN, 440, TIME); //A4 because for people that knows a bit of music is a nice catch up
  delay(TIME);
  noTone(PIN);
  delay(TIME);
  tone(PIN, 1000, TIME); //period of 1 ms that is easy to see in the oscilloscope
  delay(TIME);
  noTone(PIN);
}

```

#### Setup of the oscilloscope

Ok, once we are set up we need to use the cursors (division of volts, time division) to arrive to these:

:::success
For having the information (the tr, Vp, etc) displayed I press "Quick view" in the analyze panel. 
:::


![image](https://hackmd.io/_uploads/BJiRyfXFee.png)
_A4 440Hz image_

![image](https://hackmd.io/_uploads/ByWWxGmYee.png)
_the nothing image_

![image](https://hackmd.io/_uploads/SkbflzXtll.png)
_1000 Hz image_


::: info

I have seen many noise in the signal, is the Arduino wrong?

Depends on what do you mean by noise in the signal, but Arduino should handle pretty neat signals. Probably there is something wrong with the wires. 

:::

### Voltage (vertical axis) analysis

We can see that in the 2 "sounds" the voltage goes from 0 to 5 volts (each division is 1 volt). If we press quick view we can see that we have some imprecisions, it's not exactly 5 volts but 5.12V and the mean is that value over 2 (2.56, 2.57).

This is the Voltage of the arduino (and the USB), if we used other boards the voltage would be different. 

The mean is half of the value of the peak voltage because we want to stay up the same time as it is down. In other cases (such us using PWM) this may differ.

### Period and frequency  (horizontal axis) analysis


:::info
:information_source: These photos are done with the "quick view" pressed, but for the first time students is probably better to turn it off so there is less information on the screen. Also we are going to make some simple calculations that are done by this button.
:::

Info that you can get from here:

These are "squared" waves. They go from up to down or from down to up in a glimpse (actually you can si that is less than a micro second).

The relationship of the period/frequency can be seen better in the image of the 1000Hz sound

![image](https://hackmd.io/_uploads/SkbflzXtll.png)
_1000 Hz image_


The divisions (TB) is 500 microseconds, a full wave is up and down and it takes 2 of these 500 microseconds to be done, so a total of 1 millisecond. This is the period of the wave that is repeated over an over. 

Here is a good moment to remember that 

$$Frequency = \frac{1}{Period}$$

So if the period is $1$ millisecond, that is $10^{-3}$ seconds:

$$Frecuency =  \frac{1}{10^{-3}} = 10^{-(-3)} = 10 ^3 = 1000 Hz$$

If we want to confirm we can try to manually find what is the period of the wave of 440 Hz.

First we do the calculations

$$Period = \frac{1}{Frecuency} = \frac{1}{440 Hz} = 0.002\overset{ \huge\frown}{27} s= 2.\overset{ \huge\frown}{27}·10^{-3} s = 2.\overset{ \huge\frown}{27} ms$$

Then we measure in the oscilloscope (we may want to have only one signal for that but we can handle the change)

![image](https://hackmd.io/_uploads/BJiRyfXFee.png)
_A4 440Hz image_

Here we can see the divisions of 500 microseconds (0.5 mill). If we count from the center we see that we need more than 4 divisions (2 milliseconds) but less than 5 divisions (2.5 milliseconds). So the period is between these two values.

If we use the time division knob we can make some zoom on the signal. If we press the "run|stop" it will be even easier beacause the signal will halt in the moment that we press stop.  

![image](https://hackmd.io/_uploads/r19gOfXteg.png)

With these divisions we have 5 and a half (maybe a bit more) on the 5 volts line and the same in the other side. So we have around 11 divisions and a bit more. That's 11*200 microseconds, the total is around the 2.2 milliseconds that we just calculated.

:::info
Also the oscilloscope in the bottom part does in the quick view this analyisis with precision and it analyses the frequency and the period. It can be turned on/off by pressing "quick view"
:::

### Extra information from this setup

This is a bit more advance but we can see that we can do a little bit of zoom to check that the up and down is very fast but it's not "instant"

If we zoom in we can see how the peak is done. It all happens in a nanosecond but there it's not a straight line either

![image](https://hackmd.io/_uploads/SkF7hfmteg.png)

### Variants

Use one of the arduino song from Robson Couto 

https://github.com/robsoncouto/arduino-songs

It's going to be more varied in the sound but it's better to see a full melody. Also remember that Robson Couto default buzzer pin is 11 so change the pin in the code or phisically.
