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();
}
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.
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
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.