I2S Library Examples
Enables to use the I2S protocol on SAMD21 board like Arduino Zero, Arduino MKRZero and Arduino MKR1000.
The first example will show you how to read and visualize audio data coming from an I2S microphone. The second example shows you how to generate a simple tone using a SAMD21 based board and an I2S DAC.
Hardware Required
- Zero, MKR1000 or MKRZero Board
- I2S microphone (i.e ICS43432)
- MAX98357A amplifier
- 3W minimum Speaker 4 or 8 Ohms
Circuit
To run the first example you simply have to connect the board and the I2S microphone using the I2S bus as shown in the image. The image is for MKRZero; you find the proper pins for Zero and MKR1000 at the beginning of the sketch, in the comments.
To run the second example you simply have to connect the board and the I2S DAC using the I2S bus as shown in the image. The image is for MKRZero; you find the proper pins for Zero and MKR1000 at the beginning of the sketch, in the comments.
I2S Input Serial Plotter
This example shows you how to read and visualize on the serial plotter I2S audio data coming from an I2S microphone.
1/*2
3 This example reads audio data from an Invensense's ICS43432 I2S microphone4
5 breakout board, and prints out the samples to the Serial console. The6
7 Serial Plotter built into the Arduino IDE can be used to plot the audio8
9 data (Tools -> Serial Plotter)10
11 Circuit:12
13 * Arduino Zero, MKR family and Nano 33 IoT14
15 * ICS43432:16
17 * GND connected GND18
19 * 3.3V connected to 3.3V (Zero, Nano) or VCC (MKR)20
21 * WS connected to pin 0 (Zero) or 3 (MKR) or A2 (Nano)22
23 * CLK connected to pin 1 (Zero) or 2 (MKR) or A3 (Nano)24
25 * SD connected to pin 9 (Zero) or A6 (MKR) or 4 (Nano)26
27 created 17 November 201628
29 by Sandeep Mistry30
31 */32
33#include <I2S.h>34
35void setup() {36
37 // Open serial communications and wait for port to open:38
39 // A baud rate of 115200 is used instead of 9600 for a faster data rate40
41 // on non-native USB ports42
43 Serial.begin(115200);44
45 while (!Serial) {46
47 ; // wait for serial port to connect. Needed for native USB port only48
49 }50
51 // start I2S at 8 kHz with 32-bits per sample52
53 if (!I2S.begin(I2S_PHILIPS_MODE, 8000, 32)) {54
55 Serial.println("Failed to initialize I2S!");56
57 while (1); // do nothing58
59 }60}61
62void loop() {63
64 // read a sample65
66 int sample = I2S.read();67
68 if (sample) {69
70 // if it's non-zero print value to serial71
72 Serial.println(sample);73
74 }75}
I2S Simple Tone
This example shows you how to generate a simple tone using a SAMD21 based board (MKRZero, MKR1000 or Zero) and an I2S DAC like the adafruit MAX98357A.
1/*2
3 This example generates a square wave based tone at a specified frequency4
5 and sample rate. Then outputs the data using the I2S interface to a6
7 MAX98357 I2S Amp Breakout board.8
9 Circuit:10
11 * Arduino Zero, MKR family and Nano 33 IoT12
13 * MAX98357:14
15 * GND connected GND16
17 * VIN connected 5V18
19 * LRC connected to pin 0 (Zero) or 3 (MKR) or A2 (Nano)20
21 * BCLK connected to pin 1 (Zero) or 2 (MKR) or A3 (Nano)22
23 * DIN connected to pin 9 (Zero) or A6 (MKR) or 4 (Nano)24
25 created 17 November 201626
27 by Sandeep Mistry28
29 */30
31#include <I2S.h>32
33const int frequency = 440; // frequency of square wave in Hz34
35const int amplitude = 500; // amplitude of square wave36
37const int sampleRate = 8000; // sample rate in Hz38
39const int halfWavelength = (sampleRate / frequency) / 2; // half wavelength of square wave40
41short sample = amplitude; // current sample value42int count = 0;43
44void setup() {45
46 Serial.begin(9600);47
48 Serial.println("I2S simple tone");49
50 // start I2S at the sample rate with 16-bits per sample51
52 if (!I2S.begin(I2S_PHILIPS_MODE, sampleRate, 16)) {53
54 Serial.println("Failed to initialize I2S!");55
56 while (1); // do nothing57
58 }59}60
61void loop() {62
63 if (count % halfWavelength == 0) {64
65 // invert the sample every half wavelength count multiple to generate square wave66
67 sample = -1 * sample;68
69 }70
71 // write the same sample twice, once for left and once for the right channel72
73 I2S.write(sample);74
75 I2S.write(sample);76
77 // increment the counter for the next sample78
79 count++;80}
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.