Problem with ultrasonic

I am using an uno and a ultrasonic sensor.

when i plug it in normally(bblynk not connected) it gives correct values.

But when i put the same code using blynk, it does not work. please help me with this.

Can you post your whole Blynk-added code? It makes it kind of hard to guess what’s wrong now.

indent preformatted text by 4 spaces

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

WidgetLED led1(V1);
WidgetLED led2(V1);
WidgetLED led3(V1);
WidgetLED led4(V1);
WidgetLCD lcd(V5);

char auth[] = “cf85da98b53e4b62941f4fa328c43794”;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
// pinMode(ultraSoundSignal,OUTPUT);
}

void loop()
{
int x = 0;
x = sonar.ping_cm();
delay(500); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
Serial.println(x);

if(x== 0)
{
Serial.println(“1 on”);
led1.on();
led2.off();
led3.off();
led4.off();
}
if (x> 5 && x< 10)
{
led2.on();
Blynk.notify(“Quarter of the tank is full!!”);
// Blynk.email("kaustubhagarwal18@gmail.com", “Subject: Security”, “Please Check! Something fishy!”);
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(4, 0, "Quarter "); // use: (position X: 0-15, position Y: 0-1, “Message you want to print”)
lcd.print(4, 1, “Full!”);

}
if(x>10 && x<15)
{
led3.on();
Blynk.notify(“More than half of the tank is full!”);
Blynk.tweet(“I am almost half full! #arduino #iot #blynk #smartwatertank”);
// Blynk.email("kaustubhagarwal18@gmail.com", “Subject: Security”, “Please Check! Something fishy!”);
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(4, 0, "More than Half “); // use: (position X: 0-15, position Y: 0-1, “Message you want to print”)
lcd.print(4, 1, “is Full!”);
}
if(x>15)
{
led4.on();
Blynk.notify(“You can close the motor now. Almost full!”);
Blynk.email("kaustubhagarwal18@gmail.com”, “Subject: Water Tank”, “You need to close it now!!”);
Blynk.tweet(“I am almost full! Stop The supply #arduino #iot #blynk #smartwatertank”);
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(4, 0, "Almost "); // use: (position X: 0-15, position Y: 0-1, “Message you want to print”)
lcd.print(4, 1, “Full!”);
}

delay(1000);//delay 1 seconds.
Blynk.run();
}

NORMAL CODE:

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
}

You are putting all the code in your loop() from what I can tell (kind of hard to read, you can use the </> button wrap it in three backticks ` to make it more readable for us) and you use delays. This is most likely (99% sure) the reason it doesn’t work. It’s better to call timed functions with the SimpleTimer library. There is an example called PushData which shows how to do that.

Hi @KAUSTUBHAGARWAL,

Adapt this code to your needs:

#include <ESP8266_HardSer.h>
#include <BlynkSimpleShieldEsp8266_HardSer.h>
#include <SimpleTimer.h>
#include <NewPing.h>


// Set ESP8266 Serial object
#define EspSerial Serial
ESP8266 wifi(EspSerial);
#define TRIGGER_PIN  9  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     8  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 
#define PING_INTERVAL 200 // Milliseconds between pings.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "YourAuthToken";

WidgetLCD lcd(V0);

SimpleTimer timer;


void setup()
{
  Serial.begin(9600);
  delay(10);
  
  EspSerial.begin(9600);
  delay(10);
  
  Blynk.begin (auth, wifi, "ssid", "pass");
  delay(10);

  timer.setInterval(1000L, ultra);
}

void ultra()
{
  unsigned int uS = sonar.ping();
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(0,0,"Distance (cm):");
  lcd.print(0,1,(uS / US_ROUNDTRIP_CM));
}

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

The result adding a LCD widget (advanced mode) at V0:

There’s only an issue when the distance is less than 10 cm… the Arduino goes down and disconnects from the server, I have no idea about it… Really strange for me…

2 Likes

better use basic function instead of Newping
I already used Newping to test it, but I prefere to use that


  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = ((duration / 2) / 29.1) * 10;
1 Like

Hehe… @Blynk_Coeur It looks like you got caught in a time warp… Responding to a 3 year old post is the best late response I have seen yet :rofl::rofl:

Sorry, but closing this old topic. :wink: RIP

3 Likes