How do I make multiple Ultrasonic Sensors work on Blynk with ESP32 Devkit V1

Hello,

I’m trying to connect 2 ultrasonic sensors HC-SR04 to ESP32 Devkit V1 Board, but I’m having a hard time to make both sensors work, only one of them got the readings. I want both sensors to be able to measure their own distances, and how do I display it on the Blynk Dashboard? What should I put on the datastreams? Any idea would help.

Thanks

Here’s my code

//BlynkTimer timer;


#define power  12  //D12
#define trigPin1 23
#define echoPin1 22
#define trigPin2 33
#define echoPin2 32
float distance;
float distance2;

void setup()
{
 pinMode(trigPin1, OUTPUT);
 pinMode(echoPin1, INPUT);
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);
 pinMode(power,OUTPUT);
 digitalWrite(power,HIGH);
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

}

void loop()
{
  Blynk.run();
   // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin1, LOW);
  digitalWrite(trigPin2, LOW);
  
  delayMicroseconds(5);

 // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin1, HIGH);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  digitalWrite(trigPin2, LOW);
  
  // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
int  duration = pulseIn(echoPin1, HIGH);
  
  // Calculate the distance:
  distance = duration*0.034/2;
  distance2 = duration*0.034/2;
  
//distance = (duration/2) / 29.1;
  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
//int waterLevelPer = 400-distance;

  Serial.print("Distance = ");
  Serial.print(distance);
  Serial.println(" cm");
  Serial.println(distance2);
  Blynk.virtualWrite(V0,distance);
  Blynk.virtualWrite(V1,distance2);
  delay(500); 
}```

That’s not a complete sketch, you’re missing all of your library #include lines of code, along with the three lines of Blynk firmware and your WiFi credential declarations.
Please post your complete sketch, and replace any sensitive information with the word “REDACTED”.

You have all of the logic processing in your void loop, with a blocking delay to control timing.
This doesn’t work with Blynk, you need to use Blynk timer to call functions which contain your logic processing code. Read this…

In addition, you can’t read two distance sensors at the same time, they require critical timing and this can only be achieved by reading one, then the other, then uploading the results to Blynk.

I don’t understand the question.

Pete.

Thank you, Pete, for the response.

I actually figured it out how to use multiple ultrasonic sensors on ESP32. Here’s my code with all the library and that sort of things.

// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "template id"
#define BLYNK_DEVICE_NAME "ultrasonic"
#define BLYNK_AUTH_TOKEN "token"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "REDACTED";
char pass[] = "REDACTED";

//BlynkTimer timer;


#define power  12  //D12

#define trigPin1 33
#define echoPin1 32
float distance1;

#define trigPin2 23
#define echoPin2 22
float distance2;

void setup()
{
 pinMode(trigPin1, OUTPUT);
 pinMode(echoPin1, INPUT);
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);
 
 pinMode(power,OUTPUT);
 digitalWrite(power,HIGH);
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

}

void loop()
{
  Blynk.run();
   // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin1, LOW);
  
  delayMicroseconds(5);

 // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  
  // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
int duration = pulseIn(echoPin1, HIGH);

  // Calculate the distance:
  distance1 = duration*0.034/2;

 digitalWrite(trigPin2, LOW);
  
  delayMicroseconds(5);

 // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  
  // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
int duration2 = pulseIn(echoPin2, HIGH);

  // Calculate the distance:
  distance2 = duration2*0.034/2;

  
//distance = (duration/2) / 29.1;
  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
//int waterLevelPer = 400-distance;

  Serial.print("Distance = ");
  Serial.print(distance1);
  Serial.println(" cm");
  Serial.print("Distance2 = ");
  Serial.print(distance2);
  Serial.println(" cm");

  Blynk.virtualWrite(V0,distance1);
  Blynk.virtualWrite(V1,distance2);
  delay(500); 
}

With this code, both ultrasonic sensors actually reads their distance independently. No interruption from one and another. Let me know if I got something incorrect.

(EDIT) Oh and about the datastreams, I just make two datastreams. One for distance1 with pin V0, and the other one is distance2 with pin V1.

Thanks again. Pete

Rai.

You obviously didn’t read the “keep your void loop clean” link I provided, or read the comments I made about moving your logic processing code not using blocking delays, otherwise you would have had just two lines of code in your void loop, not all of this…

Pete.