recent
Hot News

Playing Musical Notes With an Arduino Board

Home

 



In this tutorial, we will learn how to make noise with an Arduino / Genuino board! As a bonus, we will go back in time with a retro music player. What could be better than a tune of Tetris to brighten up your day!

Hello everyone !

If you ask a maker what they like to do, they are sure to tell you that they like to tinker, make edits that flash and / or make music (or noise, depending on the point of view).

We love all the things that glow, flash and even more the things that make music (except when it comes to the toy of the little brother / nephew / neighbor's son, in this case it's just very annoying).

In my previous articles, we saw a lot of things together, but nothing that would add a sound dimension to a project. It is high time to right this injustice.

Make music with tone ()

In this chapter, we will study the basics of sound generation with an Arduino / Genuino board. To do this, we are going to make a test setup that we will use throughout this tutorial.

N.B. We are only going to generate sounds in this tutorial, but it is quite possible to use the information below to generate signals for a completely different purpose.

The test set-up


Necessary material

To carry out this assembly, we will need:
  • An Arduino UNO board (and its USB cable),
  • An 8 ohm loudspeaker (headphone loudspeaker for example),
  • A 100 ohm resistor (brown / black / brown),
  • A test plate and wires to wire our assembly.
Schematic view of the assembly

Prototyping view of the assembly


To make this circuit, you have to start by connecting one of the speaker pins to pin D9 of the Arduino board. The other speaker pin should be connected to one end of the 100 ohm resistor.

N.B. It is possible to use any digital or analog spindle. The choice of pin D9 for this tutorial is completely arbitrary, you are free to choose another pin if you wish.

The finished assembly

We then complete the circuit by connecting the other end of the 100 ohm resistor to the GND pin of the Arduino board with a wire.

The code

Generating sound with an Arduino / Genuino board is as easy as calling the tone () function in a piece of code.

The code below allows for example to generate a La3 at 440Hz, like a tuning fork, but with an Arduino board:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/**
 * Arduino tone example().
 */
 
const byte PIN_BUZZER = 9;

void setup() {
  pinMode(PIN_BUZZER, OUTPUT);

  // Note "La3" 440Hz
  tone(PIN_BUZZER, 440); 
}

void loop() {
  
}

The tone () function generates a square wave (50% of the time at 5 volts, 50% of the time at 0 volts) of a given duration and frequency.

1  tone(int broche, unsigned int fréquence);
2  tone(int broche, unsigned int fréquence, unsigned long durée);

The tone () function takes as arguments two mandatory parameters and one optional parameter. This function does not return any value.

The mandatory parameters are the pin number on which to generate the signal and the signal frequency in Hertz (between 31Hz and 65535Hz for all classic Arduino boards based on an AVR microcontroller).

A duration (optional) can be specified to stop the generation of the signal after a certain number of milliseconds. If the duration is not specified, the signal is generated indefinitely until a call to the noTone () function, presented in the next chapter.

N.B. The tone () function is non-blocking, this means that it does not block the program for the specified duration. If you want to make a retro music player for example (for which an example code is presented as a bonus, as if by chance;)), you will have to add a delay () after the call to tone () to have the right tempo.


Known limitations of tone ()

The tone () function has several known limitations:
  • only one signal can be generated at a time, on a single pin,
  • a call to tone () on one pin when a signal is generated on another pin has no effect,
  • a call to tone () on one pin when a signal is generated on the same pin immediately changes the frequency of the signal,
  • on an Arduino UNO board, the use of tone () makes it impossible to use pins D3 and D11 in PWM with analogWrite ().

Appendix: square wave, tone () and analogWrite ()

Illustration of a square signal

The tone () function generates a square wave with a precise frequency and a duty cycle of 50%. This means that the high and low times of the signal are the same durations.

You should not confuse the tone () function with the analogWrite () function which seems to have a similar behavior at first glance.

The tone () function is used to generate a signal of variable frequency and fixed duty cycle. The analogWrite () function generates a signal of fixed frequency and variable duty cycle. The difference is subtle, but it changes everything

Have silence with noTone ()

As we have seen in the previous chapter, it is possible to call the tone () function with or without signal generation time.

When you call the tone () function without duration, it generates a signal indefinitely, which can quickly get very annoying, a bit like a mosquito that you can't catch.

Fortunately, there is a function, called noTone () which allows you to find some silence.

1
void noTone(int broche);
It suffices to give the pin number to the noTone () function as a parameter and the latter will immediately stop the signal generation on this pin, until the next call to the tone () function.

PS You will notice that the above Arduino functions were designed with a possible future evolution to generate multiple signals at the same time on multiple pins. This is not the case for the moment, but everything has been planned for later.

Bonus: a little retro music

It's time for the d-d-d-d-duel bonus chapter!

And what better than a retro music player (chiptune) to make a bonus chapter!

The code

The assembly is identical to that of the previous chapter, so I go directly to the code;)

To make music with tone (), just chain calls to tone () and delay ().

For example, to have a little Tetris look in your office:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * Tetris musical theme in Arduino with tone()
 */

const byte PIN_BUZZER = 9;

void setup() {
  pinMode(PIN_BUZZER, OUTPUT);
}

void loop() {
  tone(PIN_BUZZER, 2637, 200);
  delay(400);
  tone(PIN_BUZZER, 1975, 200);
  delay(200);
  tone(PIN_BUZZER, 2093, 200);
  delay(200);
  tone(PIN_BUZZER, 2349, 200);
  delay(400);
  tone(PIN_BUZZER, 2093, 200);
  delay(200);
  tone(PIN_BUZZER, 1975, 200);
  delay(200);
  tone(PIN_BUZZER, 1760, 200);
  delay(400);
  tone(PIN_BUZZER, 1760, 200);
  delay(200);
  tone(PIN_BUZZER, 2093, 200);
  delay(200);
  tone(PIN_BUZZER, 2637, 200);
  delay(400);
  tone(PIN_BUZZER, 2349, 200);
  delay(200);
  tone(PIN_BUZZER, 2093, 200);
  delay(200);
  tone(PIN_BUZZER, 1975, 200);
  delay(400);
  tone(PIN_BUZZER, 1975, 200);
  delay(200);
  tone(PIN_BUZZER, 2093, 200);
  delay(200);
  tone(PIN_BUZZER, 2349, 200);
  delay(400);
  tone(PIN_BUZZER, 2637, 200);
  delay(400);
  tone(PIN_BUZZER, 2093, 200);
  delay(400);
  tone(PIN_BUZZER, 1760, 200);
  delay(400);
  tone(PIN_BUZZER, 1760, 200);
  delay(800);
  tone(PIN_BUZZER, 1760, 200);
  delay(400);
  tone(PIN_BUZZER, 2349, 200);
  delay(200);
  tone(PIN_BUZZER, 2794, 200);
  delay(200);
  tone(PIN_BUZZER, 3520, 200);
  delay(400);
  tone(PIN_BUZZER, 3136, 200);
  delay(200);
  tone(PIN_BUZZER, 2794, 200);
  delay(200);
  tone(PIN_BUZZER, 2637, 200);
  delay(600);
  tone(PIN_BUZZER, 2093, 200);
  delay(200);
  tone(PIN_BUZZER, 2637, 200);
  delay(400);
  tone(PIN_BUZZER, 2349, 200);
  delay(200);
  tone(PIN_BUZZER, 2093, 200);
  delay(200);
  tone(PIN_BUZZER, 1975, 200);
  delay(400);
  tone(PIN_BUZZER, 1975, 200);
  delay(200);
  tone(PIN_BUZZER, 2093, 200);
  delay(200);
  tone(PIN_BUZZER, 2349, 200);
  delay(400);
  tone(PIN_BUZZER, 2637, 200);
  delay(400);
  tone(PIN_BUZZER, 2093, 200);
  delay(400);
  tone(PIN_BUZZER, 1760, 200);
  delay(400);
  tone(PIN_BUZZER, 1760, 200);
  delay(800);
}

Conclusion

This tutorial is now complete.

If you enjoyed this tutorial, feel free to comment on it on the forum, post it on social media, and support the site if you like it.







google-playkhamsatmostaqltradent