This example uses Acknowledgement (ACK) payloads attached to ACK packets to demonstrate how the nRF24L01's IRQ (Interrupt Request) pin can be configured to detect when data is received, or when data has transmitted successfully, or when data has failed to transmit.
This example was written to be used on 2 devices acting as "nodes". Use the Serial Monitor to change each node's behavior.
22volatile bool got_interrupt =
false;
23volatile bool wait_for_event =
false;
28RF24 radio(CE_PIN, CSN_PIN);
31uint8_t address[][6] = {
"1Node",
"2Node" };
45const uint8_t tx_pl_size = 5;
46const uint8_t ack_pl_size = 4;
47uint8_t pl_iterator = 0;
49char tx_payloads[][tx_pl_size + 1] = {
"Ping ",
"Pong ",
"Radio",
"1FAIL" };
50char ack_payloads[][ack_pl_size + 1] = {
"Yak ",
"Back",
" ACK" };
52void interruptHandler();
64 Serial.println(F(
"radio hardware is not responding!!"));
69 Serial.println(F(
"RF24/examples/InterruptConfigure"));
72 Serial.println(F(
"Which radio is this? Enter '0' or '1'. Defaults to '0'"));
73 while (!Serial.available()) {
76 char input = Serial.parseInt();
77 radioNumber = input == 1;
78 Serial.print(F(
"radioNumber = "));
79 Serial.println((
int)radioNumber);
82 Serial.println(F(
"*** PRESS 'T' to begin transmitting to the other node"));
86 attachInterrupt(digitalPinToInterrupt(IRQ_PIN), interruptHandler, FALLING);
100 radio.enableDynamicPayloads();
104 radio.enableAckPayload();
108 radio.openWritingPipe(address[radioNumber]);
111 radio.openReadingPipe(1, address[!radioNumber]);
116 radio.stopListening();
122 radio.maskIRQ(1, 1, 0);
126 radio.writeAckPayload(1, &ack_payloads[0], ack_pl_size);
127 radio.writeAckPayload(1, &ack_payloads[1], ack_pl_size);
128 radio.writeAckPayload(1, &ack_payloads[2], ack_pl_size);
130 radio.startListening();
141 assessInterruptEvent();
144 if (role && !wait_for_event) {
149 if (pl_iterator == 0) {
152 Serial.println(F(
"\nConfiguring IRQ pin to ignore the 'data sent' event"));
153 radio.maskIRQ(
true,
false,
false);
154 Serial.println(F(
" Pinging RX node for 'data ready' event..."));
156 }
else if (pl_iterator == 1) {
159 Serial.println(F(
"\nConfiguring IRQ pin to ignore the 'data ready' event"));
160 radio.maskIRQ(
false,
false,
true);
161 Serial.println(F(
" Pinging RX node for 'data sent' event..."));
163 }
else if (pl_iterator == 2) {
167 radio.maskIRQ(1, 1, 1);
169 Serial.println(F(
"\nSending 1 payload to fill RX node's FIFO. IRQ pin is neglected."));
171 if (radio.write(&tx_payloads[pl_iterator], tx_pl_size)) {
172 if (radio.rxFifoFull()) {
173 Serial.println(F(
"RX node's FIFO is full; it is not listening any more"));
175 Serial.println(
"Transmission successful, but the RX node might still be listening.");
178 Serial.println(F(
"Transmission failed or timed out. Continuing anyway."));
182 }
else if (pl_iterator == 3) {
185 Serial.println(F(
"\nConfiguring IRQ pin to reflect all events"));
186 radio.maskIRQ(0, 0, 0);
187 Serial.println(F(
" Pinging inactive RX node for 'data fail' event..."));
190 if (pl_iterator < 4 && pl_iterator != 2) {
194 wait_for_event =
true;
198 radio.startFastWrite(tx_payloads[pl_iterator++], tx_pl_size,
false);
204 }
else if (pl_iterator == 4) {
208 radio.stopListening();
216 Serial.println(F(
"\n*** PRESS 'T' to restart the transmissions"));
217 Serial.println(F(
"*** PRESS 'R' to change to Receive role\n"));
220 }
else if (pl_iterator == 2) {
224 }
else if (!role && radio.rxFifoFull()) {
229 radio.stopListening();
234 radio.writeAckPayload(1, &ack_payloads[0], ack_pl_size);
235 radio.writeAckPayload(1, &ack_payloads[1], ack_pl_size);
236 radio.writeAckPayload(1, &ack_payloads[2], ack_pl_size);
239 radio.startListening();
243 if (Serial.available()) {
246 char c = toupper(Serial.read());
250 Serial.println(F(
"*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
252 Serial.println(F(
"*** RESTARTING IRQ PIN TEST ***"));
255 wait_for_event =
false;
261 radio.stopListening();
263 }
else if (c ==
'R' && role) {
265 Serial.println(F(
"*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
269 radio.maskIRQ(0, 0, 0);
275 radio.writeAckPayload(1, &ack_payloads[0], ack_pl_size);
276 radio.writeAckPayload(1, &ack_payloads[1], ack_pl_size);
277 radio.writeAckPayload(1, &ack_payloads[2], ack_pl_size);
278 radio.startListening();
288void interruptHandler() {
289 got_interrupt =
true;
296void assessInterruptEvent() {
299 Serial.println(F(
"\tIRQ pin is actively LOW"));
301 bool tx_ds, tx_df, rx_dr;
302 radio.whatHappened(tx_ds, tx_df, rx_dr);
307 Serial.print(F(
"\tdata_sent: "));
309 Serial.print(F(
", data_fail: "));
311 Serial.print(F(
", data_ready: "));
312 Serial.println(rx_dr);
319 if (pl_iterator <= 1) {
320 Serial.print(F(
" 'Data Ready' event test "));
321 Serial.println(rx_dr ? F(
"passed") : F(
"failed"));
322 }
else if (pl_iterator == 2) {
323 Serial.print(F(
" 'Data Sent' event test "));
324 Serial.println(tx_ds ? F(
"passed") : F(
"failed"));
325 }
else if (pl_iterator == 4) {
326 Serial.print(F(
" 'Data Fail' event test "));
327 Serial.println(tx_df ? F(
"passed") : F(
"failed"));
329 got_interrupt =
false;
330 wait_for_event =
false;
338 if (radio.available()) {
341 uint8_t pl_size = !role ? tx_pl_size : ack_pl_size;
342 char rx_fifo[pl_size * 3 + 1];
343 if (radio.rxFifoFull()) {
344 rx_fifo[pl_size * 3] = 0;
345 radio.read(&rx_fifo, pl_size * 3);
348 while (radio.available()) {
349 radio.read(&rx_fifo + (i * pl_size), pl_size);
352 rx_fifo[i * pl_size] = 0;
354 Serial.print(F(
"Complete RX FIFO: "));
355 Serial.println(rx_fifo);
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
#define pinMode(pin, direction)
#define delayMicroseconds(usec)