Hi all
Trying to make up a set of 2 way data transfer between an UNO (transmitter) and a Nano (Receiver).
100% certain the wiring is correct. Checked and checked again. Each nRF24L01 has it's own 3.3v supply, so no power issues.
But... zip. No data transfer.
I have been using this resource:
https://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/?utm_content=cmp-true
Transmitter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(10, 9); // CE, CSN
//const byte address[6] = "00001";
const byte addresses [][6] = {"00001", "00002"}; // Setting the two radio addresses (RX and TX)
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte a = 0;
byte b = 125;
byte c = 255;
int d = 1024;
float e = 3.141592;
String f = "Test";
};
Data_Package data; // Create a variable with the above structure
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[1]); // 00002
radio.openReadingPipe(1, addresses[0]); // 00001
radio.setAutoAck(true);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MIN); // RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, and RF24_PA_max.
//radio.startListening(); // Set the module as receiver
radio.stopListening(); // This sets the module as transmitter
}
void loop() {
// Send the whole data from the structure to the receiver
radio.write(&data, sizeof(Data_Package));
Serial.println("Transmit...");
delay(500);
}
Receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(10, 9); // CE, CSN
const byte addresses [][6] = {"00001", "00002"}; // Setting the two radio addresses (RX and TX)
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte a = 0;
byte b = 125;
byte c = 255;
int d = 1024;
float e = 3.141592;
String f = "Test";
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[0]); // 00001
radio.openReadingPipe(1, addresses[1]); // 00002
radio.setAutoAck(true);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MIN); // RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, and RF24_PA_max.
radio.startListening(); // Set the module as receiver
Serial.println("ONLINE");
delay(1000);
}
void loop() {
// Check whether there is data to be received
if (radio.available()) {
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
Serial.print("a: ");
Serial.print(data.a);
Serial.print(" b: ");
Serial.print(data.b);
Serial.print(" c: ");
Serial.print(data.c);
Serial.print(" d: ");
Serial.print(data.d);
Serial.print(" e: ");
Serial.print(data.e);
Serial.print(" f: ");
Serial.print(data.f);
data.a = 0;
data.b = 0;
data.c = 0;
data.d = 0;
data.e = 0;
data.f = "";
}
}
Any ideas?
Not sure what radio.setAutoAck(true); does. But either state makes no difference.
Others in the comments have said that #const byte addresses [][6] = {"00001", "00002"}; should actually be const byte addresses [][5] = {"00001", "00002"};
But that makes no difference either
The 2 way demo code on that webpage isn't very good. It displays the received variables, even if they are not arriving. At first I thought it all worked until I noticed that turning off the transmitter made no difference to the received displayed data.