NRF24l01+ HC SR04+ Arduino uno interface

I'm trying to interface ultrasonic sensor HC SR04 and I just want to send a message using nrf24l01 transceiver to another two arduinos which has the same nrf24l01. When the distance is in between certain range for example 0-170 the the nrf will send a message to another two arduinos. I have the programs required for each of the devices but the problem is I don't know how to make use of different pipes of nrf24l01. Please help me with this and if not please direct me to the post which can help me out. Thank you.

For sending a message from one Arduino to two others there is no need to use multiple pipes. In fact for very many applications there is no need to use multiple pipes.

You have not said if you need 2-way communication.

Have a look at the 2nd example in this Simple nRF24L01+ Tutorial. There is also an example to illustrate how to extend that to 2 slaves.

…R

Thank you. Your tutorial has all the required information :)

Hi I was able to get both NRF and ultrasonic sensor work together but I had assigned a message to be typed only if the distance was below certain value but even for the values above that I’m getting the same message. Can someone please help me on what I’m doing wrong.
I’m new to Arduino and programming please help me out.
Thank you.

#include <RF24.h>
RF24 radio(7, 8);
const byte rxAddr[6] = "00001";
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
const char text[] = "Accident zone";
const char text1[] = "Safe";
void setup() 
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(rxAddr);
  radio.stopListening();
  
}
void loop() {
digitalWrite(trigPin, LOW);
delay(100);
digitalWrite(trigPin, HIGH);
delay(500);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
if (distance<5  )
{
  
radio.write(&text, sizeof(text));
  
}
else 
radio.write(&text1, sizeof(text1));

}

I’m getting output as Accident Zone even with distance less than 5cm.

Nagi2195: I'm getting output as Accident Zone even with distance less than 5cm.

How do you know?

Put

Serial.print("Distance ");
Serial.println(distance);

into your program and see what values it is getting

Almost certainly this line

distance= duration*0.034/2;

gives a value of 0 because you are using integer values

...R

Robin2: gives a value of 0 because you are using integer values

Yes now it is showing Distance 0 Edit: I changed distance=duration/29/2 Still it is showing 0

What is the value of the variable duration?

Why bother with 2 divisions when you could just do duration / 58

...R

Robin2: What is the value of the variable duration?

For a distance of 10 the duration would be around 580. Edit: Finally Got it working by changing the trigger and echo pins. Thanks a lot for helping me out.

Why do you call stopListening() in setup?

void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(rxAddr);
  radio.stopListening();

}

Typing Ctrl-T in the IDE formats your code more consistently than your fingers alone.

Added:

In radio.open*WritingPipe(rx*Addr); I see a small naming/understanding problem.

I would not assume that a variable named rxAddr contains a target address, maybe in a couple of months you get the same impression. Why not call it alarmAddress?

I have two transmitters and two Receivers. The receiver 1 has to accept datas from T1 and T2 simultaneously. Similarly receiver2 has to accept a message from T2 and T1. Is this process possible? If it is then do I need to use pipes? I'm new to the programming sorry if it has been already answered. Thank you.

Nagi2195: I have two transmitters and two Receivers.

Does that mean you have 4 Arduinos?

If so, please post a diagram showing the roles of each and the communication directions

The receiver 1 has to accept datas from T1 and T2 simultaneously.

An nRF24 has only 1 wireless so it can only receive one message at a time. If T1 and T2 send at the same time both messages will be garbled. You can easily have a program that receives messages quickly one after the other so it appears simultaneous to a human.

Have you tried the multi-slave example in my Tutorial? It is designed to avoid two slaves transmitting at the same time.

...R

Robin2: ... the multi-slave example in my Tutorial? It is designed to avoid two slaves transmitting at the same time.

For this scenario you will have to expand the example to work with two masters, which will be hard to synchronize. ;)

The receiving nodes could both open a pipe without autoack and the sensor nodes could send messages to this pipe (again without ack). Inside the message should be information about the sending node and the detected event.

That way any number of clients can listen to that channel, not only two.

The sensors do not really need to know whether their message was received (by any node), they could just repeat that message once a second (or more often) while the condition is true.

Like a PIR that does not really care whether someone can get its output signal.

Robin2:
Does that mean you have 4 Arduinos?

If so, please post a diagram showing the roles of each and the communication directions

Yes I’m using four Arduinos.
Here’s the diagram in the attachment.

New Doc 2017-05-21.pdf (277 KB)

Whandall: The receiving nodes could both open a pipe without autoack and the sensor nodes could send messages to this pipe (again without ack). Inside the message should be information about the sending node and the detected event.

That way any number of clients can listen to that channel, not only two.

The sensors do not really need to know whether their message was received (by any node), they could just repeat that message once a second (or more often) while the condition is true.

Like a PIR that does not really care whether someone can get its output signal.

Is there any way so that I can store a message in particular address and make the receiver to read only the data in that particular address?

You don't answer my questions, why should I answer yours? (see #8)

Whandall: You don't answer my questions, why should I answer yours? (see #8)

Sorry sir, I had got everything working and had even posted about that just before your post so didn't see that. And for the post #8 I was using rxAddr to store a particular address. I used stoplistening() because I used that particular transceiver to only transmit the data and there was another receiver to receive that particular data. It was meant only for obe way communication.

You should use stopListening() only to get back to idle after a call to startListening().

You should study the datatsheet of the NRF and the documentation of the library.

Nagi2195: Is there any way so that I can store a message in particular address and make the receiver to read only the data in that particular address?

You can not store messages in addresses.

You can send messages to an address (with and without acknowledge). You can listen to up to six pipe addresses at the same time (again with and without auto-acknowledge).

If some stations listen to a pipe, a message sent to that address will be copied by all listeners. This will not work with autoacknowledge, because all listeners would try to acknowledge, so autoack should be suppressed in sender and receiver on broadcast addresses.

Whandall: You should use stopListening() only to get back to idle after a call to startListening().

You should study the datatsheet of the NRF and the documentation of the library.

You can not store messages in addresses.

You can send messages to an address (with and without acknowledge). You can listen to up to six pipe addresses at the same time (again with and without auto-acknowledge).

If some stations listen to a pipe, a message sent to that address will be copied by all listeners. This will not work with autoacknowledge, because all listeners would try to acknowledge, so autoack should be suppressed in sender and receiver on broadcast addresses.

Ok thanks for the information. Will try it out.

For the future please post images as JPG or PNG files so we don't have to download them. See this Image Guide

I had no sense from your Original Post that there were 4 Arduinos involved. It just said

When the distance is in between certain range for example 0-170 the the nrf will send a message to another two arduinos.

But your diagram only shows a Tx sending to a single Rx.

At the moment this a typical XY problem

Please explain in simple terms what the project is all about.

...R