Skip to content

Commit b675fc1

Browse files
committed
2 more examples for NRF52
- Add radio transmission & reception examples using both PWM and I2S output
1 parent d3cc55c commit b675fc1

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Example of receiving audio over radio and playing it back.
2+
* Note: This example uses I2S output. Modify the USE_I2S #define
3+
* below to enable PWM output.
4+
*/
5+
6+
#include <AutoAnalogAudio.h>
7+
#include <nrf_to_nrf.h>
8+
9+
AutoAnalog aaAudio;
10+
nrf_to_nrf radio;
11+
12+
uint8_t address[][6] = { "1Node", "2Node" };
13+
#define BUFFER_SIZE 125
14+
#define USE_I2S 0 // Change to 1 to enable I2S output instead of PWM
15+
16+
void setup() {
17+
18+
Serial.begin(115200);
19+
20+
radio.begin();
21+
radio.setPayloadSize(BUFFER_SIZE);
22+
radio.setAutoAck(0);
23+
radio.setDataRate(NRF_2MBPS);
24+
radio.openReadingPipe(1,address[1]);
25+
radio.startListening();
26+
27+
Serial.println("Analog Audio Begin");
28+
aaAudio.begin(0, 1, USE_I2S); //Setup aaAudio using DAC and PWM. Change the third value to a 1 to enable I2S
29+
30+
//Setup for audio: Use 8-bit, mono WAV files @ 16kHz
31+
aaAudio.dacBitsPerSample = 8; // 8-bit
32+
aaAudio.setSampleRate(16000, 0); // 16khz, mono
33+
34+
pinMode(6,OUTPUT); // For I2S output: Connected to SD pin of MAX98357A
35+
digitalWrite(6,HIGH);
36+
37+
}
38+
39+
void loop() {
40+
41+
if(radio.available()){
42+
radio.read(&aaAudio.dacBuffer[0],BUFFER_SIZE);
43+
aaAudio.feedDAC(0,BUFFER_SIZE);
44+
}
45+
46+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* Example of reading audio from SD card and sending over radio
2+
* Note: This example uses I2S output. Modify the USE_I2S #define
3+
* below to enable PWM output.
4+
*/
5+
6+
#include <SPI.h>
7+
#include <SD.h>
8+
#include <AutoAnalogAudio.h>
9+
#include <nrf_to_nrf.h>
10+
11+
AutoAnalog aaAudio;
12+
nrf_to_nrf radio;
13+
14+
uint8_t address[][6] = { "1Node", "2Node" };
15+
#define BUFFER_SIZE 125
16+
#define USE_I2S 1 // Change to 0 to enable PWM output instead of I2S
17+
18+
/*********************************************************/
19+
/* Tested with MAX98357A I2S breakout on XIAO 52840 Sense
20+
/* BCLK connected to Arduino D1 (p0.03)
21+
/* LRCK connected to Arduino D3 (p0.29)
22+
/* DIN connected to Arduino D5 (p0.05)
23+
/* SD connected to Arduino D6 (p1.11)
24+
/*********************************************************/
25+
26+
void setup() {
27+
28+
Serial.begin(115200);
29+
30+
radio.begin();
31+
radio.setPayloadSize(BUFFER_SIZE);
32+
radio.setAutoAck(0);
33+
radio.setDataRate(NRF_2MBPS);
34+
radio.openWritingPipe(address[1]);
35+
radio.stopListening();
36+
37+
Serial.print("Init SD card...");
38+
if (!SD.begin(10000000, 2)) {
39+
Serial.println("init failed!");
40+
return;
41+
}
42+
Serial.println("init ok");
43+
Serial.println("Analog Audio Begin");
44+
aaAudio.begin(0, 1, USE_I2S); //Setup aaAudio using DAC and I2S
45+
46+
//Setup for audio: Use 8-bit, mono WAV files @ 16kHz
47+
aaAudio.dacBitsPerSample = 8; // 8-bit
48+
aaAudio.setSampleRate(16000, 0); // 16khz, mono
49+
50+
pinMode(6, OUTPUT); //Connected to SD pin of MAX98357A
51+
digitalWrite(6, HIGH);
52+
53+
playAudio("guitar/g8b16km.wav"); // 16kHz @ 8-bits is about the maximum when reading from SD card & streaming over radio
54+
}
55+
56+
void loop() {
57+
58+
loadBuffer();
59+
}
60+
61+
/*********************************************************/
62+
/* A simple function to handle playing audio files
63+
/*********************************************************/
64+
65+
File myFile;
66+
67+
void playAudio(const char *audioFile) {
68+
69+
if (myFile) {
70+
myFile.close();
71+
}
72+
//Open the designated file
73+
myFile = SD.open(audioFile);
74+
//Skip past the WAV header
75+
myFile.seek(44);
76+
}
77+
78+
void loadBuffer() {
79+
80+
if (myFile.available()) {
81+
myFile.read(aaAudio.dacBuffer, BUFFER_SIZE); // Change this to dacBuffer16 for 16-bit samples
82+
aaAudio.feedDAC(0, BUFFER_SIZE);
83+
radio.startWrite(&aaAudio.dacBuffer[0], BUFFER_SIZE, 1);
84+
} else {
85+
myFile.seek(44);
86+
}
87+
}

0 commit comments

Comments
 (0)