I've looked over both my wiring and code quite a bit, double-checking previous forum posts for answers, etc. but can't quite get these modules to communicate. I'm attempting to take the output from a UT sensor and display it on an LCD screen connected to a second UNO. Right now the LCD screen prints "No Communication", which I programmed it to do if the radio.available()
function returns false.
I have the UT sensor and transmitter code labeled "Transmitter" and the LCD and receiver code labeled "Reciever".
Transmitter:
#define echoPin 6 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 7 //attach pin D3 Arduino to pin Trig of HC-SR04
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
int returnDis = 7;
int disOut = 0;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
radio.begin(); //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening(); //This sets the module as transmitter
Serial.begin(9600);
}
void loop() {
trigClear();
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
radio.write(&distance, sizeof(distance));
delay(1000);
}
void trigClear() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
}
Reciever:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <LiquidCrystal.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int distance0 = 0;
int distance = 0;
int velocity;
void setup() {
radio.begin();
radio.openReadingPipe(0, address); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening();
lcd.begin(16, 2);
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
}
void loop() {
lcd.clear();
if (radio.available()) {
radio.read(&distance, sizeof(distance));
//if object found, print out new distance and find the velocity
if (distance <= 400) {
lcd.print("Distance: ");
lcd.print(distance);
lcd.print("cm");
//print out velocity
velocity = (abs(distance - distance0));
lcd.setCursor(0, 1);
lcd.print("Velocity: ");
lcd.print(velocity);
lcd.print("Cm/s");
Serial.println("Distance ");
Serial.println(distance);
//set initial distance to new distance and delay by one second
distance0 = distance;
delay(1000);
}
else {
lcd.print("No Object Found");
}
}
else {
lcd.print("No Communication");
delay(1000);
}
}
Below are my wiring schematics(please let me know if there's anything I can do to make them more clear)
Transmitter:
Reciever:
I have also used a test program to find that both nRF24L01 modules are not functioning as intended at all. Here's the test programs serial output:
If someone could explain this portion to me in better detail I would appreciate it greatly. I'm new to wireless communication and it's confusing at best.