Example sketch showing how to create an audio digital receiver with the NRF24 class. Works with the nrf24_audio_tx sample transmitter Connect audio output to pin 6, through a low pass filter consisting of a 1k resistor in series followed by a 0.0033 microfarad capacitor to ground (48kHz filter). The audio quality is poor: dont expect hi-fi! We have to change the PWM frequency to 62 kHz so we can get bandwidth reasonable audio out through the low pass filter Tested on UNO
#include <NRF24.h>
#include <SPI.h>
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
void setup()
{
Serial.begin(9600);
while (!Serial)
;
Serial.println("NRF24 init failed");
Serial.println("setChannel failed");
Serial.println("setThisAddress failed");
Serial.println("setPayloadSize failed");
Serial.println("setRF failed");
Serial.println("powerOnRx failed");
setPwmFrequency(6, 1);
Serial.println("initialised");
}
unsigned long count = 0;
void loop()
{
uint8_t buf[32];
uint8_t len = sizeof(buf);
if (nrf24.
recv(buf, &len))
{
uint8_t i;
for (i = 0; i < 32; i++)
{
analogWrite(6, buf[i]);
delayMicroseconds(65);
}
}
}