Problem in 2 way comunication between nrf24l01+

First of all, I would like to add that this is my first post and first 'bigger' project. I have set up two Arduinos connected wirelessly with an NRF module. Arduino 2 sends distance, and Arduino 1 turns on 240V. I want to double check if the 240V is on and if it is turn on an LED on arduino 2. I can easily communicate to Arduino 1 by sending the distance, but I can't get any information back from Arduino 1. It seems like Arduino 2 isn't receiving any data because Arduino 1 isn't even sending it. Why is that?
arduino 1

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <avr/wdt.h>

RF24 radio(7, 8);  // CE, CSN

const byte address[6] = "00001";
const byte address1[6] = "00002";
const unsigned long ONE_MINUTE = 5000;  // Duration of one minute in milliseconds

unsigned long lastTrueTime = 0;  // Variable to store the time when open became true
bool open = false;               // Initialize open as false
bool buffor = false;             // Initialize buffer as false

void setup() {
  wdt_enable(WDTO_8S);
  Serial.begin(9600);

  // Initialize radio and check if initialization was successful
  if (!radio.begin()) {
    Serial.println("Failed to initialize radio!");
    // Hang here forever if initialization fails
  }

  radio.openReadingPipe(0, address);
  radio.openWritingPipe(address1);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  pinMode(5, OUTPUT);
}

void loop() {
  radio.startListening();
  unsigned long currentMillis = millis();

  unsigned int text;

  if (radio.available()) {
    radio.read(&text, sizeof(text));  // Read the received data
    Serial.println(text);
    if (text < 10 and text > 2 and !open and !buffor) {
      open = true;
      lastTrueTime = currentMillis;  // Update the last time open became true
      buffor = true;                 // Set buffer to true when the door is first opened
    }
    if (text > 10 || text < 2) {
      open = false;
      buffor = false;  // Reset buffor to false when the door is closed
    }
  }
  int openS = digitalRead(5);
  if (radio.write(&openS, sizeof(open))) {
    Serial.println("Y");
  }
  radio.stopListening();

  if (open) {
    digitalWrite(5, HIGH);
    // Check if open has been true for more than one minute
    if (currentMillis - lastTrueTime >= ONE_MINUTE) {
      open = false;  // Set open to false if it has been true for one
    }
  } else {
    digitalWrite(5, LOW);
  }
  wdt_reset();
}

arduino 2

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <NewPing.h>
#include <avr/wdt.h>

#define trigPin 4
#define echoPin 3
#define DISTANCE 50

NewPing sonar(trigPin, echoPin, DISTANCE);
RF24 radio(7, 8);  // CE, CSN

const byte address[6] = "00001";
const byte address1[6] = "00002";

void setup() {
  wdt_enable(WDTO_4S);
  Serial.begin(9600);

  if (!radio.begin()) {
    Serial.println("Failed to initialize radio!");
    // Hang here forever if initialization fails
  }

  radio.openWritingPipe(address);
  radio.openReadingPipe(1, address1);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  unsigned int distance = sonar.ping_cm();
  radio.stopListening();
  // Send the distance measurement wirelessly
  bool success = radio.write(&distance, sizeof(distance));
  Serial.println(distance);
  if (success) {
    Serial.println("Distance sent successfully");
    return;
  } else {
    Serial.println("F");
  }
  radio.startListening();
  int open;
  if (radio.available()) {
    radio.read(&open, sizeof(open));
    Serial.println(open);
  }

  if (open) {
    digitalWrite(5, HIGH);
  } else {
    digitalWrite(5, LOW);
  }
  wdt_reset();
}

as far as i can see everything goes downhill when i put stoplistening before

  int openS = digitalRead(5);
  if (radio.write(&openS, sizeof(open))) {
    Serial.println("Y");
  }

open is a bool variable in your TX code. It's size is 1.

thanks, changed that

Indeed. It looks like your code spends almost all its time with listening off. When you turn it on, there's nothing there so it ends loop quickly and goes back to not listening. Try sending less frequently.

Can you post an annotated schematic showing all hardware parts including caps and resistors. Be sure to show all power sources and connections. Many times I have seen similar problems that eventually were hardware problems.

I tried that and it worked, but why?


thats for code 2


code 1

There might be problem with the pins, i have no idea how to change pins in that app

Nice pictures useless as schematics! My modules do not look like that

I do not know, probably the moon and sun were in the correct quadrants
. This should help:
Gil's Crispy Critter Rules, they apply to processor hardware:
Rule #1. A Power Supply the Arduino is NOT!
Rule #2. Never Connect Anything Inductive to an Arduino!
Rule #3 Don't connecting or disconnecting wires with power on.
Rule #4 Do not apply power to any pin unless you know what you are doing.
LaryD's Corollary's
Coro #1 when first starting out, add a 220R resistor in series with both Input and Output pins.
Coro #2 buy a DMM (Digital Multi-meter) to measure voltages, currents and resistance. Violating these rules tends to make crispy critters out of Arduinos.
Hint: It is best to keep the wires under 25cm/10" for good performance.
For starters you are violating rule #1.

The Arduino's 3.3V pin cannot supply enough current to reliably run an nRF24L01 module. Even with a 10uF cap across the module's pins, you might get lucky, you might not. And your diagram doesn't show such a cap. i would suggest using a proper external 3.3V regulator circuit, with appropriate bypass caps and a 10uF across the module's power pins.

I use, with good success, homemade versions of these power adapters.

Did you get the radio modules to talk by themseves before adding all the othter code.

I got my radios to work by carefully following Robin2's simple rf24 tutorial.

Use the sketch in post #30 to confirm physical connection (wiring) between a radio module and its attaced processor.

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