Skip to content

Commit f3a37b9

Browse files
committed
PWM example
1 parent 8d7353b commit f3a37b9

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# PWM Output Test
2+
3+
This is a simple basic test for the PWM output.
4+
5+
We just send a generated sine wave to some defined pins and expect to hear some audio signal.
6+
7+
8+
### Output Device: Piezo Electric Element
9+
10+
To test the output I am using a piezo electric element
11+
12+
![DAC](https://pschatzmann.github.io/arduino-audio-tools/resources/piezo.jpeg)
13+
14+
It should also be possible to connect a headphone to the output pins...
15+
16+
17+
On the ESP32 the output is on the Pins GPIO26 and GPIO27
18+
19+
| PIEZO | ESP32
20+
| --------| ---------------
21+
| + | GPIO22 / GPIO23
22+
| - | GND
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @file streams-generator-pwm.ino
3+
* @author Phil Schatzmann
4+
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/streams-generator-pwm/README.md
5+
* @author Phil Schatzmann
6+
* @copyright GPLv3
7+
*/
8+
9+
#include "AudioTools.h"
10+
11+
using namespace audio_tools;
12+
13+
int pins[] = {22, 23};
14+
int channels = 2;
15+
uint16_t sample_rate=22000;
16+
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
17+
GeneratedSoundStream<int16_t> sound(sineWave, channels); // Stream generated from sine wave
18+
AudioPWM pwm;
19+
StreamCopy copier(pwm, sound); // copy in to out
20+
21+
22+
void setup() {
23+
Serial.begin(115200);
24+
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
25+
26+
// setup sine wave
27+
sineWave.begin(sample_rate, N_B4);
28+
29+
// setup PWM output
30+
auto config = pwm.defaultConfig();
31+
config.sample_rate = sample_rate;
32+
config.resolution = 8; // must be between 8 and 11 -> drives pwm frequency (8 is default)
33+
// alternative 1
34+
//config.channels = 2; // not necesarry because 2 is default
35+
//config.start_pin = 22;
36+
// alternative 2: defines pins and channels
37+
config.setPins(pins);
38+
pwm.begin(config);
39+
}
40+
41+
void loop(){
42+
copier.copy();
43+
}

0 commit comments

Comments
 (0)