Approximate distance measurement with NRF24L01

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
  }
}

I recall the resolution of micros() on a UNO is 4uS.

In that time a radio wave will travel circa 1.2km.

And there would be the significant, in relation to the time of flight, software delays at both transmitter an receiver.

The LoRa SX1280 2.4Ghz tranceiver has the specialist internal hardware needed to measure distances, proven to work up to 85km.

You bring up a good point of the accuracy of the micros() function being limited to the UNO's clock frequency of 16MHz.

What I understand is that the resolution of 4uS causes the distance to be 1.2km in an unreal world where all other lines of code causes no delay.

Could this be feasible if an external clock signal of 100MHz was connected and counted how long the transmission took minus the software delay.

But is that good enough ?

You have not said, what distances you need to measure and with what resolution.

Sorry, I forgot to mention that I want to measure from a few meters to approximately 150 meters. Relative short range for demonstration purposes.

Have a look at this new Nordic developement

Well, you did not say what resolution you wanted, but for a resolution of 5M the round trip time of all that software running with two packet transmissions and two packet recieves, would need to be stable to around 17nS.

As I mentioned, this has already been done, but it needs different hardware, I doubt you would get the resoltion and stability needed with a home brew Arduino setup.

Good video, it's exactly what I'm trying to replicate. However from nordicsemiconductors, the Nordic Distance Toolbox only works with the nRF52833 and nRF52840.

Besides the LoRa SX1280 2.4GHz transceiver that you've suggested, do you know any other transceivers with similar capabilities?

Not myself, I am aware of the device mentioned here;

Measuring distances, for positioning, is a very very common asked for item on here, mostly people assume it can be done with RSSI, but as mentioned in the Nordic video, it does not really work.

If a 'solution' to such a common requirement was as simple as an Arduino and a high speed counter, you might have expected that there would be heaps of shields available, with all the needed bits on them.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.