BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Two Ultrasonic sensor

Hi and Hello everyone!!
I’m a beginner in esp32+blynk try to do final year project. I try to do by my own for two ultrasonic sensor HC-SR04 connecting with Blynk. But the 2nd sensor cannot read and just show the exact same value like 1st ultrasonic sensor. Hope you guys can help me.Thanks in advance

Here is some information of the hardware and the code is attached below

• ESP32 + Wi-Fi.
• iPhone (IOS 16)
• Blynk server
• Blynk Library version 1.0
• Code.

#define BLYNK_TEMPLATE_ID "****"
#define BLYNK_DEVICE_NAME "CAT HOME MONITORING SYSTEM"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define BLYNK_DEBUG

#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"
#define trig 12
#define echo 13
#define trig1 14
#define echo1 15

BlynkTimer timer;
long tankDepth=24;
long tankDepth1=24;

void waterLevel(){
  digitalWrite(trig,LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long t = pulseIn(echo, HIGH);
  long cm = t / 29 / 2;
  Serial.println(cm);
  double level= tankDepth-cm;
  if(level>0){
    long percentage=((level/tankDepth))*100;
    Blynk.virtualWrite(V0,percentage);
  }
  else{
    Blynk.virtualWrite(V0,0);
  }
  delayMicroseconds(2);
  digitalWrite(trig1,LOW);
  delayMicroseconds(2);
  digitalWrite(trig1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig1, LOW);
  long t1 = pulseIn(echo1, HIGH);
  long cm1 = t1 / 29 / 2;
  double level1= tankDepth1-cm;
  Serial.println(cm1);
  if(level1>0){
    long percentage=((level1/tankDepth1))*100;
    Blynk.virtualWrite(V3,percentage);
  }
  else{
    Blynk.virtualWrite(V3,0);
  }
}

void setup()
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(trig1, OUTPUT);
  pinMode(echo1, INPUT);
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
  timer.setInterval(5000L, waterLevel);
}

void loop() {
  BlynkEdgent.run();
  timer.run();
}

Please edit your post and add triple backticks ``` before and after your whole sketch.

1 Like

I’m sorry, already edited

Wrong character, triple backticks look like this ```

1 Like

Done…sorry

I spotted a minor error…

and if it was me, I’d have called this percentage1

but I don’t think either of these are causing your issue.

I’d be inclined to split void waterLevel() into two functions - one to measure tank 1 and the other to measure tank 2. You can then call them, independently to check that they work and give different results. You could then use two BlkynkTimers, or call the second function from the first one.

Also, have you checked the min/max values for datastreams V0 and V3 to ensure that they are both 0-100 ?

Pete.

2 Likes

Hi Pete,
First of all Thanks a lot for replying. I’ll try to edit the code based on your advice and yes I already set value for both 0-100.

Sincerely,
Azar

1 Like

Hi @PeteKnight , is it look like this what you said just now.

#define BLYNK_TEMPLATE_ID "****"
#define BLYNK_DEVICE_NAME "CAT HOME MONITORING SYSTEM"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define BLYNK_DEBUG
#define USE_ESP32S2_DEV_KIT

#include "BlynkEdgent.h"
#define trig 12
#define echo 13
#define trig1 14
#define echo1 15

BlynkTimer timer;
BlynkTimer timer1;

long tankDepth=24;
long tankDepth1=24;

void waterLevel(){
  digitalWrite(trig,LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long t = pulseIn(echo, HIGH);
  long cm = t / 29 / 2;
  Serial.println(cm);
  double level= tankDepth-cm;
  if(level>0){
    long percentage=((level/tankDepth))*100;
    Blynk.virtualWrite(V0,percentage);
  }
  else{
    Blynk.virtualWrite(V0,0);
  }
}
void foodLevel(){
  digitalWrite(trig1,LOW);
  delayMicroseconds(2);
  digitalWrite(trig1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig1, LOW);
  long t1 = pulseIn(echo1, HIGH);
  long cm1 = t1 / 29 / 2;
  Serial.println(cm1);
  double level1= tankDepth-cm1;
  if(level1>0){
    long percentage1=((level1/tankDepth))*100;
    Blynk.virtualWrite(V3,percentage1);
  }
  else{
    Blynk.virtualWrite(V3,0);
  }
}


void setup()
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(trig1, OUTPUT);
  pinMode(echo1, INPUT);
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
  timer.setInterval(10L, waterLevel);
  timer1.setInterval(10L, foodLevel);
}

void loop() {
  BlynkEdgent.run();
  timer.run();
  timer1.run();
}

You don’t ned to do this. One timer object can handle up to 16 timers. so just stick with…

BlynkTimer timer;

you’ve gone from taking a reading every 5 seconds to taking two every 10 milliseconds and Blynk can’t cope with that…

You should probably do this:

  timer.setInterval(5000L, waterLevel);
  delay(500);
  timer.setInterval(5000L, foodLevel);

The delay between the two timers means that they start half a second apart from each other.

If you want more info on timers you should read this (although it’s probably best reserved for when you’re having a sleepless night and counting sheep isn’t working)…

Pete.

1 Like

Thanks a lot!! sure I’ll go through that…right now its 2.50 a.m. in Malaysia and counting sheep not worked…Thats why Im try to solve this problem…Haha
Nice to know you Sir.

1 Like

Hi, percentage i think not going to show correct value. Becase ultrosonic sensor minimum value 19 sm, max value more than 400. I used map function.
I advice you to search for arduino map function

1 Like

Hi Kamram, Thank You for idea…really appreciate it…but in my case its worked sorry for the late reply​:relaxed::pray:t3:…anyway will try your idea in future.:muscle:t2::muscle:t2::muscle:t2: