communication between an arduino uno and mega using a NRF24L01 antenna

for a project that I’ve been developing i need to make an Arduino Uno and mega communicate, so I bought the antennas and watched a few youtube videos, so I downloaded the sketched and changed them a little bit for my testing.
in both arduinos i have a button and an led, the button from one Arduino turns on the led from the other Arduino.
when I tested the code it didn’t work.

this is the code that I have on the Arduino mega. the button is on pin 3 and the led the pin 4

/*
* Arduino Wireless Communication Tutorial
*     Example 2 - Transmitter Code
*                
* by Dejan Nedelkovski, www.HowToMechatronics.com
* 
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

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

boolean led = false;
boolean led1 = false;

RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};

void setup() {
  pinMode(3, INPUT_PULLUP);
  pinMode(4, OUTPUT);
  radio.begin();
  radio.openWritingPipe(addresses[1]); // 00002
  radio.openReadingPipe(1, addresses[0]); // 00001
  radio.setPALevel(RF24_PA_MIN);
}

void loop() {
  if(led1 == true){
    digitalWrite(4, HIGH);
  }

  if(digitalRead(3) == 0){
    led = true;
  }
  
  delay(5);

  radio.stopListening();
  radio.write(&led, sizeof(led));

  delay(5);
  radio.startListening();
  while (!radio.available());
  radio.read(&led1, sizeof(led1));

}

this is the code that I have on the Arduino Uno.

/*
* Arduino Wireless Communication Tutorial
*     Example 2 - Receiver Code
*                
* by Dejan Nedelkovski, www.HowToMechatronics.com
* 
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

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

boolean led = false;
boolean led1 = false;


RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};

void setup() {
  pinMode(3, INPUT_PULLUP);
  pinMode(4, OUTPUT);
  radio.begin();
  radio.openWritingPipe(addresses[0]); // 00001
  radio.openReadingPipe(1, addresses[1]); // 00002
  radio.setPALevel(RF24_PA_MIN);
}

void loop() {
  if(led == true){
    digitalWrite(4, HIGH);
  }

 if(digitalRead(3) == 0){
  led1 = true;
 }
  delay(5);
  radio.startListening();
  if ( radio.available()) {
    while (radio.available()) {
      radio.read(&led, sizeof(led));
    }
    delay(5);
    radio.stopListening();
    radio.write(&led1, sizeof(led1));
  }
}

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