Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Digital-to-Analog Converter (DAC)
The Arduino Uno R4 WiFi comes equipped with a built-in DAC (Digital-to-Analog Converter) feature. A DAC is crucial for converting digital signals into their analog counterparts, a functionality that’s particularly vital in applications like audio processing, analog signal generation, and other scenarios requiring precise analog output.
The DAC on the Uno R4 WiFi boasts up to 12-bit resolution, delivering true analog output capabilities that outperform those of PWM pins.
Play Music with DAC
Circuit Diagram
Upload the Code
Open the 07-dac.ino file located at elite-explorer-kit-main\r4_new_feature\07-dac, or paste the following code into your Arduino IDE.
Note
Please place the pitches.h file in the same directory as the code to ensure proper functioning.
/*****************
Public Constants
*****************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 49
1/*
2 This code is aimed at producing a melody through a DAC (Digital to
3 Analog Converter) on pin A0 of an Arduino Uno R4 board using the
4 analogWave library. The melody and rhythm are defined within an
5 array where each note is followed by its duration, based on a
6 specified tempo. The analogWave object is initialized to generate
7 sine waves, and the melody is played within the main loop. Each
8 note is played for 85% of its duration, followed by a pause of 15%
9 of the note's duration before proceeding to the next note. The
10 melody repeats indefinitely with a one-second pause between
11 repetitions.
12
13 Board: Arduino Uno R4
14*/
15
16
17#include "analogWave.h"
18#include "pitches.h"
19
20// Melody and rhythm defined as an array
21// Each note is followed by its duration (4 = quarter note, 8 = eighth note, etc.)
22int melody[] = {
23 // Super Mario Bros theme
24 // Score available at https://musescore.com/user/2123/scores/2145
25 // Theme by Koji Kondo
26 NOTE_E5,8, NOTE_E5,8, REST,8, NOTE_E5,8, REST,8, NOTE_C5,8, NOTE_E5,8, //1
27 NOTE_G5,4, REST,4, NOTE_G4,8, REST,4,
28 NOTE_C5,-4, NOTE_G4,8, REST,4, NOTE_E4,-4, // 3
29 NOTE_A4,4, NOTE_B4,4, NOTE_AS4,8, NOTE_A4,4,
30 NOTE_G4,-8, NOTE_E5,-8, NOTE_G5,-8, NOTE_A5,4, NOTE_F5,8, NOTE_G5,8,
31 REST,8, NOTE_E5,4,NOTE_C5,8, NOTE_D5,8, NOTE_B4,-4,
32};
33
34analogWave wave(DAC); // Initialize the analogWave object for DAC(A0)
35
36int noteCounter = 0; // Index to keep track of which note is being played
37int tempo = 200; // Set the tempo in BPM (Beats Per Minute)
38int wholenote = (60000 * 4) / tempo; // Calculate the duration of a whole note in ms
39int divider = 0, noteDuration = 0; // Variables to hold note duration
40
41void setup() {
42 wave.sine(10); // Initialize the sine wave generator with a frequency of 10 Hz
43}
44
45void loop() {
46 // Calculate the duration of the current note
47 divider = melody[noteCounter + 1];
48 if (divider > 0) {
49 // For regular notes
50 noteDuration = wholenote / divider;
51 } else if (divider < 0) {
52 // For dotted notes (duration increased by 50%)
53 noteDuration = wholenote / abs(divider);
54 noteDuration *= 1.5; // Increase the duration by 50% for dotted notes
55 }
56
57 // Play the note
58 wave.freq(melody[noteCounter]);
59 delay(noteDuration * 0.85); // Play the note for 85% of its duration
60 wave.stop();
61
62 // Pause between notes
63 delay(noteDuration * 0.15); // Pause for 15% of the note duration
64
65 // Increment the note counter by 2 (because each note is followed by its duration)
66 noteCounter += 2;
67
68 // Reset the counter when reaching the end of the melody
69 int totalNotes = sizeof(melody) / sizeof(melody[0]);
70 noteCounter = noteCounter % totalNotes;
71
72 // Add a delay between repetitions of the melody
73 if (noteCounter == 0) {
74 delay(1000);
75 }
76}
This project leverages the Arduino and DAC (Digital-to-Analog Converter) to play the iconic Super Mario Bros theme song. It utilizes a library called analogWave for sine wave generation and another library, pitches.h, for defining note frequencies.
melody[]: This array contains the notes to be played along with their durations. Notes are represented by predefined pitches (e.g.,NOTE_E5), and durations are represented in terms of beats (e.g., 4 signifies a quarter note). You can try composing your own melody by changing the notes and durations in the melody[] array. If you are interested, there is a repository on GitHub (robsoncouto/arduino-songs) that provides Arduino code for playing different songs. Although their approach may be different from this project, you can refer to their notes and durations. (Simply replace themelody[]in the corresponding track with the code in this project.)tempo: The tempo for this project is set at 200 BPM (Beats Per Minute), which is used to calculate the duration of each note. Modifying this value will change the speed of the song’s performance.Sine Wave Generator: The
analogWavelibrary’ssinefunction initializes a 10 Hz sine wave generator, which is used for outputting the notes via DAC.Note Duration: Based on the set tempo and the beat count for each note, the duration for each note is calculated.
Play and Pause: Each note plays for 85% of its calculated duration, followed by a 15% pause to distinguish between notes.
Loop: Upon completing the melody, the code automatically resets and starts playing again.
This is an example that demonstrates how to use Arduino and external hardware (DAC) to generate music. It also shows how to use arrays and loops to simplify the logic of music playback.
Reference