Short range motion or presence e detection: which sensor?

My project
In my project I use an Arduino UNO R3 and a DFPlayer Mini. My purpose of this project is to have a sensor to detect a person when walking into the toilet (1 by 1.5 m), triggering the DFPlayer to play a MP3 file at detection.

Issue
First I used a HC-SR505 PIR motion sensor. But I quickly found out that this needs a range of 3 meters to be able to detect. My toilet is much smaller, so it doesn't work. I tried to adapt the coding on Arduino to get this sensor more sensitive in this short range, but it didn't work out.

Question
Which sensor would suit for my purpose, and is able to detect presence of a person within 25 - 60 centimeter? (And is still affordable)

I did not find a PIR-sensor that works within this short range.
I have seen the HLK-LD2410C mmWave sensor. It seems it might work. But I am not sure if it works in short distances like between 25 - 60 cm.

I used a mini PIR (AM312) as the trigger for an ESP32 CAM based camera. It works fine at short range.

Another option is an ultrassonic HCSR04.

Thnx @Brazilino !

I came across the AM312 Mini. But I read a datasheet that mentioned a working range around 3 meters. So, I did not pay attention to it anymore. Maybe I should just try it. I think implementing this one will have no effect on the Arduino code I currently have.

The HCSR04 is also interesting, I think this one works between 2 and 450 cm's. so that's pretty good. With this one I do have to find out how to drive it by code.

Do you have any experience with mmWave sensors?

Nope. I think they're interesting sensors, but I haven't tried them yet.

They are easy to use and have unlimited documentation, libraries and examples on the net. Definitely no rocket science. Since they're distance sensors, you'll need to define a range where it will consider that someone is inside the toilet.

I find it interesting the PIR (Passive InfraRed) does not work. It sounds like you have it connected wrong. Check this link: https://www.instructables.com/PIR-Motion-Sensor-Tutorial/ I have a lot of them in my home in the alarm system etc. Some have been working for over 40 years. The only possible maintenance is a dusting.

Thnx @gilshultz !

That’s interesting that you mention about wrong connection. The HC-SR505 has 3 legs, one that goes to +, one to GND and the middle one is connected to pin 2 of the Arduino UNO R3.

What are your thoughts about this?
(Let me know if you want to see the Arduino code)

That is correct but there was no indication as to how you had connected it in the question. Many times people connect to an analog input and wonder why they cannot adjust the range. It should be working for you but be sure it is pointed where people will be in front of it to trip it.

Not true. If pir doesn't detect person entering in 1.5mq room it's broken or adjusted wrong or wired wrong.

Thanx @kmin & @gilshultz for your comments.

I hereby will share some more information for clarification.
My connection between components looks like this:

So the HC-SR505 PIR-sensor has a plus & GND connection (on the sensor PCB the markings for are visible: left = GND, middle = signal, right is Plus).
The sensor's Signal leg is connected to pin 12 of the Arduino UNO R3 (so not pin 2 like in the image above).

Then the Arduino code looks like this:

===

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

#define PIN_BUSY 3
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int PIRmotion = 12; // choose the input pin (for PIR sensor)
int PIRstate = LOW; // we start, assuming no motion detected
int PIRval = 0; // variable for reading the pin status

void setup() {
  Serial.begin(115200);
  Serial.println("Setup");

  pinMode(PIN_BUSY, INPUT);
  pinMode(PIRmotion, INPUT); // declare sensor as input

  Serial.println("Setting up software serial");
  mySoftwareSerial.begin(9600);

  Serial.println("check dfplayer");
  if (!myDFPlayer.begin(mySoftwareSerial)) { // Use softwareSerial to communicate with mp3.
    Serial.println(F("DFPlayer error."));
    while (true); // Stop de code als de DFPlayer niet goed initialiseert
  }
  Serial.println("Setting up software serial —- done");

  //—-Mp3 play—-
  Serial.println("Setup mp3 player settings");
  myDFPlayer.volume(20);
  myDFPlayer.setTimeOut(500); // Set serial communication time out 500ms
}

void loop() {
  checkMotion();
}

void playMp3() {
  Serial.println("—– playMp3 —–");
  // play
  myDFPlayer.play(1); // Dit speelt het eerste MP3-bestand één keer af
  delay(500); 
  Serial.println("play done");
}

void checkMotion() {
  PIRval = digitalRead(PIRmotion); // read input value

  if (PIRval == HIGH) { // check if the input is HIGH
    if (PIRstate == LOW) { // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      PIRstate = HIGH;

      Serial.println("—– playMp3 —–");
      playMp3(); // Speel het MP3-bestand één keer af bij detectie van beweging
    }
  } else {
    if (PIRstate == HIGH) {
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      PIRstate = LOW;
    }
  }
}

===

Please let me know what you suggest to change.
Thanks in advance!

Firs try that your sensor is working and adjust sensitivity, time-delay and single/multiple trigger mode (sensor potentiometers/jumper).

void loop() {
PIRval = digitalRead(PIRmotion); // read input value
Serial.println(PIRval);
delay(500);
}

When it works like you want, experiment with your actual code.

Thnx @kmin

Test 1
I followed your suggestion to first test the sensor. Since I have two of the HC-SR505 PIR sensors, I tested them both. Using this connection setup:

Results 1 (with both):

  • The LED turns ON when I turn on power, and then it turns OFF after 6-7 seconds.
  • Movement in front of the HC-SR505 PIR does not make the LED go ON. I tried different distances, no response.
  • I noticed when I physically tap/touch the dome on the PIR, the LED goes on.

Test 2
By coincidence I found another PIR sensor lying around. I simply connected it to my Arduino and DFPlayer setup.

Results 2
It worked immediately! After the MP3 finished, I waited 10 sec's to see what happened (nothing so good). Then I moved my hand in front of the SC-HR501, at a distance of 30cm, and the Arduino + DFPlayer immediately picked up and started playing the MP3 again.

Outcome

  • I think we can conclude that the HC-SR505 (both of them) are broken.
  • By testing with the HC-SR501, you have convinced me that this type of PIR sensor does work in short ranges.
  • I have ordered new SC-HR505 PIR sensors to finalize this project.

HC-SR505 PIR sensor:

HC-SR505 PIR sensor:

I recommended to do adjustments with jumper and delay time.
Can easily be, that you have set the sensor to not trigger for five minutes after the first power on...

Thnx again @kmin

Concerning your recommendation; The HC-SR505 does not have jumpers, and as far as I know the delay time cannot be set. Therefore I haven't been able to set the sensor for any other setting than its normal operation.

Or did I misunderstand your point?

For this project I would very much like to keep using the HC-SR505 because of its compact size.

Sorry for that.
I got misleading information :wink:

Well, I'm glad things are progressing, but I'm puzzled that you have two units of the 505 sensor with the same problem. The new ones may/may not confirm that hypothesis.

The 505 datasheet says it outputs a 3.3V signal and a 5V Arduino like Uno would digitalRead() anything above 3V as a HIGH. So, everything seems to be ok. But after reading this:

I started to suspect that the sensor has a bad contact or might be sending a signal less than 3V. Could it be a possibility? (at this point the test with the LED of post #11doesn't help. It would be better to check with a DMM or apply the code kmin suggested in post #10 and check with the Arduino itself).

...or even bad jumper wire...

Very interesting @Brazilino & @kmin !

The remark 'bad contact' and ''bad jumper wire' made me think.

The jumper wires are already some years old and a bit wrinkled. Though I did test them with the other PIR sensor (HC-SR501) and with that sensor everything worked fine.

For possible bad contacts on the HC-SR505, I checked the contacts and PCB with a magnifying glass. Couldn't directly see bad contacts, though it didn't have quality soldering on the contacts. So I resoldered the contacts of the 3 pins and of the leads coming from the sensor itself to the board. And I did that with both sensors.

Re-test
Then I retested with both HC-SR505 PIR sensors. To be clear, everything is now directly connected with Arduino UNO R3 and DFPlayer (so no breadboard).
The first sensor was completely dead; no response to nothing. Not to movement and not to physically tapping the dome of the sensor.
With the 2nd HC-SR505 I have success. This one does respond to movement!

Conclusion
So I guess I was lucky that I have been able to save one of the 2 HC-SR505 sensors. Thanks for suggesting to check the contacts!
The other one could not be saved, but that's fine.

Next
I did notice that, when the PIR picks up the first movement and starts the MP3 file, and then detects a 2nd movement before the MP3 has ended, the Arduino restarts the MP3 from the beginning.
This is something I don't want to happen. The MP3 file should just play until the end, and only restart at a new movement, after the MP3 file has ended.

Question
What code do I need to correct that, and where should I place that?

You can ignore it in your code either with millis() counter or maybe with some DFRobotDFPlayerMini library command to verify if mp3 is finished.

I don't have that sensor so I cannot physically test your sketch. But I may be able to help if you can clarify what is happening when you trigger your sketch?

Meanwhile, your IFs don't look right to me; I think you should be comparing the PIRstate with the previous PIRstate?

BTW, I do have an-HW-490 IR sensor. Obviously it's intended to work with an IR transmitter, but I found that on its own it delivers a low going signal when I wave my arm at about 30-40 cm distance. So that could in turn trigger your DFR Player. If you simply want to play a track (or several in succession) you don't need an Arduino.

If your player board have pin "BUSY", you can use that to tell arduino if to start new mp3 on PIR trigger or not.