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

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.

Circuit for Input Serial Plotter.
Circuit for Input Serial Plotter.

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.

Circuit for I2S Simple Tone.
Circuit for I2S Simple Tone.

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 microphone
4
5 breakout board, and prints out the samples to the Serial console. The
6
7 Serial Plotter built into the Arduino IDE can be used to plot the audio
8
9 data (Tools -> Serial Plotter)
10
11 Circuit:
12
13 * Arduino Zero, MKR family and Nano 33 IoT
14
15 * ICS43432:
16
17 * GND connected GND
18
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 2016
28
29 by Sandeep Mistry
30
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 rate
40
41 // on non-native USB ports
42
43 Serial.begin(115200);
44
45 while (!Serial) {
46
47 ; // wait for serial port to connect. Needed for native USB port only
48
49 }
50
51 // start I2S at 8 kHz with 32-bits per sample
52
53 if (!I2S.begin(I2S_PHILIPS_MODE, 8000, 32)) {
54
55 Serial.println("Failed to initialize I2S!");
56
57 while (1); // do nothing
58
59 }
60}
61
62void loop() {
63
64 // read a sample
65
66 int sample = I2S.read();
67
68 if (sample) {
69
70 // if it's non-zero print value to serial
71
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 frequency
4
5 and sample rate. Then outputs the data using the I2S interface to a
6
7 MAX98357 I2S Amp Breakout board.
8
9 Circuit:
10
11 * Arduino Zero, MKR family and Nano 33 IoT
12
13 * MAX98357:
14
15 * GND connected GND
16
17 * VIN connected 5V
18
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 2016
26
27 by Sandeep Mistry
28
29 */
30
31#include <I2S.h>
32
33const int frequency = 440; // frequency of square wave in Hz
34
35const int amplitude = 500; // amplitude of square wave
36
37const int sampleRate = 8000; // sample rate in Hz
38
39const int halfWavelength = (sampleRate / frequency) / 2; // half wavelength of square wave
40
41short sample = amplitude; // current sample value
42int 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 sample
51
52 if (!I2S.begin(I2S_PHILIPS_MODE, sampleRate, 16)) {
53
54 Serial.println("Failed to initialize I2S!");
55
56 while (1); // do nothing
57
58 }
59}
60
61void loop() {
62
63 if (count % halfWavelength == 0) {
64
65 // invert the sample every half wavelength count multiple to generate square wave
66
67 sample = -1 * sample;
68
69 }
70
71 // write the same sample twice, once for left and once for the right channel
72
73 I2S.write(sample);
74
75 I2S.write(sample);
76
77 // increment the counter for the next sample
78
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.