Having problems with NRF24L01 module code

I was trying to learn the NRF24L01 module for my project. I was follwoing a totorial
(https://lastminuteengineers.com/nrf24l01-arduino-wireless-communication/#google_vignette)
but when I trid to compile the code I had a error stating "redefenition of RF 24 radio" I could not figure out what was going on I hope thios forum helps me.

//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 8); 

//address through which two modules communicate.
const byte address[6] = "00001";

void setup()
{
  radio.begin();
  
  //set the address
  radio.openWritingPipe(address);
  
  //Set module as transmitter
  radio.stopListening();
}
void loop()
{
  //Send message to receiver
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  
  delay(1000);
}//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 8); 

//address through which two modules communicate.
const byte address[6] = "00001";

void setup()
{
  radio.begin();
  
  //set the address
  radio.openWritingPipe(address);
  
  //Set module as transmitter
  radio.stopListening();
}
void loop()
{
  //Send message to receiver
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  
  delay(1000);
}

This is the code of the transmitter, I also get the same problem with the code of the reciever.

//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//create an RF24 object
RF24 radio(9, 8);  // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";

void setup()
{
  while (!Serial);
    Serial.begin(9600);
  
  radio.begin();
  
  //set the address
  radio.openReadingPipe(0, address);
  
  //Set module as receiver
  radio.startListening();
}

void loop()
{
  //Read the data if available in buffer
  if (radio.available())
  {
    char text[32] = {0};
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

The error messages:
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\arduino_wireless_test_reciever.ino:7:11: error: redefinition of 'RF24 radio'
RF24 radio(9, 8); // CE, CSN
^
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\sketch_aug21a.ino:6:6: note: 'RF24 radio' previously declared here
RF24 radio(9, 8);
^~~~~
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\arduino_wireless_test_reciever.ino:10:21: error: redefinition of 'const byte address [6]'
const byte address[6] = "00001";
^
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\sketch_aug21a.ino:9:12: note: 'const byte address [6]' previously defined here
const byte address[6] = "00001";
^~~~~~~
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\arduino_wireless_test_reciever.ino: In function 'void setup()':
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\arduino_wireless_test_reciever.ino:12:6: error: redefinition of 'void setup()'
void setup()
^~~~~
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\sketch_aug21a.ino:11:6: note: 'void setup()' previously defined here
void setup()
^~~~~
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\arduino_wireless_test_reciever.ino: In function 'void loop()':
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\arduino_wireless_test_reciever.ino:26:6: error: redefinition of 'void loop()'
void loop()
^~~~
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\sketch_aug21a.ino:21:6: note: 'void loop()' previously defined here
void loop()
^~~~

exit status 1

Compilation error: redefinition of 'RF24 radio'

Welcome to the forum

Please post the full error message

Have you by any chance got the transmitter and receiver sketches open in 2 tabs in the same instance of the IDE ?

Hello @want0f, in the first sketch are you using 2 times the same setup and loop? O suppose not but can you see in the library if RF24 already is defined? A redefinition means it is defined already somewhere. I´ll load the code up when I´m home, maybe then I´ll see the problem.

This line tells you where it thinks the problem is: "1f2n9k2.dnz6\sketch_aug21a\arduino_wireless_test_reciever.ino:7:11: error: redefinition of 'RF24 radio'
RF24 radio(9, 8); // CE, CSN" The 7:11:error tells me there is a problem around line 7 in the aforementioned information. Try to comment it out and see what happens.

Hi @want0f I've uploaded the code and I don't receive any errors:
I've installed following libraries:
RF24 by TMRh20 and BTLE by Florian Echtler, which libraries do you have installed?
Following I've uploaded the code in seperate sketches 1 for the transmitter and 1 for the receiver:

//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 8); 

//address through which two modules communicate.
const byte address[6] = "00001";

void setup()
{
  radio.begin();
  
  //set the address
  radio.openWritingPipe(address);
  
  //Set module as transmitter
  radio.stopListening();
}
void loop()
{
  //Send message to receiver
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  
  delay(1000);
}

1 Into another sketch:

//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//create an RF24 object
RF24 radio(9, 8);  // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";

void setup()
{
  while (!Serial);
    Serial.begin(9600);
  
  radio.begin();
  
  //set the address
  radio.openReadingPipe(0, address);
  
  //Set module as receiver
  radio.startListening();
}

void loop()
{
  //Read the data if available in buffer
  if (radio.available())
  {
    char text[32] = {0};
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

Greetings, Dingsken.

Look carefully at the error messages, of which this is an example

C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\arduino_wireless_test_reciever.ino:7:11: error: redefinition of 'RF24 radio'
RF24 radio(9, 8); // CE, CSN
^
C:\Users\user\AppData\Local\Temp.arduinoIDE-unsaved2023721-24744-1f2n9k2.dnz6\sketch_aug21a\sketch_aug21a.ino:6:6: note: 'RF24 radio' previously declared here
RF24 radio(9, 8);
^~~~~

Note that the IDE is accessing 2 sketches in the sketch folder. Either of them may be perfect but both of them contain at least some of the same definitions, hence the errors

1 Like

@UKHeliBob @want0f Yeah I suppose this is the problem, you have used 2 sketches with the same unsaved name. Save 1 as f.e. sketch_transmitter and the other as sketch_receiver.

thank you very much

1 Like

From your reply I assume that is the problem

When testing radio communication it is tempting to do what you did but it will not work because all of the .ino files open in the same instance of the IDE will be compiled together

Save each file in a different location. If you are using IDE 1.x then you can open both in separate instances of the IDE. Do not use file/new to open the second file otherwise both will use the same COM port and if you change it in one then it will also change in the other. Instead, open the first file then start a new instance of the IDE and open the second file in it. That way each sketch can use a different COM port

If you are using IDE 2.x then File/New will work OK as COM port changes for one sketch will not affect the other one

It looks like Windows makes that easy for apps that support it. Hold the shift key down when you do the thing that launches an app, presto! 2nd (or Nth) instance.

So far as bit of googling goes for MacOS tells me, it's not as simple.

But the terminal command open is the key. I have not tried this yet; I have enough trouble with one instance. cough!

At the command line prompt

whatever_your_prompt_is> open -n -a *nameOfApp*

HTH

a7

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