nrf24: Problem with receiving distance value from the ToF-mini sensor

Hi folks!

Im trying to get the distancevalue from the curcuitboard with nrf and ToF powered up with a battery.
The reveicer is connected to my serialport.

The sender and receiver is up and running,they transfer the “data.id”, but not the distance value “data.cm”. I have no idea how to solve it, but I have tried for many hours now.

A picture of my setup is attached.

I appreciate the help :slight_smile:

RF sender:

#include <SPI.h>  
#include "RF24.h"
#include <DFRobot_TFmini.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(6, 5);
DFRobot_TFmini  TFmini;
uint16_t distance;


RF24 myRadio (7, 8);
byte addresses[][6] = {"0"};

struct package
{
  int id = 1;
  int cm;
};


typedef struct package Package;
Package data;


void setup()
{
  Serial.begin(115200);
  TFmini.begin(mySerial);
  myRadio.begin();  
  myRadio.setChannel(115); 
  myRadio.setPALevel(RF24_PA_LOW);
  myRadio.setDataRate( RF24_250KBPS ) ; 
  myRadio.openWritingPipe( addresses[0]);
delay(60);
}

void loop()
{
   delay(60);               
  data.cm = TFmini.getDistance();
  myRadio.stopListening();
  TFmini.measure(); 
  Serial.print("\nPackage:");
  Serial.print(data.id);
  Serial.print("\n");
  Serial.println(data.cm);
  data.id = data.id + 1;
  myRadio.write(&data, sizeof(data)); 
  
}

RF receiver

#include <SPI.h>  
#include "RF24.h" 



RF24 myRadio (7, 8); 
byte addresses[][6] = {"0"}; 

struct package
{
  int id = 0;
  int cm;
};


typedef struct package Package;
Package data ;

void setup() 
{
  Serial.begin(115200);
  myRadio.begin(); 
  myRadio.setChannel(115); 
  myRadio.setPALevel(RF24_PA_LOW);
  myRadio.setDataRate( RF24_250KBPS ) ; 
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
  delay (60);
}


void loop()  
{
  delay(60);
  myRadio.startListening();
  
  if ( myRadio.available()) 
  {
     myRadio.read( &data, sizeof(data) );
    Serial.print("\nPackage:");
    Serial.print(data.id);
    Serial.print("\n");
    Serial.println(data.cm);
    }
    
}

–Sender COM values–
Gives the right sensorvalues:

Package:601
43

Package:602
30

Package:603
30

Package:604
30

Package:605
48

Package:606
48

Package:607
48

Package:608
46

Package:609
41

Package:610
30

–Receiver COM values–
Not the right sensorvalues:

Package:2761
-3

Package:2762
-3

Package:2763
-3

Package:2764
-3

Package:2765
-3

What values for data.cm do you see at the transmitter and receiver ?

Take the delay() out of the receiving code - it should be listening all the time. And there is no need for

myRadio.startListening();

in loop(). That is already done in setup()

In the Tx program I suspect there is no advantage sending a message every 60 millisecs - that is about 16 times per second. 10 times, or even 5 times would probably be sufficient and would allow more time for other things to get done.

...R

UKHeliBob:
What values for data.cm do you see at the transmitter and receiver ?

This is what I see

Package:2761
-3

Package:2762
-3

Package:2763
-3

Package:2764
-3

....

Which end of the link is that ?

UKHeliBob:
Which end of the link is that ?

This is the values I get for the receiver end:

Package:300
-3

Package:301
-3

Package:302
-3

Package:303
-3

Package:304
-3

For the sender: [Rigth sensorvalues]

Package:601
43

Package:602
30

Package:603
30

Package:604
30

Package:605
48

Package:606
48

You seem to have declared the data struct in an odd way

Try

#include <SPI.h>
#include "RF24.h"

RF24 myRadio (7, 8);
byte addresses[][6] = {"0"};

struct package
{
  int id = 0;
  int cm;
} data;

void setup()
{
  Serial.begin(115200);
etc
etc

You are not mentioning what MCU's you are using but a possible cause of the problem could be that "int" is not 16bit on both ends. Try to declare your struct as follows:

//Typedef & variable:
struct TF_PACKAGE {
  uint16_t id = 0;
  int16_t cm;
} data;

You may also want to limit the payload size of the radios with "radio.setPayloadSize(sizeof(TF_PACKAGE))" in "setup()".

UKHeliBob:
You seem to have declared the data struct in an odd way

Try

#include <SPI.h>

#include “RF24.h”

RF24 myRadio (7, 8);
byte addresses[6] = {“0”};

struct package
{
  int id = 0;
  int cm;
} data;

void setup()
{
  Serial.begin(115200);
etc
etc

Now I got new results: [some of the values are correct, but still going back to -3]

Package:14
-3

Package:15
-3

Package:16
124

Package:17
-3

Package:18
116

Package:19
122

Package:20
130

Package:21
-3

Package:22
-3

Danois90:
You are not mentioning what MCU's you are using but a possible cause of the problem could be that "int" is not 16bit on both ends. Try to declare your struct as follows:

//Typedef & variable:

struct TF_PACKAGE {
  uint16_t id = 0;
  int16_t cm;
} data;




You may also want to limit the payload size of the radios with "radio.setPayloadSize(sizeof(TF_PACKAGE))" in "setup()".

Im using Arduino Nano, as you can see in the picture attached.
It didn't work limiting the payload size :confused:

Is this the actual address you are using:

byte addresses[][6] = {"0"};

If that is the case you should change it to fill all the 6 bytes, not only the first 2 (mind the null-terminator of the string):

byte addresses[][6] = {"12345"};

Danois90:
Is this the actual address you are using:

byte addresses[][6] = {"0"};

If that is the case you should change it to fill all the 6 bytes, not only the first 2 (mind the null-terminator of the string):

byte addresses[][6] = {"12345"};

Did that as well, didnt work :frowning:

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. The high-power nRF24s (with the external antenna) will definitely need an external power supply. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

I define my structs like this

struct PackageStruct
{
  int id;
  int cm;
};

PackageStruct data;

I don't think you can initialise values in a struct definition

...R

Robin2:
Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. The high-power nRF24s (with the external antenna) will definitely need an external power supply. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

I define my structs like this

struct PackageStruct

{
  int id;
  int cm;
};

PackageStruct data;




I don't think you can initialise values in a struct definition

...R

Thanks for the feedback. I had a look at it and tested your "Simple one way transmission" to double check my wireless communication and it works fine . The problem is with the sensor values which somehow is not being read correctly or send correctly. The ToF-mini lidar is using uart, and I dont know if it has something to do with that. Do you think it has something to do with how the byte is being transferred from the sensor to the receiver ?

If Robin's programs work OK then your problem is likely to be in in reading the sensor. Does your program work if you give fixed values to the variables that would normally be read from the sensor ?

Which pins on the Arduino does the sensor use ? You imply that it is using pins 0 and 1, but is that the case and can it be changed , or is it using SoftwareSerial ? If the latter, then how does the library know which pins are being used ? What voltage does the sensor use for its power and data lines ?

More details please

According to the library, the distance is an unsigned 16bit integer and you have to call "measure()" before you read the distance. You are using a wrong data type and measuring after you readout. Try this:

struct TF_PACKAGE { uint16_t id, cm; } data;

loop() {
  TFmini.measure();
  data.id++;
  data.cm = TFmini.getDistance();
  sendTheData();
  wait(500);
}