Controlling two Ultrasonic Sensor with Arduino Uno with code | HC-SR04 module control using Arduino with code

Controlling two Ultrasonic Sensor with Arduino Uno with code


Controlling two Ultrasonic Sensor with Arduino Uno with code
Two Ultrasonic Sensor with Arduino Uno Connection

About Sensor 


HC-SR04 Ultrasonic Sensor Module Distance Measuring Transducer


Description of ultrasonic sensor


The Sensor HC-SR04 Ultrasonic Sensor Module is a popular and affordable distance-measuring transducer that uses ultrasonic waves to detect the distance of an object in the sensor's range. This sensor is commonly used in robotics, automation, security systems, and other applications that require accurate and reliable distance measurements.

The HC-SR04 module consists of a transmitter and a receiver, which are mounted on the same PCB board. The transmitter emits a 40-kHz ultrasonic wave, and the receiver on the board detects the echo signal when the wave reflects off any object in the range. The time it takes for the sound wave to travel to the object and back is measured and converted into a distance using the speed of sound formula.

The HC-SR04 module has a total of four pins: VCC, GND, Trig, and Echo, as shown in the image. VCC and GND are connected to the power supply (5V) on the microcontroller board. Trig is the trigger pin, and Echo is the echo pin. When the Trig pin receives a high pulse, the module sends out an ultrasonic wave. The Echo pin then outputs a high pulse that is proportional to the time it takes for the wave to return. By measuring the duration of the high pulse, the distance to the object can be calculated.

The HC-SR04 ultrasonic sensor module has a range of up to 4 meters and a resolution of 0.3 cm. This sensor is a low-cost, easy-to-use device that can be interfaced with various microcontrollers, such as Arduino and the Raspberry Pi. However, it may not work well in noisy environments and may require some calibration for optimal performance.



Code Explanation 

In this code, we first define the Arduino pins for the two ultrasonic sensors. Then, in the setup function, we set the trigger pins as outputs and the Echo pins as inputs of an ultrasonic sensor. You can also start serial communication at a baud rate of 9600 set on the Arduino board.
In the Void Loop function, we can measure the distance for the first sensor and store it in the variable distance1. Then we do the same for the second sensor and store the distance value in variable distance2. After  sensor senses the object, print both distances to the serial monitor using the serial print function.

In this program, I have added a short delay before measuring again to prevent the sensors from interfering with each other.
Note that the pulseIn function returns the duration of the pulse in microseconds, so we need to divide by 58 to get the distance in centimeters. Also, make sure to connect the sensors to the correct pins on the Arduino Uno as per the circuit and program, and also to include the Ultrasonic.h library if necessary or install separately from the library on Arduino IDE.


Code

Controlling 2 Ultrasonic Sensor with Arduino Uno with code

// Define the pins for the first ultrasonic sensor
const int trigPin1 = 7;
const int echoPin1 = 6;

// Define the pins for the second ultrasonic sensor
const int trigPin2 = 5;
const int echoPin2 = 4;

void setup() {
  // Set the trigger pins as outputs
  pinMode(trigPin1, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  
  // Set the echo pins as inputs
  pinMode(echoPin1, INPUT);
  pinMode(echoPin2, INPUT);
  
  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  // Measure distance for the first sensor
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  long duration1 = pulseIn(echoPin1, HIGH);
  int distance1 = duration1 / 58;

  // Measure distance for the second sensor
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  long duration2 = pulseIn(echoPin2, HIGH);
  int distance2 = duration2 / 58;

  // Print the distances to the serial monitor
  Serial.print("Distance 1: ");
  Serial.print(distance1);
  Serial.print(" cm, Distance 2: ");
  Serial.print(distance2);
  Serial.println(" cm");

  // Wait for a short time before measuring again
  delay(100);
}


Controlling 4 Ultrasonic Sensor with Arduino Uno with code


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

// Define the pins for the second ultrasonic sensor
const int trigPin2 = 4;
const int echoPin2 = 5;

// Define the pins for the third ultrasonic sensor
const int trigPin3 = 6;
const int echoPin3 = 7;

// Define the pins for the fourth ultrasonic sensor
const int trigPin4 = 8;
const int echoPin4 = 9;

void setup() {
  // Set the trigger pins as outputs
  pinMode(trigPin1, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  
  // Set the echo pins as inputs
  pinMode(echoPin1, INPUT);
  pinMode(echoPin2, INPUT);
  pinMode(echoPin3, INPUT);
  pinMode(echoPin4, INPUT);
  
  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  // Measure distance for the first sensor
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  long duration1 = pulseIn(echoPin1, HIGH);
  int distance1 = duration1 / 58;

  // Measure distance for the second sensor
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  long duration2 = pulseIn(echoPin2, HIGH);
  int distance2 = duration2 / 58;

  // Measure distance for the third sensor
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  long duration3 = pulseIn(echoPin3, HIGH);
  int distance3 = duration3 / 58;

  // Measure distance for the fourth sensor
  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  long duration4 = pulseIn(echoPin4, HIGH);
  int distance4 = duration4 / 58;

  // Print the distances to the serial monitor
  Serial.print("Distance 1: ");
  Serial.print(distance1);
  Serial.print(" cm, Distance 2: ");
  Serial.print(distance2);
  Serial.print(" cm, Distance 3: ");
  Serial.print(distance3);
  Serial.print(" cm, Distance 4: ");
  Serial.print(distance4);
  Serial.println(" cm");

  // Wait for a short time before measuring again
  delay(100);
}

Controlling 6 Ultrasonic Sensor with Arduino Uno with code

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

// Define the pins for the second ultrasonic sensor
const int trigPin2 = 4;
const int echoPin2 = 5;

// Define the pins for the third ultrasonic sensor
const int trigPin3 = 6;
const int echoPin3 = 7;

// Define the pins for the fourth ultrasonic sensor
const int trigPin4 = 8;
const int echoPin4 = 9;

// Define the pins for the fifth ultrasonic sensor
const int trigPin5 = 10;
const int echoPin5 = 11;

// Define the pins for the sixth ultrasonic sensor
const int trigPin6 = 12;
const int echoPin6 = 13;

void setup() {
  // Set the trigger pins as outputs
  pinMode(trigPin1, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(trigPin5, OUTPUT);
  pinMode(trigPin6, OUTPUT);

  // Set the echo pins as inputs
  pinMode(echoPin1, INPUT);
  pinMode(echoPin2, INPUT);
  pinMode(echoPin3, INPUT);
  pinMode(echoPin4, INPUT);
  pinMode(echoPin5, INPUT);
  pinMode(echoPin6, INPUT);

  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  // Measure distance for the first sensor
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  long duration1 = pulseIn(echoPin1, HIGH);
  int distance1 = duration1 / 58;

  // Measure distance for the second sensor
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  long duration2 = pulseIn(echoPin2, HIGH);
  int distance2 = duration2 / 58;

  // Measure distance for the third sensor
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  long duration3 = pulseIn(echoPin3, HIGH);
  int distance3 = duration3 / 58;

  // Measure distance for the fourth sensor
  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  long duration4 = pulseIn(echoPin4, HIGH);
  int distance4 = duration4 / 58;

  // Measure distance for the fifth sensor
  digitalWrite(trigPin5, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin5, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin5, LOW);
  long duration5 = pulseIn(echoPin5, HIGH);
  int distance5 = duration5 / 58;

  // Measure distance for the sixth sensor
  digitalWrite(trigPin6, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin6, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin6, LOW);
  long duration6 = pulseIn(echoPin6, HIGH);
  int distance6 = duration6 / 58;

  // Print the distances to the serial monitor
  Serial.print("Distance 1: ");
  Serial.print(distance1);
  Serial.print(" cm, Distance 2: ");
  Serial.print(distance2);
  Serial.print(" cm, Distance 3: ");
  Serial.print(distance3);
  Serial.print(" cm, Distance 4: ");
  Serial.print(distance4);
  Serial.println(" cm");
  Serial.print("Distance 5: ");
  Serial.print(distance5);
  Serial.print(" cm, Distance 6: ");
  Serial.print(distance6);
  Serial.print("cm");
  // Wait for a short time before measuring again
  delay(100);
}