/**
Author: tiger888
Date: 21/08/2024
Last Update: Initial Version 1.0
Sydney, Australia
******* THE PROJECT *********************************
Simulation: NRF24L01 RF Transceiver (RECEIVER Only)
*****************************************************
Project Components;
1 x Arduino Nano
1 x 5v Power supply but pin to Arduino 3.3v.
Optional use mobile phone power bank instead.
1 x NRF24L01 (RF Transceiver) - Receiver mode only
1 x Capacitors 470 micro farad (white noise filter)
1 x Resistors 220 ohm (LED current limiter)
1 x LED yellow (Rx status indicator: steady ON when received TX, else OFF)
1 x LED red (Rx status indicator: steady ON when no data received, else OFF)
Note: In the case that both LED are ON then Rx received data but data is empty (issue?)
*/
#include <SPI.h>
#include <RF24.h>
// Pin 7, 8 are CE & CSN NFR24L01 default pin config
#define CE_PIN 7
#define CSN_PIN 8
// Monitor LED Rx status
#define LED_RX_PIN 5 // yellow (steady ON when Rx receives data, else OFF)
#define LED_NODATA_PIN 9 // red (Normally OFF, Steady ON when no data received)
// RF24 simple constructor
RF24 radio(CE_PIN, CSN_PIN);
// Import: this defined address MUST match its Transmitter pair
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
char dataReceived[32] = {0};
char testData[32] = {"Test Received Data"};
void setup() {
Serial.begin(9600);
Serial.flush();
// Default LED Rx status
pinMode(LED_RX_PIN, OUTPUT);
digitalWrite(LED_RX_PIN, LOW);
pinMode(LED_NODATA_PIN, OUTPUT);
digitalWrite(LED_NODATA_PIN, LOW);
// Init RF24 as receiver mode
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
Serial.println("Receiver setup completed.");
}
void loop(){
if (radio.available()) {
radio.read(&dataReceived, sizeof(dataReceived));
// Testing only - remove when done
//strcpy(dataReceived, testData);
//////////////////////////////////
// Check if dataReceived is empty
if (strlen(dataReceived) == 0) {
// Rx received but data is empty
digitalWrite(LED_RX_PIN, HIGH); // Yellow LED ON
digitalWrite(LED_NODATA_PIN, HIGH); // Red LED ON
Serial.println("Data received but data is empty");
} else {
// Rx received data that is not empty (all good here.)
digitalWrite(LED_RX_PIN, HIGH); // Yellow LED ON
digitalWrite(LED_NODATA_PIN, LOW); // Red LED OFF
Serial.print("Data received: ");
Serial.println(dataReceived);
}
} else {
// Failed: either Rx is not working or Tx did not send data
digitalWrite(LED_RX_PIN, LOW); // Yellow LED OFF
digitalWrite(LED_NODATA_PIN, LOW); // Red LED OFF
Serial.println("Data NOT received.");
}
}