Hi, I'm creating a project where I'm using two HC-SR04 sensors to measure the distance between them. I'm using the receiver from one HC-SR04 and the transmitter from another to communicate. Ideally, the HC-SR04 transmitter would send a signal and the receiver would detect it by triggering its own trigger pin to set the echo pin to high (I covered the trigger pin of the HC-SR04 receiver so its echo pin can't detect the signal it sent). However, the receiver is not detecting any signal (pulseIn(echo, HIGH) = 0) which is where the problem comes in. Initially, I used time delays for the sensors to sync with each other but it resulted in inaccurate and imprecise distances which is why I thought I'd use a radio module (NRF24L01) to sync the sensors. Unfortunately, this time, the receiver echo pin did not read anything and I'm basically getting 0 distances. Here's the code I made for reference:
Transmitter:
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio (7,8);
const byte address[6] = "00001";
const int trig = 9;
const int echo = 10;
const unsigned long event;
unsigned long prevTime;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_LOW);
radio.stopListening();
}
void loop() {
unsigned long now = millis();
int x = 1;
if (now - prevTime >= 3000){
radio.write(&x, sizeof(x));
trigpin();
prevTime = now;
}
}
void trigpin(){
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
}
Receiver:
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio (7,8);
const byte address[6] = "00001";
const int trig = 9;
const int echo = 10;
long duration;
float distance;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_LOW);
radio.startListening();
Serial.begin(9600);
}
void loop() {
if (radio.available()){
int x = 0;
radio.read(&x, sizeof(x));
if (x = 1){
duration = 0;
trigpin();
duration = pulseIn(echo, HIGH);
distance = duration * (0.034 / 2);
Serial.println(duration);
Serial.println(distance);
Serial.println("---");
}
}
}
void trigpin(){
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
}
How it works:
The transmitter is supposed to send an ultrasonic wave every 3 seconds to the receiver. But before doing so, it notifies the receiver to send a fake trigger to set the echo pin to high. The receiver then calculates the distance and prints it.
Result:
The printed values are "0" for both duration and distance.
Possible cause(s):
I was thinking that maybe I got zero values was because it took too long for the echo pin to be set to high-maybe it might have missed its window to receive the signal from the transmitter. I tried adding delays before triggering the transmitter trig pin and after sending the radio signal from transmitter to receiver but I got the same results.
Can someone point out what I did wrong and how I can fix it? I hope I didn't take too much of your time. Thanks very much for your help and happy new year! ![]()
P.S. I've only started studying Arduino mainly through YouTube tutorials so please go easy on me ![]()
