How to use nrf24L01 with HC-SR04

We are trying to implement an Arduino with the HC-SR04 sensors that will transmit the values of the sensor to another Arduino using an NRF24L01 transceiver. We believe that there is no problem with the setup of our sensors and the transmitter. In order to receive the values from the sonar we are using the NewPing library. The problem that we run into is that, that the moment we turn on the transmitter using radio.begin() in the code, the sonar values are 0 but it can transmit a set data value. When we don't turn on the transmitter we get the proper sonar values. Here is our code for transmitting the data. Please reply as soon as possible we are on a time crunch.

#include <SPI.h>   // Comes with Arduino IDE
#include "RF24.h"
#include <NewPing.h>
#include <RF24_config.h>

#define tRight 11
#define eRight 10
#define tLeft 5
#define eLeft 6
#define tBack 13
#define eBack 12
#define max_dist 150

RF24 myRadio (7, 8);
NewPing sonarR(tRight, eRight, max_dist);
NewPing sonarL(tLeft, eLeft, max_dist);
NewPing sonarB(tBack, eBack, max_dist);


byte addresses[][6] = {"1Node"};
int sensorValue = 100;

void setup(){
  pinMode(tRight, OUTPUT);
  pinMode(eRight, INPUT);
  myRadio.begin();
  myRadio.setChannel(110);
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.openWritingPipe( addresses[0]);
  Serial.begin(9600);
//  delay(1000);

}
void loop(){
  myRadio.write( &sensorValue, sizeof(sensorValue) );
  Serial.println("SENT");
  int R = sonarR.ping_cm();
  int L = sonarL.ping_cm();
  int B = sonarB.ping_cm();
  Serial.print("Right: ");
  Serial.println(R);
  Serial.print("Left: ");
  Serial.println(L);
  Serial.print("Back: ");
  Serial.println(B);
  if(R<= 15 && R != 0.00){
    Serial.println("Object on RIGHT is too close!");
  }
  if(L<= 15 && L != 0.00){
    Serial.println("Object on LEFT is too close!");
  }
  if(B<= 15 && B != 0.00){
    Serial.println("Object in BACK is too close!");
  }
  Serial.println("____________________________");

  delay(1000);
  
}

Manual_Ping_transmit.ino (1.28 KB)

Hi,
First thing I see (maybe OK??) is that you do not need to set pins used by NewPing in Setup.. The library takes care of that..

nRF24L01 is very sensitive to power problems. See info on ArduinoInfo.Info HERE:

Do you have a nRF24L01 base module with power regulator?

Also try short distance with low power. See example settings:

radio.setDataRate(RF24_250KBPS);
speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps. 250K Bits per second gives longest range.

radio.setPALevel(RF24_PA_MIN);
Set Power Amplifier (PA) level to one of four levels: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX

The power levels correspond to the following output levels respectively: NRF24L01: -18dBm, -12dBm,-6dBM, and 0dBm

If that works and high power does not you have 3.3v power supply problems...

Let us know...

terryking228:
If that works and high power does not you have 3.3v power supply problems...

Let us know...

So I tried it with the setting that you said but what happens is that the code runs once and then immediately stops, as in it does not go through more iterations of void loop(). Could we possibly supply a separate source of power or will it only work when connected to the Arduino's GND and VCC?

Looking at the program in your Original Post I wonder what RF24 library you are using. The ManiacBug version probably does not work with the delay(1000). The newer TMRh20 version of the RF24 library will work fine in the same situation. Unfortunately TMRh20 also called his library "RF24" so if you want to change make sure you completely delete the ManiacBug version first. Or, if you know how, give the TMRh20 library a different name.

I got my nRF24s working with the tutorial that @terryking228 has linked to.

The pair of programs in this link may be useful.

...R

I have the same problem.
even though the NRF24L module is not connected to Arduino, if I have readio.begin() instruction then the sensor shows 0 values. but when i remove (or comment) the radio.begin(), the sensor shows proper distance values in the serial!

Which pins are used for the HCSR-04s? What pins are used for the NRF24?

Hewnikel:
I have the same problem.

It was three years ago when @QuadBros had that problem - I have completely forgotten about it.

Just describe your problem from the beginning and post your program.

...R

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(4, 5); // CE, CSN
const byte address[6] = "00001"; //Byte of array representing the address. This is the address where we will receive the data. This should be same on the transmitting side.

//buttons read from Remote Control
bool forwardButton = 0;
bool rightButton = 0;
bool leftButton = 0;
bool backwardButton = 0;

int In1 = 9; // to control motor A - forward
int In2 = 8; // to control motor A - backward
int In3 = 7; // to control motor B - forward
int In4 = 6; // to control motor B - backward

//connection of the ultrasonic sensor
int trigPin = 11; // Trigger
int echoPin = 12; // Echo
int highPin = 2;
long duration, cm;

void setup()
{
Serial.begin(9600);
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(); //This sets the module as receiver

//Define inputs and outputs of L298N motor driver
pinMode(In1, OUTPUT);
pinMode(In2, OUTPUT);
pinMode(In3, OUTPUT);
pinMode(In4, OUTPUT);

//Define inputs and outputs of ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(highPin, OUTPUT);
}

void loop(){
digitalWrite(highPin, HIGH);
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// Convert the time into a distance
cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343

Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(250);

groundFungus:
Which pins are used for the HCSR-04s? What pins are used for the NRF24?

You can check my program I posted. in the setup() when I remove radio.begin(); the sensor works. when I put it back it shows 0cm only.

Thanks guys.

Hewnikel:
You can check my program I posted.

That reads a bit like "I want you to help me but I am too lazy to answer your question".

Especially as the problem seems to be identified by @groundFungus's question. You are using pins 11 and 12 for your sensor. On an Uno the SPI pins needed for communication with the nRF24 are pins 11, 12 and 13.

...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

Robin2:
That reads a bit like "I want you to help me but I am too lazy to answer your question".

Especially as the problem seems to be identified by @groundFungus's question. You are using pins 11 and 12 for your sensor. On an Uno the SPI pins needed for communication with the nRF24 are pins 11, 12 and 13.

If you think so, then please don't help :slight_smile:

I shared my program and asked the guy to refer to it and look in the setup() part!

@groundFungus I am thankfully awaiting your reply.

See reply #9.