How to connect two ultrasonic sensors to Arduino uno | FREE 2024

Share your love
How to connect two ultrasonic sensors to arduino uno

How to connect two ultrasonic sensors to arduino uno

How to connect two ultrasonic sensors to Arduino uno, connecting two ultrasonic sensors to an Arduino Uno is a common task and can be exact connections may vary depending on the specific ultrasonic sensors you have, so refer to the datasheets of your sensors for accurate pinouts and specifications.

Assuming you are using the popular HC-SR04 ultrasonic sensors, here’s a general guide:

Materials You Need:

  • Two HC-SR04 Ultrasonic Sensors
  • Breadboard
  • Jumper wires
  • Arduino Uno

Connection:

1.Power Connections:

  • Connect the VCC (5V) pin of each sensor to the 5V output on the Arduino.
  • Connect the GND pin of each sensor to the GND on the Arduino.

2. Trigger and Echo connections:

  • Connect the Trigger (Trig) pin of the first sensor to a digital pin on the Arduino (e.g., D3).
  • Connect the Echo (Echo) pin of the first sensor to another digital pin on the Arduino (e.g., D3).
  • Connect the Trigger (Trig)pin of the second sensor to a different digital pin on the Arduino (eg., D5)
  • Connect the Echo (Echo) pin of the second sensor to another digital pin on the Arduino (e.g., D4).

3. Note:

  • Ensure that you don’t connect the Trig and Echo pins of the two sensors to the same digital pin on the Arduino board. Each sensor should have its unique set of Trig and Echo pins.

Arduino Code

// Define the pins for the first sensor
int trigPin1 = 2;
int echoPin1 = 3;

// Define the pins for the second sensor
int trigPin = 4;
int echoPin = 5;
void setup()  {
  Serial.begin(9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
// Code to use the first sensor
long duration1, distance1;
digitalWrite(trigPin1, LOW);
delayMicrosecond(2);
digitalWrite(trigPin1, HIGH);
delayMicrosecond(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseln(echoPin, HIGH);
distance1 = (duration1 / 2) * 0.0343;

// Code to use the second sensor 
long duration2, distanc2;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicrosecond(10);
digitalWrite(trigPin, LOW);
duratio2 pulseln(echoPin, HIGH);
distance2 = (duration2 / 2) * 0.0343;

//Print the distances
Serial.print("Distance from Sensor 1: ");
Serial.print(distance1);
Serial.prinln(" cm");

Serial.print("Distance from Sensor 2: ");
Serial.print(distance2);
Serial.println(" cm);

delay(1000); // Adjast the delay as needed

}

Upload this code to your Arduino Uno and open the Serial Monitor (Tools-> Serial Monitor) to see the distance readings from both sensors.

This example that the speed of sound is approximately 343 meters per second (0,0343 cm/microsecond). Adjustments and environmental conditions. And you will be able to learn How to connect two ultrasonic sensors to arduino uno.

Simple project with ultrasonic sensor

How to connect two ultrasonic sensors to arduino uno

A simple project with an ultrasonic sensor is creating a distance measurements system. In this example, I’ll show you how to build a distance meter using an ultrasonic sensor and an arduino Uno.

How to connect two ultrasonic sensors to Arduino uno.

Materials:

  • Arduino Uno
  • HC-SR04 Ultrasonic Sensor
  • Jumper wires
  • Breadboard

Connections:

  1. Connect the VCC pin of the HC-SR04 to 5V on the Arduino.
  2. Connect the GND pin of the Ultrasonic sensor to GND on the Arduino.
  3. Connect the Trig pin of the Ultrasonic sensor to a digital pin on the Arduino (e.g. D4)

Arduino Code:

// Define the pins for the ultrasonic sensor
int trigPin = 2;
int echoPin = 3;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Send a palse to the sensor
 digitalWrite(trigPin, LOW);
 delayMIcrosecond(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

// Measure the duration of the pulse from the echo pin
 long duration = pulseln(echoPin, HIGH);

// Culculate the distance in centimeters
 int distance = duration * 0.034 / 2;

// Print the distance to the Serial Monitor
 Serial.print("Distance: ");
 Serial.print(distance);
 Serial.println(" cm");

 delay(1000); // Adjust the delay as needed

}

Instructions:

  1. Connect the hardware as described above.
  2. Upload the provided code to your Arduino uno.
  3. Open the Serial Monitor (Tool-> Serial Monitor) to view the distance measurements.

The ultrasonic sensor will sound wave and measure the time it takes for the wave to bounce back after hitting an object. The Arduino then calculates the distance based on the time it took for the wave to bounce back. How to connect two ultrasonic sensors to Arduino uno.

This is a basic example, and you can expand on it by adding features like displaying the distance on an LCD screen, trigging an action when an object is too close, or even integrating it into a larger project.

If you want other examples about an Arduino and sensors, just leave a comment of it and will process it for you.

And if you have any questions about coding and Arduino, leave your question in the comment section and will assist you as soon as possible.

Share your love
Derrick Murehwa
Derrick Murehwa
Articles: 31

Leave a Reply

Your email address will not be published. Required fields are marked *