notes on using multiple HCSR04 ultrasonic sensors with Arduino Uno

Integrating multiple HC-SR04 ultrasonic sensors with an Arduino Uno can significantly enhance your project's spatial awareness capabilities. In this guide, we'll walk you through the process of connecting several HC-SR04 sensors to an Arduino Uno, providing detailed wiring instructions and a sample sketch to help you get started. By following these steps, you'll be able to accurately measure distances from multiple points, enabling more complex and responsive designs.

Example


Materials

- Arduino UNO
HCSR04's
- USB cable
- breadboard
- wires

Setup


Sketch
[using HCSR04 ultrasonic sensor library from [1]]

#include <HCSR04.h>

HCSR04 hc(7, new int[4]{8, 9, 10, 11}, 4);
//class HCSR04 (trig pin , echo pin, number of sensors)

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("-");
for (int i = 0; i < 4; i++) {
Serial.println(hc.dist(i)); //return curent distance in serial for sensor 1 to 4
}
delay(60);// in order to prevent trigger signal to the echo signal.
}

References

Comments