The 3-pin mode on your Arduino is useful if you only have a few digital I/O pins available. Two I/O pins are often required to connect an HC-SR04 sensor to an Arduino. However, in 3-wire mode, just one I/O pin is required as opposed to two.
A single I/O pin is utilised for input and output in this manner. Trig and Echo are not utilised simultaneously, therefore this is feasible.
Here's how to use the 3-wire mode to connect the HC-SR04 sensor to Arduino.
Wiring HC-SR04 Ultrasonic Sensor to Arduino UNO – 3 Wire Mode |
Simply attach the TRIG and ECHO pins to digital pin #9 and provide pin 9 for both pin values in the code. The remaining code is the same.
#define TRIGGER_PIN 9 // Trigger and Echo both on pin 9
#define ECHO_PIN 9
What are the limitations?
Particularly when compared to other inexpensive ultrasonic sensors, the HC-SR04 ultrasonic sensor performs exceptionally well in terms of accuracy and general usage. The HC-SR04 sensor may not always function as a result. Some of the HC-limitations SR04's are depicted in the images below:
- The sensor is farther away from the item or obstruction than 13 feet.
- To prevent sound from being reflected back to the sensor, the object's reflecting surface is at a shallow angle.
- Too little sound might be reflected by the item to reach the sensor. Additionally, sound may bounce off the floor if your device's HC-SR04 sensor is set low.
- The HC-SR04 sensor may have trouble detecting some things because their soft, uneven surfaces absorb sound rather than reflect it (such as plush animals).