Audio sine wave generator for ESP32
This Arduino library is meant for creating audio tones on an ESP32 microcontroller using its internal digital-to-audio (DAC) converter. The result is a well-formed sine wave on either of its two DAC pins, GPIO25 or GPIO26.
First, include DacTone in your sketch:
#include "DacTone.h"
Next, instantiate a DacTone object:
DacTone audio;
Finally, call tone() to start playing a tone, and noTone() to silence it:
audio.tone(1200); // start 1200 Hz sine wave output
delay(1000); // play it for a second, then
audio.noTone(); // silence it
- Beep - A simple blink sketch, audio-style
- Scales - How to generate notes using frequency calculations
- PlayBach - Play a short musical piece using defined notes
- SoundEffects - How to use lower-level code for sound generation
- FastTones - Shows accuracy of sine wave frequency
DacTone allows you to control the amplitude (volume) and offset (DC bias) of the waveform. If the volume and/or bias exceed to output capabilities of the DAC, waveform clipping will result. The isClipping() method will tell you if the current waveform is clipping. Changing the amound of clipping can result in interesting sounds!
-
tone(int Hz)
- starts audio output at the requested frequency. Acceptable input values are 0 to 5000. Calling tone with no parametertone()
results in 880 Hz output. Calling tone(0) silences output. -
noTone()
- silences audio output -
setVolume(int volume)
- sets the amplitude of the waveform. There are 4 levels of amplitude: 100, 50, 25, and 12. These roughly correspond to voltage amplitudes of 3.2, 1.7, 1.0, and 0.6 volts.
The following methods are used less often, but may be helpful in certain cases:
-
setOffset(int offset)
- sets the DC bias of the waveform. Acceptable input values are -128 to +127. The default offset of zero results in a DC bias of about 1.65V, which will allow full voltage swing at 100% volume without waveform clipping. -
isClipped()
- returns true if the current waveform is being clipped. -
setFrequency(int div, int step)
- a high-speed method of setting the output frequency. It is much faster than tone(), but you must already know the clock divisor and frequency step parameters. -
int getFrequency(int *div, int *step)
- returns the actual frequency output from a call to tone() or setFrequency(). It also returns the last clock divisor and frequency step parameters used.
See comments in DacTone.cpp for documentation of each public method.
- DacTone seems to interferes with
digitalInput()
under certain conditions. - DacTone output may differ from the requested frequency by several Hz. This is usually not an issue, but a good musical ear may hear the difference!
The low-level code for enabling sine wave output is provided by krzychb. See his dac-cosine library for more information.