File tree Expand file tree Collapse file tree 3 files changed +86
-0
lines changed
CircuitPython_PCM5122_HW_Mode Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ // SPDX-FileCopyrightText: 2025 Ladyada for Adafruit Industries
2+ //
3+ // SPDX-License-Identifier: MIT
4+
5+ #include < I2S.h>
6+ #include < math.h>
7+
8+ #define pBCLK D9 // BITCLOCK - I2S clock
9+ #define pWS D10 // LRCLOCK - Word select
10+ #define pDOUT D11 // DATA - I2S data
11+
12+ // Create I2S port
13+ I2S i2s (OUTPUT);
14+
15+ const int frequency = 440 ; // frequency of square wave in Hz
16+ const int amplitude = 500 ; // amplitude of square wave
17+ const int sampleRate = 16000 ; // 16 KHz is a good quality
18+
19+ const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
20+
21+ int16_t sample = amplitude; // current sample value
22+ int count = 0 ;
23+
24+ void setup () {
25+ Serial.begin (115200 );
26+ while (!Serial) delay (10 );
27+
28+ Serial.println (F (" Adafruit PCM51xx Hardware Mode Test" ));
29+
30+ // Initialize I2S peripheral
31+ Serial.println (" Initializing I2S..." );
32+ i2s.setBCLK (pBCLK);
33+ i2s.setDATA (pDOUT);
34+ i2s.setBitsPerSample (16 );
35+
36+ // Start I2S at the sample rate
37+ if (!i2s.begin (sampleRate)) {
38+ Serial.println (" Failed to initialize I2S!" );
39+ }
40+
41+ }
42+
43+ void loop () {
44+ if (count % halfWavelength == 0 ) {
45+ // invert the sample every half wavelength count multiple to generate square wave
46+ sample = -1 * sample;
47+ }
48+
49+ // write the same sample twice, once for left and once for the right channel
50+ i2s.write (sample);
51+ i2s.write (sample);
52+
53+ // increment the counter for the next sample
54+ count++;
55+ }
Original file line number Diff line number Diff line change 1+ # SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
2+ #
3+ # SPDX-License-Identifier: MIT
4+
5+ """
6+ Sine tone playback test for the PCM5122 I2S DAC in hardware mode.
7+ """
8+
9+ import array
10+ import math
11+ import time
12+
13+ import audiobusio
14+ import audiocore
15+ import board
16+
17+ audio = audiobusio .I2SOut (board .D9 , board .D10 , board .D11 )
18+
19+ tone_volume = 0.5 # Increase this to increase the volume of the tone.
20+ frequency = 440 # Set this to the Hz of the tone you want to generate.
21+ length = 8000 // frequency
22+ sine_wave = array .array ("h" , [0 ] * length )
23+ for i in range (length ):
24+ sine_wave [i ] = int ((math .sin (math .pi * 2 * i / length )) * tone_volume * (2 ** 15 - 1 ))
25+ sine_wave_sample = audiocore .RawSample (sine_wave )
26+
27+ while True :
28+ audio .play (sine_wave_sample , loop = True )
29+ time .sleep (1 )
30+ audio .stop ()
31+ time .sleep (1 )
You can’t perform that action at this time.
0 commit comments