Last active
May 16, 2024 21:50
-
-
Save todbot/da48c8d7f9d8998875e605b503da60f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// esp32s3_i2s_simple_sine.ino | |
// demonstrate I2S on ESP32S3 | |
// origally from guiguig on Discord: https://discord.com/channels/327254708534116352/422210663507558401/1240067408660795424 | |
// video demo below in comment | |
#include <I2S.h> | |
#define BCK 16 // feather esp32s3 A2 | |
#define DOUT 17 // feather esp32s3 A1 | |
#define LCK 18 // feather esp32s3 A0 | |
double t = 0.0; | |
double frequency = 440.0; | |
int sampleRate = 22500; | |
//int sampleRate = 44100; // not acheiveable without DMA | |
double amplitude = 2048.0; | |
double value; | |
void setup() { | |
Serial.begin(115200); | |
// Set I2S output pins | |
I2S.setAllPins(/* BitClkPin */ BCK, /* LRClkPin */ LCK, /* dataOutPin */ DOUT, /* dataInPin */ -1, /* mclkPin */ -1); | |
if (!I2S.begin(I2S_PHILIPS_MODE, sampleRate, 16)) { | |
while (1) { | |
Serial.println("Failed to initialize I2S!"); | |
delay(100); | |
} | |
} | |
} | |
uint32_t last_time = 0; | |
void loop() { | |
if( millis() - last_time > 500 ) { | |
last_time = millis(); | |
frequency = random(220, 440); | |
Serial.print("new frequency "); Serial.println(frequency); | |
} | |
// Sine wave waveform | |
value = sin(2.0 * PI * frequency * t); | |
// // Write both channel's samples | |
I2S.write((short)(value * amplitude)); | |
I2S.write((short)(value * amplitude)); | |
t += 1.0 / (double)sampleRate; | |
} |
The Arduino setup used was this. The board was generic "ESP32S3 Dev Module" because I was having issues with the Adafruit ESP32S3 Feather board variant. Using the generic means I had to put the board in USB Bootloader mode (BOOT + RESET button) before every upload and press RESET to start the sketch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Video demo:
IMG_3826.mov