Problem in sending structure using nRF24L01

I just try to control a car wirelessly using nRF24L01 module with Arduino Uno. But i don't have a joystick module now so I use two potentiometers , one potmeter for forward and backward control and another potmeter for right and left control. I stored the potentiometer values in structure form and try to send it to the receiver .but it does not send and receive anything.

I did not complete the entire program , just now i started to write the program . using the program i attached below, i try to make sure that the transmitter transmits whole structure correctly and receiver receives whole structure correctly .

I don't no where the problem really occurs .Nothing is printed on serial port .

Transmitter code:

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

const int CEpin=9;
const int CSNpin=10;
RF24 radio(CEpin,CSNpin);
const byte address[]={"00001"};

const int Potpin1=A0;
const int Potpin2=A1;

int Readval1=0;
int Readval2=0;

struct motorWriting
{
  int Right=0;
  int Left=0;
  int Forward=0;
  int Backward=0;
}dataTosend;

void setup() {

pinMode(Potpin1,INPUT);
pinMode(Potpin2,INPUT);

Serial.begin(9600);
radio.begin();
radio.openWritingPipe(&address);
radio.startListening();
}

void loop() {

Readval1=analogRead(Potpin1);
Readval2=analogRead(Potpin2);
 
if(0<=Readval1 && Readval1<=450 )
  dataTosend.Backward=map(Readval1,0,450,0,255);
if(574<=Readval1 && Readval1<=1023)
  dataTosend.Forward=map(Readval1,574,1023,0,255);
if(0<=Readval2 && Readval2<=450)
  dataTosend.Left=map(Readval2,0,450,0,255);
if(574<=Readval2 && Readval2<=1023)
  dataTosend.Right=map(Readval2,574,1023,0,255);


if(radio.write(&dataTosend,sizeof(dataTosend)))
{
  Serial.print("\nTransmitted Data is : ");
  Serial.print("\nRight = ");Serial.print(dataTosend.Right);
  Serial.print("\nLeft  = ");Serial.print(dataTosend.Left);
  Serial.print("\nForward  = ");Serial.print(dataTosend.Forward);
  Serial.print("\nBackward = ");Serial.print(dataTosend.Backward);
  Serial.print("\n\n");
}

}

Receiver code:

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

const int CEpin=9;
const int CSNpin=10;
RF24 radio(CEpin,CSNpin);
const byte address[]={"00001"};

struct motorReading
{
int Right=0;
int Left=0;
int Forward=0;
int Backward=0;
   
}dataToread;



void setup() {
  
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,&address);
radio.startListening();

}

void loop() {

if(radio.available())
{
  radio.read(&dataToread,sizeof(dataToread));
  Serial.print("\nReceived Data is : ");
  Serial.print("\nRight = ");Serial.print(dataToread.Right);
  Serial.print("\nLeft  = ");Serial.print(dataToread.Left);
  Serial.print("\nForward  = ");Serial.print(dataToread.Forward);
  Serial.print("\nBackward = ");Serial.print(dataToread.Backward);
  Serial.print("\n\n");  
}

}

what I have to correct in this program to transmit and receive a structure. I don't bother about the other part of the program

Do the nRF24L01s work, transmit and receive, with the basic examples in the RF24 library ?

both the modules works very well @srnet

You could try putting a short delay() at the end of the transmitter loop().
Are you using the same board type on both the transmitter and receiver.? If not, the data type 'int' might not match. Best to use int16_t then it's clear.
Otherwise, systematically rebuild your code from something that works, until it breaks.

And don't forget to pack the structures to byte boundaries.

} __attribute__((packed, aligned(1)));

In the transmitter should absolutely be

radio.stopListening();

1 Like

yes, I am using Arduino Uno for both transmitter and receiver

I can not understand what do you mean? Can you please explain

That is only necessary for a mixture of 32 bit and 8 bit clients.

What happened when you changed the startListening to stop Listening?

how can i use array instead of structure in this program to store different variables ?
how to send and receive a array of different variables? @Whandall

[quote="Whandall, post:9, topic:908958"]
What happened when you changed the startListening to stop Listening?
[/quote

I changed startListening to stopListening and also tried with delay() at the end of the void loop () in transmitter .There is no improvement .

Show the code which you used to verify the operation of the nrf24L01 devices and some one will help you to integrate a struct into it.

How are you powering your NRFs?

The flimsy 3.3V supply from a standard UNO may not be enough.

If you absolutely know that the modules work (how do you know that ?) then these two sketches will work

Tx a struct :

// SimpleTx - the master or the transmitter

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

//not all #defines used but here for documentation
#define CE_PIN   9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char txNum = '0';

struct data
{
  byte pinState;
  byte temperature;
} sentData;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;

void setup()
{
  Serial.begin(115200);
  Serial.println("SimpleTx Starting");
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - prevMillis >= txIntervalMillis)
  {
    sentData.pinState = !sentData.pinState;
    sentData.temperature = sentData.temperature + 1;
    send();
    prevMillis = millis();
  }
}

void send()
{
  bool rslt;
  rslt = radio.write( &sentData, sizeof(sentData) );
  if (rslt)
  {
    Serial.println("  Acknowledge received");
  }
  else
  {
    Serial.println("  Tx failed");
  }
}

Rx a struct :

// SimpleRx - the slave or the receiver

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

//not all #defines used but here for documentation
#define CE_PIN   9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct data
{
  byte pinState;
  int temperature;
} receivedData;

bool newData = false;

void setup()
{
  Serial.begin(115200);
  Serial.println("SimpleRx Starting");
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();
}

void loop()
{
  getData();
  showData();
}

void getData()
{
  if ( radio.available() )
  {
    radio.read( &receivedData, sizeof(receivedData) );
    newData = true;
  }
}

void showData()
{
  if (newData == true)
  {
    Serial.print("Data received\tpinState : ");
    Serial.print(receivedData.pinState);
    Serial.print("\t");
    Serial.print("temperature : ");
    Serial.println(receivedData.temperature);
    newData = false;
  }
}

I believe that they were originally taken from Simple nRF24L01+ 2.4GHz transceiver demo

I tested the modules with

Transmitter :

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

const byte CEpin=9;
const byte CSNpin=10;

RF24 radio(CEpin,CSNpin);

const byte address[]={"00001"};
char buf[10]={"hi...!!!!!"};
//uint8_t address[]={"00001"};

void setup() {

Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address[0]);
radio.setChannel(124);
radio.setDataRate(RF24_2MBPS);
radio.setPALevel(RF24_PA_LOW);
radio.setPayloadSize(sizeof(buf));
radio.enableDynamicPayloads();
//radio.enableAckPayload();
radio.startListening();
radio.setCRCLength(RF24_CRC_16);//CRC 8 dose not works
}

void loop() {

radio.stopListening();
int a=radio.getPayloadSize();
Serial.print("\nPayload Size :");Serial.print(a);

int x=radio.getCRCLength();
Serial.print("\nCRC length :");Serial.print(x);

if(!radio.write(buf,sizeof(buf)))
   Serial.print("\nError......message not sent");   
else  
{
  Serial.print("\nmessage sent successfully");
  Serial.print("\nSent Data : ");Serial.print(buf);
}
radio.stopListening();
Serial.print("\n\n\n");
delay(500);
}

Receiver:

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

const byte CEpin=9;
const byte CSNpin=10;

RF24 radio(CEpin,CSNpin);

const byte address[]={"00001"};
char buf[10];
//uint8_t address[]={"00001"};


void setup() {

Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,address[0]);
radio.setChannel(124);
radio.setDataRate(RF24_2MBPS);
radio.setPALevel(RF24_PA_LOW);
radio.enableDynamicPayloads();
radio.startListening(); 
radio.setCRCLength(RF24_CRC_16);//CRC 8 dose not works
}

void loop() {

int a=radio.getDynamicPayloadSize();
Serial.print("\nDynamic Payload Size :");
Serial.print(a);

int x=radio.getCRCLength();
Serial.print("\nCRC length :");
Serial.print(x);

if(radio.available())
{
  while(radio.available(address[0]))
  {
    Serial.print("\nReceiving Data at pipe address :  ");
    Serial.print("00001");
    radio.read(buf,sizeof(buf));
    Serial.print("\nReceived Data : ");
    Serial.print(buf);
  }
}
Serial.print("\n\n\n");
delay(500);

}

I slightly adjusted your sketches (baud rate, debug info, etc.) and tested them.

Sender

#include <printf.h>
#include <RF24.h>

const int CEpin = 9;
const int CSNpin = 10;
RF24 radio(CEpin, CSNpin);
const byte address[] = {"00001"};

const int Potpin1 = A0;
const int Potpin2 = A1;

uint16_t Readval1;
uint16_t Readval2;

struct motorWriting {
  int Right;
  int Left;
  int Forward;
  int Backward;
  void display(const __FlashStringHelper* titlePROGMEM) {
    Serial.print(titlePROGMEM);
    Serial.print(F(" right = "));
    Serial.print(Right);
    Serial.print(F(", left  = "));
    Serial.print(Left);
    Serial.print(F(", forward  = "));
    Serial.print(Forward);
    Serial.print(F(", backward = "));
    Serial.println(Backward);
  }
} dataToSend;

void setup() {
  Serial.begin(115200);
  printf_begin();
  radio.begin();
  radio.openWritingPipe(address);
  //radio.setAutoAck(false);  // don't require a working receiver
  radio.stopListening();
  radio.printDetails();
}

void loop() {
  static uint32_t lastSent;
  uint32_t topLoop = millis();
  if (topLoop - lastSent >= 1000) {
    lastSent = topLoop;

    Readval1 = analogRead(Potpin1);
    if (Readval1 <= 450 ) {
      dataToSend.Backward = map(Readval1, 0, 450, 0, 255);
    }
    if (574 <= Readval1) {
      dataToSend.Forward = map(Readval1, 574, 1023, 0, 255);
    }

    Readval2 = analogRead(Potpin2);
    if (Readval2 <= 450) {
      dataToSend.Left = map(Readval2, 0, 450, 0, 255);
    }
    if (574 <= Readval2) {
      dataToSend.Right = map(Readval2, 574, 1023, 0, 255);
    }

    if (radio.write(&dataToSend, sizeof(dataToSend))) {
      dataToSend.display(F("Transmitted Data was : "));
    } else {
      Serial.println(F("Transmit failed."));
    }
  }
}

Output with running receiver:

09:43:36.222 -> SPI Speedz	= 10 Mhz
09:43:36.222 -> STATUS		= 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
09:43:36.222 -> RX_ADDR_P0-1	= 0x3130303030 0x6d6f632d5a
09:43:36.222 -> RX_ADDR_P2-5	= 0x2a 0xc4 0xc5 0xc6
09:43:36.222 -> TX_ADDR		= 0x3130303030
09:43:36.222 -> RX_PW_P0-6	= 0x20 0x20 0x20 0x20 0x20 0x20
09:43:36.222 -> EN_AA		= 0x3f
09:43:36.222 -> EN_RXADDR	= 0x03
09:43:36.222 -> RF_CH		= 0x4c
09:43:36.222 -> RF_SETUP	= 0x07
09:43:36.222 -> CONFIG		= 0x0e
09:43:36.222 -> DYNPD/FEATURE	= 0x00 0x00
09:43:36.222 -> Data Rate	= 1 MBPS
09:43:36.256 -> Model		= nRF24L01+
09:43:36.256 -> CRC Length	= 16 bits
09:43:36.256 -> PA Power	= PA_MAX
09:43:36.256 -> ARC		= 1
09:43:37.206 -> Transmitted Data was :  right = 0, left  = 140, forward  = 0, backward = 159
09:43:38.187 -> Transmitted Data was :  right = 0, left  = 126, forward  = 0, backward = 128
09:43:39.205 -> Transmitted Data was :  right = 0, left  = 113, forward  = 0, backward = 113
09:43:40.187 -> Transmitted Data was :  right = 0, left  = 104, forward  = 0, backward = 104
09:43:41.200 -> Transmitted Data was :  right = 0, left  = 100, forward  = 0, backward = 101
09:43:42.214 -> Transmitted Data was :  right = 0, left  = 96, forward  = 0, backward = 98
09:43:43.197 -> Transmitted Data was :  right = 0, left  = 98, forward  = 0, backward = 98
09:43:44.212 -> Transmitted Data was :  right = 0, left  = 96, forward  = 0, backward = 97

Receiver

#include <printf.h>
#include <RF24.h>

const int CEpin = 9;
const int CSNpin = 10;
RF24 radio(CEpin, CSNpin);
const byte address[] = {"00001"};

struct motorReading {
  int Right;
  int Left;
  int Forward;
  int Backward;
  void display(const __FlashStringHelper* titlePROGMEM) {
    Serial.print(titlePROGMEM);
    Serial.print(F(" right = "));
    Serial.print(Right);
    Serial.print(F(", left  = "));
    Serial.print(Left);
    Serial.print(F(", forward  = "));
    Serial.print(Forward);
    Serial.print(F(", backward = "));
    Serial.println(Backward);
  }
} dataToRead;

void setup() {
  Serial.begin(115200);
  printf_begin();
  radio.begin();
  radio.openReadingPipe(1, address);
  radio.startListening();
  radio.printDetails();
}

void loop() {
  static uint32_t lastReception;
  uint32_t topLoop = millis();
  if (radio.available()) {
    radio.read(&dataToRead, sizeof(dataToRead));
    dataToRead.display(F("Received Data is : "));
    lastReception = topLoop;
  }
  if (topLoop - lastReception >= 10000) {
    Serial.println(F("no reception for ten seconds"));
    lastReception = topLoop;
  }
}

Output with running transmitter

09:41:04.302 -> SPI Speedz	= 10 Mhz
09:41:04.302 -> STATUS		= 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
09:41:04.302 -> RX_ADDR_P0-1	= 0xe7e7e7e7e7 0x3130303030
09:41:04.302 -> RX_ADDR_P2-5	= 0xc3 0xc4 0xc5 0xc6
09:41:04.335 -> TX_ADDR		= 0xe7e7e7e7e7
09:41:04.335 -> RX_PW_P0-6	= 0x20 0x20 0x20 0x20 0x20 0x20
09:41:04.335 -> EN_AA		= 0x3f
09:41:04.335 -> EN_RXADDR	= 0x02
09:41:04.335 -> RF_CH		= 0x4c
09:41:04.335 -> RF_SETUP	= 0x07
09:41:04.335 -> CONFIG		= 0x0f
09:41:04.335 -> DYNPD/FEATURE	= 0x00 0x00
09:41:04.335 -> Data Rate	= 1 MBPS
09:41:04.335 -> Model		= nRF24L01+
09:41:04.335 -> CRC Length	= 16 bits
09:41:04.335 -> PA Power	= PA_MAX
09:41:04.335 -> ARC		= 0
09:41:04.642 -> Received Data is :  right = 0, left  = 99, forward  = 0, backward = 100
09:41:05.624 -> Received Data is :  right = 0, left  = 98, forward  = 0, backward = 98
09:41:06.638 -> Received Data is :  right = 0, left  = 95, forward  = 0, backward = 94
09:41:07.620 -> Received Data is :  right = 0, left  = 93, forward  = 0, backward = 92

I used Nanos with a separate 3.3V supply for the NRFs.

always i am using 3.3 v from the arduino uno board.

i did not faced any issue while using 3.3 v suply from arduino uno

What is the output of my code on your hardware?

What type of NRF module are you using?
The high power ones definitely won't work with 3.3V of a standard UNO (clone).

i am also getting result like you , but i can't understand how and where did you get those informations? (like SPI Speedz,STATUS,ect..)

By calling radio.printDetails() after initializing the printf interface that this function uses.

It would be helpful if you post the generated output.