Hello, I'm trying to measure the approximate distance of two modules.
Using a pair of Arduino Unos and NRF24L01 2.4 GHz wireless transceiver modules, I'm running into an issue with the provided code. The serial output of the receiver code isn't showing any output. Which leads to the Transmitter code to constantly display "Waiting for response from receiver..."
Note: I've tested the connections from the Arduino Uno to the NRF24L01 with the provided example sketch pairs
(nrf24_client & nrf24_server)
(nrf24_reliable_datagram_client & nrf24_reliable_datagram_server)
The only modifications was CE set to pin 9 and CSN set to pin 10 for the driver.
Transmitter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// CE and CSN pins for the NRF24L01 module
#define CE_PIN 9
#define CSN_PIN 10
// Define the address of the transmitter and receiver modules
const byte address[6] = "00001";
const byte receiverAddress[6] = "00002";
RF24 radio(CE_PIN, CSN_PIN);
unsigned long startTime = 0;
unsigned long endTime = 0;
unsigned long TOA = 0;
float distance = 0;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening(); // Set the module to transmitter mode
}
void loop() {
// Define the message to send to the receiver
char message[] = "Hello, Receiver!";
// Send the message to the receiver
radio.write(&message, sizeof(message));
startTime = micros(); // Note the time when the message was sent
// Wait for the response from the receiver
while (!radio.available()) {
Serial.println("Waiting for response from receiver...");
delay(1000); // Wait for 1 second before checking again
}
Serial.println("Received response from receiver!");
// Note the time when the response was received
endTime = micros();
// Calculate the TOA and print it to the serial monitor
TOA = endTime - startTime;
Serial.print("TOA: ");
Serial.print(TOA);
Serial.print(" us, ");
// Calculate the distance based on TOA and print it to the serial monitor
distance = (TOA / 2.0) * 0.0003;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" meters");
delay(1000); // Wait for 1 second before sending the next message
}
Receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// CE and CSN pins for the NRF24L01 module
#define CE_PIN 9
#define CSN_PIN 10
// Define the address of the transmitter and receiver modules
const byte address[6] = "00002";
const byte transmitterAddress[6] = "00001";
RF24 radio(CE_PIN, CSN_PIN);
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening(); // Set the module to receiver mode
}
void loop() {
// Check if there is a message available
if (radio.available()) {
// Read the message from the transmitter
char message[20];
radio.read(&message, sizeof(message));
Serial.print("Received message: ");
Serial.println(message);
// Send a response back to the transmitter
char response[] = "Hello, Transmitter!";
radio.stopListening(); // Set the module to transmitter mode
radio.openWritingPipe(transmitterAddress);
radio.write(&response, sizeof(response));
radio.openReadingPipe(0, address); // Set the module back to receiver mode
}
}