srijay
June 18, 2018, 11:28am
1
Hello,
I have being trying to establish communication between two NRF modules, but some reasons i am not able to rectify the problem.
According to me the program has no issues.
Please can anyone help !
Ill attach my code below for Tx and Rx.
[b]Transmitter:[/b]
#include <NewPing.h>
#include <MedianFilter.h>
#include <Wire.h>
#include <MedianFilter.h>
#define TRIGGER_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 450 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
MedianFilter filter(31,0);
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
float dataToSend;
float txNum ;
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(4,15); // delay, count
radio.openWritingPipe(0xF0F0F0F0FF);
}
void loop() {
updateMessage();
delay(50);
send();
delay(100);
}
void send()
{
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend));
Serial.print("Data Sent");
Serial.print(dataToSend);
if (rslt)
{
Serial.println("Acknowledge received");
}
else
{
Serial.println("Tx failed");
}
}
void updateMessage()
{
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int txNum,uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
filter.in(uS);
txNum = filter.out();
Serial.print("Ping: ");
//Serial.print( txNum / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//Serial.println("cm");
dataToSend = txNum / US_ROUNDTRIP_CM;
}
[b]Receiver[/b]
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
int dataReceived;
bool newData = false;
void setup() {
Serial.begin(115200);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
}
void loop()
{
radio.openReadingPipe(0, 0xF0F0F0F0FF);
radio.startListening();
Serial.print("Pipe");
getData();
showData();
delay(1000);
}
void getData() {
if ( radio.available())
{
radio.read(&dataReceived, sizeof(dataReceived));
newData = true;
}
}
void showData()
{
if (newData == true)
{
Serial.print("Data received");
Serial.println(dataReceived);
newData = false;
}
}
[tt][/tt]
finalreceiver1.ino (834 Bytes)
transmitter1.ino (1.83 KB)
Robin2
June 18, 2018, 12:36pm
2
Have a look at this Simple nRF24L01+ Tutorial .
The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with.
...R
srijay
June 18, 2018, 12:50pm
3
Actually its similar to your program. There are some things which i have changed in Transmitter module. That's all.
I am not getting any errors in program.
The problem is its not able to communicate.
Move
radio.openReadingPipe(0, 0xF0F0F0F0FF);
radio.startListening();
from loop to setup.
So the receiver at least gets a chance to receive something.
Robin2
June 18, 2018, 3:48pm
5
srijay:
Actually its similar to your program.
I did not recognize it because my examples don't use NewPing.
Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.
Also start by getting the example code to work without any changes.
...R
srijay
June 18, 2018, 4:41pm
6
Whandall:
Move
radio.openReadingPipe(0, 0xF0F0F0F0FF);
radio.startListening();
from loop to setup.
So the receiver at least gets a chance to receive something.
Thank you for the suggestion.
Its actually working but the receiver is not receiving the exact data.
I mean it should receive some sensor value but received 0 is displayed
srijay
June 18, 2018, 5:43pm
7
Hey thanks Whandall . Its working perfectly fine.
Thank you.
Appreciate you !
srijay
June 19, 2018, 11:42am
8
Hello,
It is necessary to connect 10uf capacitor to the Vcc and Gnd pin of NRF module.
In my case the connections are correct.
Problem is it only Send data for the first time. Then it automatically disconnects.
Please suggest any solution.
Thank you.
I suggest that you post your code for both ends. Then we have something to go on.
srijay
June 19, 2018, 2:29pm
10
Receiver
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
float dataReceived;
bool newData = false;
void setup()
{
Serial.begin(115200);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(0, 0xF0F0F0F0FF);
radio.startListening();
}
void loop()
{
Serial.print("Pipe");
getData();
showData();
delay(1000);
}
void getData()
{
if ( radio.available())
{
radio.read(&dataReceived, sizeof(dataReceived));
newData = true;
}
}
void showData()
{
if (newData == true)
{
Serial.print("Data received");
Serial.println(dataReceived);
newData = false;
}
}
Transmitter
#include <NewPing.h>
#include <MedianFilter.h>
#include <Wire.h>
#include <MedianFilter.h>
#define TRIGGER_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
MedianFilter filter(31,0);
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
float dataToSend;
float txNum ;
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(4,15); // delay, count
radio.openWritingPipe(0xF0F0F0F0FF);
}
void loop() {
updateMessage();
delay(50);
send();
delay(100);
}
void send()
{
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend));
Serial.print("Data Sent");
Serial.print(dataToSend);
if (rslt)
{
Serial.println("Acknowledge received");
}
else
{
Serial.println("Tx failed");
}
}
void updateMessage()
{
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int txNum,uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
filter.in(uS);
txNum = filter.out();
Serial.print("Ping: ");
//Serial.print( txNum / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//Serial.println("cm");
dataToSend = txNum / US_ROUNDTRIP_CM;
}
srijay
June 19, 2018, 2:36pm
11
Only when i press reset button on the Tx side the first acknowledgement is received.
Then no communication takes place.......
Confused......
Please find the screenshot of COM port......
Robin2
June 19, 2018, 4:30pm
12
That looks very like the code in your other Thread . Why have you started a new Thread? I am not going to answer the same questions twice.
I suggest you click Report to Moderator and ask to have the two Threads merged. Then we will have all the info in one place.
...R
Robin2
June 19, 2018, 5:02pm
13
Please display your image(s) in your post so we can see it(them) without downloading it(them). See this Simple Image Guide
If the image is simply a picture of text, then please just copy and paste the text as it makes it much easier to read.
Have you got your two Arduinos communicating perfectly with one of the examples from my tutorial with no changes and no additions to the code?
...R
srijay
June 20, 2018, 5:04am
14
No i m not getting the output.
But i am able to get response from the example Getting started.
Robin2
June 20, 2018, 7:25am
15
srijay:
No i m not getting the output.
But i am able to get response from the example Getting started.
I presume this refers to my example programs.
If so please post the two program that YOU have uploaded to your Arduinos and also post an example of the output that each program produces.
I can understand if all this seems very tedious but debugging wireless problems is not easy.
Have you tried the connection-test program? IIRC it is in Reply #29 in my tutorial?
Have you a spare nRF24 that you can use to check if one of the others is faulty?
...R