Go Down

Topic: NRF24l01+ HC SR04+ Arduino uno interface (Read 295 times) previous topic - next topic

Nagi2195

 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.

Robin2

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


Two or three hours spent thinking and reading documentation solves most programming problems.

Nagi2195

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

Nagi2195

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.
Code: [Select]
#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.

Robin2

I'm getting output as Accident Zone even with distance less than 5cm.
How do you know?

Put
Code: [Select]
Serial.print("Distance ");
Serial.println(distance);

into your program and see what values it is getting

Almost certainly this line
Code: [Select]
distance= duration*0.034/2;
gives a value of 0 because you are using integer values

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Nagi2195

#5
May 19, 2017, 06:16 pm Last Edit: May 19, 2017, 07:43 pm by Nagi2195
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

Robin2

What is the value of the variable duration?

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

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Nagi2195

#7
May 20, 2017, 12:29 am Last Edit: May 20, 2017, 02:08 am by Nagi2195
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.

Whandall

#8
May 20, 2017, 08:45 am Last Edit: May 20, 2017, 08:55 am by Whandall
Why do you call stopListening() in setup?

Code: [Select]
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.openWritingPipe(rxAddr); 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?
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Nagi2195

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.

Robin2

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


Quote
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
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

... 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.  ;)
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

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.

Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Nagi2195

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.

Nagi2195

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?

Go Up