How To Use Multiple Proximity Senors At The Same Time?

Hi there, so this is more of a double question. First of all some background info of what i'm trying to achieve. i want to be able to roll a ball past some sensors, each triggered a different sound from seperate speakers, with the last sensor triggering a new sound in all speakers. the set up consists of 5 proximity sensors (4 PNP, 1 NPN ), 4 DFPlayer Mini devices, and an arduino. This all works with a one senor setup, but im not sure how to glue it all together.

So my questions are:

  1. Can you even control these 5 sensors simultaneously?
  2. can you control 4 DFPlayer simultaneously via SoftwareSerial

Thank you for your time.

#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>

int PIR1 = 10;
int PIR2 = 11;
int PIR3 = 12;
int PIR4 = 13;
int PIR5 = 14;
int rxPin1 = 3;
int txPin1 = 2;
int rxPin2 = 5;
int txPin2 = 4;
int rxPin3 = 7;
int txPin3 = 6;
int rxPin4 = 9;
int txPin4 = 8;

int motionStatus1 = 1;
int pirState1 = 1;
int motionStatus2 = 1;
int pirState2 = 1;
int motionStatus3 = 1;
int pirState3 = 1;
int motionStatus4 = 1;
int pirState4 = 1;
int motionStatus5 = 0;
int pirState5 = 0;

SoftwareSerial fxSerial1 (rxPin1, txPin1);
SoftwareSerial fxSerial2 (rxPin2, txPin2);
SoftwareSerial fxSerial3 (rxPin3, txPin3);
SoftwareSerial fxSerial4 (rxPin4, txPin4);
DFRobotDFPlayerMini fxPlayer1;
DFRobotDFPlayerMini fxPlayer2;
DFRobotDFPlayerMini fxPlayer3;
DFRobotDFPlayerMini fxPlayer4;


void setup() {
  pinMode(PIR1, INPUT);
  pinMode(PIR2, INPUT);
  pinMode(PIR3, INPUT);
  pinMode(PIR4, INPUT);
  pinMode(PIR5, INPUT_PULLUP);
  pinMode(rxPin1, INPUT);
  pinMode(txPin1, OUTPUT);
  pinMode(rxPin2, INPUT);
  pinMode(txPin2, OUTPUT);
  pinMode(rxPin3, INPUT);
  pinMode(txPin3, OUTPUT);
  pinMode(rxPin4, INPUT);
  pinMode(txPin4, OUTPUT);
  Serial.begin(9600);
  fxSerial1.begin(9600);
  fxSerial2.begin(9600);
  fxSerial3.begin(9600);
  fxSerial4.begin(9600);
  fxPlayer1.begin(fxSerial1);
  fxPlayer2.begin(fxSerial2);
  fxPlayer3.begin(fxSerial3);
  fxPlayer4.begin(fxSerial4);
  fxPlayer1.volume(30);
  fxPlayer2.volume(30);
  fxPlayer3.volume(30);
  fxPlayer4.volume(30);
  delay(3000);
}

void loop() {

  motionStatus1 = digitalRead(PIR1);
  motionStatus2 = digitalRead(PIR2);
  motionStatus3 = digitalRead(PIR3);
  motionStatus4 = digitalRead(PIR4);
  motionStatus5 = digitalRead(PIR5);

  // Sensor 1
  if (motionStatus1 == HIGH) {
    // Option 1
    if (pirState1 == LOW) {
      Serial.println("Sensor 1 Detected");
      pirState1 = HIGH;
      fxPlayer1.play(1);
      delay(200);
    }
  }
  else {
    // Option 1
    if (pirState1 == HIGH) {
      Serial.println("Sensor 1 Motion Ended");
      pirState1 = LOW;
    }
  }

  // Sensor 2
  if (motionStatus2 == HIGH) {
    // Option 1
    if (pirState2 == LOW) {
      Serial.println("Sensor 2 Detected");
      pirState2 = HIGH;
      fxPlayer2.play(2);
      delay(200);
    }
  }
  else {
    // Option 1
    if (pirState2 == HIGH) {
      Serial.println("Sensor 2 Motion Ended");
      pirState2 = LOW;
    }
  }

  // Sensor 3
  if (motionStatus3 == HIGH) {
    // Option 1
    if (pirState3 == LOW) {
      Serial.println("Sensor 3 Detected");
      pirState3 = HIGH;
      fxPlayer3.play(2);
      delay(200);      
    }
  }
  else {
    // Option 1
    if (pirState3 == HIGH) {
      Serial.println("Sensor 3 Motion Ended");
      pirState3 = LOW;
    }
  }
  
  // Sensor 4
  if (motionStatus4 == HIGH) {
    // Option 1
    if (pirState4 == LOW) {
      Serial.println("Sensor 4 Detected");
      pirState4 = HIGH;
      fxPlayer4.play(2);
      delay(200);      
    }
  }
  else {
    // Option 1
    if (pirState4 == HIGH) {
      Serial.println("Sensor 4 Motion Ended");
      pirState4 = LOW;
    }
  }

  // Sensor 5
  if (motionStatus5 == LOW) {
    // Option 1
    if (pirState5 == HIGH) {
      Serial.println("Hole Detected");
      pirState5 = LOW;
      fxPlayer1.play(2);
      fxPlayer2.play(2);
      fxPlayer3.play(2);
      fxPlayer4.play(2);
      delay(200);      
    }
  }
  else {
    // Option 1
    if (pirState5 == LOW) {
      Serial.println("Cup Motion Ended");
      pirState5 = HIGH;
    }
  }
}

You aren't new, but you seem to have forgotten how to use the forum. A picture of a hand drawn wiring diagram is usual.

How far apart are the PIR sensors? These are very gross measurement devices, you will need lots of seperation in order to 'hear' them individually.
Get rid of the delay and read about state machines and/or how to do multiple things at once. Those articles are in the Tutorials section.
Good luck.

In your time of learning to write programs, have you ever seen any commands to do any things simultaneously? Computers do things one at a time.

Microcontrollers like Arduino can read multiple digital inputs in a loop just like you’re already doing with the digitalRead() calls. The main limitation comes from timing, responsiveness, or polling frequency, but for proximity or motion sensors (like PIR or PNP/NPN sensors), polling in a fast loop is usually more than sufficient.

Not simultaneously, but sequentially.

I have a couple of ideas but I think the easiest if not the least expensive, would be to use one Arduino to read your sensors as a central controller to accessory Arduinos, one for each DFPlayer. Just have the accessory Arduinos poll an I/O for a digital signal from the central one and play the thing you want.

As for outputting each one or all together to the same amplifier, maybe some relays or even a custom audio cable, but I'd have to see all the audio gear to come up with something.

Impossible to say if we don't know what type of sensors they are. You say "proximity" but that could mean various things. @sonofcy thinks you mean PIR sensors. @xfpd thinks you mean ultrasonic sensors. Are either of them correct?

If PIR sensors are correct, the Arduino can't control them at all. There are often one or two small potentiometers on the PIR's PCB so that a human can control the sensitivity and on-period. But there's nothing the Arduino can control.

If ultrasonic sensors are correct, the Arduino can control when the sensors take a reading. It is possible for the Arduino to tell all sensors to take a reading simultaneously, but that would be pointless because the Arduino can't read the results from all 5 sensors simultaneously. Also it might cause incorrect readings because the sensors might detect each others audio signals.

No. The Arduino could control one after the other, in very quick succession, so it would seem to a human that all were controlled simultaneously.

I said PIR because he has five int variables called PIR1 to PIR5. Not a definitive truth, but very suggestive.

I used HC-SR04 because they are directional whereas PIR are omnidirectional (one will do).

Good point, I didn't read the code that was posted yet, it seemed premature.

But PIR sensors don't detect proximity. They detect warm objects moving in their field of view. They cannot tell you how far away the object is. A large warm object moving far away would be detected the same as a small warm object moving nearby. (I'm explaining this for @countessaflufforossa's benefit, of course.)

1 Like

Exactly!

Directional or not, at least HC-SR04 sensors can be accurately described as proximity sensors: they sense the distance between the sensor and the object.

(:robot: translated)
If by "simultaneously" we don't mean "at the exact same moment", but rather "together" on a single controller unit, then yes, it can be done. Of course, the program handles one thing at a time in (rapid) succession, so it becomes more of a software design issue than a hardware limitation.

If I'm not mistaken, a DFPlayer doesn't necessarily require bidirectional communication (MP3 player using Arduino and DFPlayer mini - Electronics-Lab.com), but you would need to adapt the 5V signals sent from the Arduino to the 3.3V accepted by the DFPlayer's RX pin (a simple voltage divider using 680Ω/1200Ω resistors will do).

If you're comfortable with basic electronic assembly, you could control multiple DFPlayers without using all those pin pairs by using just 4 pins to drive an external decoder:

Im aware of this, im using them as a trigger, im setting the distance via the sensor. all the sensor does is act like a switch.

Ok, thanks for the link. It would have to been good to have that in the original question.

"PIR" normally stands for Passive Infra-Red which is a sensor which does not emit any IR light itself (hence "Passive"), but detects IR light given off by warm objects.

Your sensors are IR but not passive, they emit a beam of IR light which reflects off objects and the sensor then detects that reflected light.

So to answer your original question: no, the Arduino cannot control these sensors, there is nothing to control.

EDIT: I suppose the Arduino could control them by switching their power on and off. Is that what you meant? If so, why is that needed?

(:robot: translated)
I’d say that by “control” he means “reading” or “managing the readings” of five sensors. The ones mentioned might also require a pull-down resistor on their output. However, we don’t have any information about the NPN sensor (which he mentioned at the beginning).

You say your object is a ball.

What sort of ball are we talking about, wooden, stainless steel ball bearing or what? Also how big is this ball?

This drip drip of information is not very helpful.

I once wrote a series of three articles about detecting balls and making sounds using the GraviTraks system for the MagPi magazine. This is the first one:-

More information from:-
Hack gravitrax with raspberry pi

You might see part 2 come up at the end of the video. That was all about using lighting effects triggered by the balls. Part 3 was about combining both lights and sound but I couldn't get a video good enough to post.

2 Likes

You aren't new, but you seem to have forgotten how to use the forum. A picture of a hand drawn wiring diagram is usual.

Thanks for this idea, this is somewhat what ive gone with in the end, seems to all be working as intended now.

1 Like