I am currently using the Arduino Pro Micro board and I want to connect 2 ultrasonic sensors to 1 board, whilst knowing which sensor is being triggered. The sensor I am using is a HC-SR04 Ultrasonic sensor.

Please help me thank you :)


Yes, you can, as long as you serialize the send/receive pulse cycles, to avoid interferences. basically, you attach the Trig/Echo lines to a different pair of pins, then do the reading on one sensor, then do the reading on the other.

| improve this answer | |
  • Sorry i'm really really new and noob at this, so i really dont know what all these mean – Raynard Aug 28 '16 at 15:52

What you need to do, for each sensor, is the following:

  • choose a GPIO that will drive the trigger line (make sure it is not a pin you are already planning to use, ex: TX/RX )
  • choose a pin that will receive the information from the Echo line, ideally using an interrupt driven implementation, instead of polling. The 328P supports exactly 2 independent interrupt lines: INT0 and INT1, so you could use one of them for each sensor.
  • have a time base and use it to time-stamp the Trig and Echo lines

Example for one sensor (for 2 just do the same and keep in mind that there might be interferences, like the OP said):

  1. toggle the Trig line for the required duration (strobe)and save it
  2. read the time
  3. wait for the interrupt to occur (you can do other things in the meanwhile)
  4. when the interrupt occurs, read again the time
  5. subtract the timestamp taken after toggling Trig from the timestamp taken after the Echo interrupt
  6. convert the delta time from the previous point into a linear measure, using the constant provided by the datasheet of the sensor
| improve this answer | |
  • “The 328P supports exactly 2 independent interrupt lines: INT0 and INT1” is rather misleading. Pin change interrupts suitable for sensing Echo lines are supported on about two dozen pins (ie on all pins except for Vcc, Gnd, ARef, AVcc, ADC6, ADC7) – James Waldby - jwpat7 May 25 '16 at 15:01
  • @jwpat7 I think my comment is correct: it does indeed support 2 independent interrupt lines. What you mention is ORed within a port. Yes, one might choose only one pin per port and sort of simulate more independent interrupt sources, but it is bordeline a design trick and requires additional programming for the port masking. Given the level of knowledge implied by the question, it seemed more effective to give the answer that is simpler to implement and maps also to other architectures that do not have the concept of port. – Igor Stoppa May 25 '16 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.