Interfacing 2 Ultrasonic sensors with one Arduino

(Representative image, Connections are different)

Ultrasonic sensors are widely used in the Arduino projects to detect objects, distances and system disturbances. Majority of peoples having some problems while trying to use Arduino to drive two HC-SR04 ultrasonic sensor. While trying to display two measured value from each sensor, one of them work fine but the other displays value 0.

 

The situation were two sensors works in parallel, but pulseIn functions are executed in serial. This is the reason why your code doesn't work. Let's image when you toss two balls by your right hand, and try catch them one by one with your right hand. When you catch one ball and put it in a box, another ball had been fallen on the ground. This is why first pulseIn can get correct value and second one fail to get correct value.

 

I have created a programme to display two distance values from different sensors and displays it in a 6x2 LCD. To sole the code problem, I have created a separate function for read the sensor and distance calculation.

 

I have used I2C display library for LCD to reduce wiring complexity. So need to use 2 libraries Wire.h & LiquidCrystal_I2C.h. If you don’t having the same, please go to ‘Manage Libraries’ in Arduino and install the same! Please refer THIS or THIS tutorial for any references.



LCD Connections:

LCD                Arduino

GND  -         Ground

VCC  -         5V

SDA  -         A4

SCL   -         A5

Ultrasonic Connections:

Ultrasonic 1             Arduino

Triger                        10

Echo                         12

VCC                         VCC

GND                         GND

Ultrasonic 2             Arduino

Triger                        9

Echo                         11

VCC                         VCC

GND                         GND

Code:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#define trigPin1 10
#define echoPin1 12
#define trigPin2 9
#define echoPin2 11
LiquidCrystal_I2C lcd(0x27,20,4);
float Duration, Distance, FrontSensor, BottomSensor;
void setup()
{
  lcd.init();
  lcd.backlight();
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
}
void loop()
{
  DistanceCalc();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Front : ");
  lcd.setCursor(10,0);
  lcd.print(FrontSensor, 0);
  lcd.setCursor(0,1);
  lcd.print("Bottom: ");
  lcd.setCursor(10,1);
  lcd.print(BottomSensor, 0);
}
void SensorRead(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  Duration = pulseIn(echoPin, HIGH);
  Distance = (Duration/2) / 29.1;
}
void DistanceCalc()
{
  SensorRead(trigPin1, echoPin1);
  FrontSensor = Distance;
  SensorRead(trigPin2, echoPin2);
  BottomSensor = Distance;
  delay(300);
}

Comments

Popular posts from this blog

Clonebot Technical Documentation

Run your apps in the system background

Create a watch dog script for Local Git Repository to Pull commits !