HC SR504 / JSN SR04 Ultrasonic sensor on ESP32

I am trying to set up a JSN SR04T Ultrasonic sensor to be triggered by a PIR sensor. The plan is to reduce power consumption and false positives in a driveway alarm

The code snippet below is the relevant segment of the program. It works to the extent that the ultrasonic sensor fires, I see a return pulse, and it performs actions unrelated to this code snippet.

Where it fails is: the sensor is pointed at the ceiling. The ceiling is ten feet high. The duration should be around 120,000 us. I am seeing 2 or 3 microseconds, and 0 cm distance

I have tried with both HC-SR04 and JSN-SR04 sensors and consistently get the same result, impossibly short distances.

const int motionSensor      = 4;    // PIR motion sensor pin
volatile byte alarmStatus   = 0;
#define TRIGGER     12              // ultrasonic sensor HC -SR504 or JSN-SR04T
#define SENSOR      34
#define BOARD_LED   25
unsigned long duration;             // Define ultrasonic sensor variables:
int distance;

void IRAM_ATTR detectMovement()     // stable: PIR motion detect and latch //
{
  alarmStatus = (1);
}

void alarmHandler()
{
  if (alarmStatus == 1)
  {
    digitalWrite(BOARD_LED, HIGH);  // Wolf Fence
    digitalWrite(TRIGGER, HIGH);    // TRIGGER the sender, 8 pings
    delayMicroseconds(10);
    digitalWrite(TRIGGER, LOW);
    // Read the SENSOR. pulseIn() returns the duration (length of the echo pulse) in microseconds:
    duration = pulseIn(SENSOR, HIGH); 
    distance = duration * 0.017;    // 58,823.5 us per meter / 17,929 us per foot
    Serial.print( "duration = " );
    Serial.print( duration );
    Serial.print(" us ## " );
    duration = 0;
    Serial.print( "Distance = " );
    Serial.print( distance );
    Serial.println( " cm" );
    delay(100);
    digitalWrite(BOARD_LED, LOW);
  }
}

void setup()
{
  Serial.begin(115200);
  // PIR Motion Setup                // stable
  pinMode(motionSensor, INPUT);
  attachInterrupt(digitalPinToInterrupt(motionSensor), detectMovement, RISING);
  pinMode(TRIGGER, OUTPUT);
  pinMode(SENSOR, INPUT);
  pinMode(BOARD_LED, OUTPUT);
  digitalWrite(BOARD_LED, LOW);
}

void loop()
{
  alarmHandler();
}

There are 4 pins in the HCSR04. VCC,GND,Trig and Echo. Is the Echo pin referred as sensor in your code?

yes, echo is SENSOR

I have always made the trigger pin Low for 2 microseconds. Then HiGH for 10 microseconds, and then LOW again. The code looks like the following:

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

You can follow the entire code and working principle here for cross-checking.

Are you using a SRO4 that is compatible with a 3V3 MCU?

GPI0_NUM_34 may not be the best choice for the trigger pin but if the other pins are taken up.

and you might consider

float distance = 0.0;


distance = (float)duration * 0.017;  

GPI0_NUM_34 may not be the best choice for the trigger pin but if the other pins are taken up.

I am triggering with 12 and sensing with 34. 34 is an input only pin.

When does alarmStaus = 0; happen?

When does alarmStaus = 0; happen?

this is a snippet of a much longer code:

void IRAM_ATTR detectMovement()     // stable: PIR motion detect and latch //
{
  alarmStatus = (FSBID);
}

void alarmHandler()
{
  if (alarmStatus == FSBID)
  {
    digitalWrite(BOARD_LED, HIGH);  // Wolf Fence
    digitalWrite(TRIGGER, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIGGER, HIGH);    // TRIGGER the sender, 8 pings
    delayMicroseconds(10);
    digitalWrite(TRIGGER, LOW);
    // Read the SENSOR. pulseIn() returns the duration (length of the echo pulse) in microseconds:
    duration = pulseIn(SENSOR, HIGH); // IF DURATION IS ZERO, RETURN;
    distance = duration * 0.017;    // 58,823.5 us per meter / 17,929 us per foot
    Serial.print( "duration = " );
    Serial.print( duration );
    Serial.print(" us ## " );
    duration = 0;
    Serial.print( "Distance = " );
    Serial.print( distance );
    Serial.println( " cm" );

    delay(100);

    if ( distance <= 90 )
    {
      Serial.print("!!!!!!!!!! alarmStatus == ");
      Serial.print(FSBID);
      Serial.println(" !!!!!!!!!!");
      sendLoRaPacket();
      logEvent();
      alarmStatus = 0;
      Serial.println("alarmStatus = 0");
      Serial.println( "iiiii Alarm-Triggered Packet Sent iiiii");
      Serial.println();
      digitalWrite(BOARD_LED, LOW);
    }
  }
}

hey, I'm old, I forget things. It belongs just before digitalWrite(BOARD_LED, LOW); in the snippet

explaining FSBID would just take things off at a tangent, which is why I posted a snippet

still getting 0's?

still getting 0's?

It is 2 PM. Barbie, Ashley, Deana, Mikala and Milena are on shift at the El Camino. I'm old, but I'm still kicking. It's a priorities thing. I will get back to this in a couple of hours

Post your updated code please. In a new message.

adding the 2 microsecond LOW before pinging the sonar had no effect

adding

float distance = 0.0;

distance = (float)duration * 0.017;  

gives me three 0s behind microseconds

duration = 2.000 microseconds Distance 0 .034 cm.

void alarmHandler()
{
  if (alarmStatus == FSBID)
  {
    digitalWrite(BOARD_LED, HIGH);  // Wolf Fence
    digitalWrite(TRIGGER, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIGGER, HIGH);    // TRIGGER the sender, 8 pings
    delayMicroseconds(10);
    digitalWrite(TRIGGER, LOW);
    // Read the SENSOR. pulseIn() returns the duration (length of the echo pulse) in microseconds:
    float duration = pulseIn(SENSOR, HIGH); // IF DURATION IS ZERO, RETURN;
    float   distance = duration * 0.017;    // 58,823.5 us per meter / 17,929 us per foot
    Serial.print( "duration = " );
    Serial.print( duration, 3 );
    Serial.print(" us ## " );
    duration = 0;
    Serial.print( "Distance = " );
    Serial.print( distance, 3 );
    Serial.println( " cm" );

    delay(100);

    if ( distance <= 90 )
    {
      Serial.print("!!!!!!!!!! alarmStatus == ");
      Serial.print(FSBID);
      Serial.println(" !!!!!!!!!!");
      sendLoRaPacket();
      logEvent();
      alarmStatus = 0;
      Serial.println("alarmStatus = 0");
      Serial.println( "iiiii Alarm-Triggered Packet Sent iiiii");
      Serial.println();
      digitalWrite(BOARD_LED, LOW);
    }
  }
}

Have you tried the New Ping library example to see if your ultrasonic is actually working?

Afterwards then you can do the pulse up delay pulse off measure time thing.

New ping will also tell if the issue is with the code, the MCU, or the SRO4 or the other thing.

Is your ultrasonic a 3V3? 2nd time asking this question.

The issue with code snippets is I cannot see the whole thing so I have to ask stupid poo! like this:

Have the global variables been commented out?

Does the New Ping example work?

Yes, I have tried the New Ping library. An HC-SR504 showed 0 CM to the ceiling. The JSN SR504 shows 19 - 20 cm to the ceiling, the floor, the the walls, and my thumb on top of the sensor

yes, I commented out the global variables

The module simply says VCC. Of course the seller says it works on both 3.3 and 5 volts, but truth is thin on the ground at eBay.

Has the OP considered that the Sr504 is not working? Maybe the MCU is not working?
Anyways, that's all I have for you. Good luck.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.