Arduino Mega - NRF- LCD communication

Hello,

I am working on communication project. where I used Arduino Mega, 16*2 LCD and NRF sensor. When I check it on the circuit, I am not getting anything. It just read the value detected by ultrasonic sensor.

I am not sure if this code is write or not. Please guide me to solve it. Aim: Nrf is acting as transmitter as well as receiver both in this project.
Transmitter

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

#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.
#define CE_PIN   22
#define CSN_PIN 24

LiquidCrystal lcd(8,9,4,5,6,7); 
RF24 radio(22, 24); // CE, CSN


const byte addresses[][6] = {"00001", "00002"};
char dataToSendforcollision1[] = "Collision  Alert";
char dataToSendforcollision2[] = "Lower the speed!";
char dataToSendforovertake1[]  =  " Car Overtaking ";
char dataToSendforovertake2[]  =  "Plan to overtake";
char dataReceived[16]; // this must match dataToSend in the TX
//ultrasonic 1
int echoPin1 =47;
int initPin1 =46;
int distance1 =0;

//ultrasonic 2
int echoPin2 =49;
int initPin2 =48;
int distance2 =0;

//ultrasonic 3
int echoPin3 =44;
int initPin3 =45;
int distance3 =0;

void setup() {
  lcd.begin(16,2);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  lcd.clear();
  lcd.setCursor(0,0);           // set cursor to column 0, row 1
  lcd.print("Dist(cm): "); // Writing on LCD
  pinMode(initPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(initPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(initPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  Serial.begin(9600);
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openWritingPipe(addresses[1]); // 00002
  radio.openReadingPipe(1, addresses[0]); // 00001
  radio.setPALevel(RF24_PA_MIN); 
}

void loop() {
 
  distance1 = getDistance(initPin1, echoPin1);
  lcd.setCursor(12,0);           // set cursor to column 12, row 0 (the first row)
  lcd.print("L: ");              // change this text to whatever you like. keep it clean.
  lcd.setCursor(14,0);
  lcd.print(distance1);
  delay(10);
 
  distance2 = getDistance(initPin2, echoPin2);
  lcd.setCursor(0,1);           // set cursor to column 0, row 1 (the first row)
  lcd.print("F: ");              // change this text to whatever you like. keep it clean.
  lcd.setCursor(3,1);
  lcd.print(distance2);
  delay(10);
 
  distance3 = getDistance(initPin3, echoPin3);
  lcd.setCursor(12,1);           // set cursor to column 12, row 0 (the first row)
  lcd.print("R: ");              // change this text to whatever you like. keep it clean.
  lcd.setCursor(14,1);
  lcd.print(distance3);
  delay(10);

 send(); // to get things started
 getData();
}

int getDistance (int initPin, int echoPin){

 digitalWrite(initPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(initPin, LOW);
 unsigned long pulseTime = pulseIn(echoPin, HIGH);
 int distance = pulseTime/58;
 return distance;

}

 //====================

void send() {

        radio.stopListening();
        //Front Sensor for Safe distance and collision alert 
       if(distance1 < 50) 
       { radio.write( &dataToSendforcollision1, sizeof(dataToSendforcollision1) );
           
       } else
       if(  (distance1 >= 50) && (distance1 <= 100)  ) 
       { radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2 ) );
           
       }


        
       //Left Sensor detect the overtaking situation by car
       if(distance2 < 30) 
      {  radio.write( &dataToSendforovertake1, sizeof(dataToSendforovertake1) );
      } else
      if(  (distance2 >=30) && (distance2 <= 60)  ) 
      {radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2) );      
      }     

          

      //Right Sensor detect the overtaking situation by car
      if(distance3 < 30) 
      { radio.write( &dataToSendforovertake1, sizeof(dataToSendforovertake1) );
      } else
      if(  (distance3>=50) && (distance3<= 80)  ) 
      { radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2) );     
      } 
        
}

//get data ==============

void getData() {
    if (radio.available()) {
    radio.read(&dataReceived, sizeof(dataReceived));
    lcd.clear();
    lcd.setCursor(0,0);           
    lcd.print(dataReceived);              // change this text to whatever you like. keep it clean.
    delay(500);
  }
}

Receiver

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

#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.
#define CE_PIN   22
#define CSN_PIN 24

LiquidCrystal lcd(8,9,4,5,6,7); 
RF24 radio(22, 24); // CE, CSN

char dataReceived[16]; // this must match dataToSend in the TX

const byte addresses[][6] = {"00001", "00002"};
char dataToSendforcollision1[] = "Collision  Alert";
char dataToSendforcollision2[] = "Lower the speed!";
char dataToSendforovertake1[]  =  " Car Overtaking ";
char dataToSendforovertake2[]  =  "Plan to overtake";

//ultrasonic 1
int echoPin1 =47;
int initPin1 =46;
int distance1 =0;

//ultrasonic 2
int echoPin2 =49;
int initPin2 =48;
int distance2 =0;

//ultrasonic 3
int echoPin3 =44;
int initPin3 =45;
int distance3 =0;


void setup(){
  lcd.begin(16,2);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  lcd.clear();
  lcd.setCursor(0,0);           // set cursor to column 0, row 1
  lcd.print("Dist(cm): "); // Writing on LCD
  pinMode(initPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(initPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(initPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[0]); // 00001
  radio.openReadingPipe(1, addresses[1]); // 00002
  radio.setPALevel(RF24_PA_MIN);
  }
  
  void loop(){
  distance1 = getDistance(initPin1, echoPin1);
  lcd.setCursor(12,0);           // set cursor to column 12, row 0 (the first row)
  lcd.print("L: ");              // change this text to whatever you like. keep it clean.
  lcd.setCursor(14,0);
  lcd.print(distance1);
  delay(10);
 
  distance2 = getDistance(initPin2, echoPin2);
  lcd.setCursor(0,1);           // set cursor to column 0, row 1 (the first row)
  lcd.print("F: ");              // change this text to whatever you like. keep it clean.
  lcd.setCursor(3,1);
  lcd.print(distance2);
  delay(10);
 
  distance3 = getDistance(initPin3, echoPin3);
  lcd.setCursor(12,1);           // set cursor to column 12, row 0 (the first row)
  lcd.print("R: ");              // change this text to whatever you like. keep it clean.
  lcd.setCursor(14,1);
  lcd.print(distance3);
  delay(10);
  getData();
  send();
  }

// get distance ===========
int getDistance (int initPin, int echoPin){

 digitalWrite(initPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(initPin, LOW);
 unsigned long pulseTime = pulseIn(echoPin, HIGH);
 int distance = pulseTime/58;
 return distance;

}

//get data ==============

void getData() {
    if (radio.available()) {
    radio.read(&dataReceived, sizeof(dataReceived));
    lcd.clear();
    lcd.setCursor(0,0);           
    lcd.print(dataReceived);              // change this text to whatever you like. keep it clean.
    delay(500);
  }
}

//send data =============

 //====================

void send() {

        radio.stopListening();
        //Front Sensor for Safe distance and collision alert 
       if(distance1 < 50) 
       { radio.write( &dataToSendforcollision1, sizeof(dataToSendforcollision1) );
           
       } else
       if(  (distance1 >= 50) && (distance1 <= 100)  ) 
       { radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2 ) );
           
       }


        
       //Left Sensor detect the overtaking situation by car
       if(distance2 < 30) 
      {  radio.write( &dataToSendforovertake1, sizeof(dataToSendforovertake1) );
      } else
      if(  (distance2 >=30) && (distance2 <= 60)  ) 
      {radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2) );      
      }     

          

      //Right Sensor detect the overtaking situation by car
      if(distance3 < 30) 
      { radio.write( &dataToSendforovertake1, sizeof(dataToSendforovertake1) );
      } else
      if(  (distance3>=50) && (distance3<= 80)  ) 
      { radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2) );     
      } 
        
}

Have you tried the simple Tx & Rx examples to check your radios work?

markd833:
Have you tried the simple Tx & Rx examples to check your radios work?

I didn't get any example for nrf, who is wokring in both transmitter and receiver mode.. Can you guide?

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

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. Start by getting the first example to work

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. The high-power nRF24s (with the external antenna) will definitely need an external power supply. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

Also note that the SPI pins on a Mega are different from those on an Uno or nano.

...R

Robin2:
Have a look at this Simple nRF24L01+ Tutorial.

Hello,

Thanks for the text. I have used 2nd method of your example. Please look to my code. I am unable to make it work. I am not getting, whats wrong..

radhika_das:
Thanks for the text. I have used 2nd method of your example. Please look to my code. I am unable to make it work. I am not getting, whats wrong..

Please start with the example from my Tutorial without any changes. If that does not work them please post the two programs that YOU have uploaded to your Arduinos and tell us in detail what happens when you run the programs.

Have you tried the connection test program in the Tutorial?

How are you powering your nRF24s?

Are you using two Megas?

...R

Robin2:
Please start with the example from my Tutorial without any changes. If that does not work them please post the two programs that YOU have uploaded to your Arduinos and tell us in detail what happens when you run the programs.

yes, I have tried it with this program. It works. It just send data from one arduino Mega to another Arduino Mega. I am connecting NRF with 3.3V of arduino Mega.

My aim is: I am working on college based project, which is vehicle to vehicle communication. In this, we are sending information about collision alert and overtaking from one car to another. As I posted in above program.

TRANSMITTER:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.
#define CE_PIN 22
#define CSN_PIN 24

LiquidCrystal lcd(8,9,4,5,6,7);
RF24 radio(22, 24); // CE, CSN

const byte address[6] = “00001”;
char dataToSendforcollision1 = “Collision Alert”;

void setup() {
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear();
lcd.setCursor(0,0); // set cursor to column 0, row 1

Serial.begin(9600);

radio.begin();
radio.openWritingPipe(address); // 00002
radio.setPALevel(RF24_PA_MAX);
radio.stopListening();

}
void loop() {
radio.write(&dataToSendforcollision1, sizeof(dataToSendforcollision1));
delay(1000);
}

RECEIVER:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.
#define CE_PIN 22
#define CSN_PIN 24

LiquidCrystal lcd(8,9,4,5,6,7);
RF24 radio(22, 24); // CE, CSN

char dataReceived[16]=" "; // this must match dataToSend in the TX

const byte address[6] = “00001”;

void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
}

void loop() {
if (radio.available()) {

radio.read(&dataReceived, sizeof(dataReceived));
lcd.clear();
lcd.setCursor(0,0);
lcd.print(dataReceived);
delay(500);
}
}

This is working. But, when I do both way, like I want arduino mega work as both transmitter and receiver. It wont work, Any hint or modification you can make?

Thanks

radhika_das:
yes, I have tried it with this program. It works. It just send data from one arduino Mega to another Arduino Mega. I am connecting NRF with 3.3V of arduino Mega.

The second example in my Tutorial uses the ackPayload concept to provide two-way communication. From your description I don't think you have tried it.

...R

PS ... When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the forum