BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

HC-SR04 Ultrasonic Sensor Does Not Write To Serial

I have an project where i can turn on off my Coffe Maker from the app that is time based. That part works fine. But when i try to add an Ultrasonic Sensor to the code, i can’t get any reading to the serial.

Sometimes it shows only the distance one time, but nothing after that.

All works regards to the ultrasonic sensor when i remove all other code. So my question is why i can’t get any readings from my sensor from the Serial monitor with the attatched code under?

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

//Put WiFi cridentials and token here----------------------------------

char auth[] = "";
char ssid[] = "";
char pass[] = "";

//---------------------------------------------------------------------

#define echoPin D6 // Echo Pin
#define trigPin D5 // Trigger Pin

long duration, distance; // Duration used to calculate distance

int coffeeSwitch = 13;
int yellowLED = 4;
int greenLED = 5;

BlynkTimer timer;

int coffeeTimer;

void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  pinMode(coffeeSwitch, OUTPUT);
  digitalWrite(coffeeSwitch, HIGH);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  timer.setInterval(2000L, waterLevel);
}

void waterLevel(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
Serial.println(distance);
//Delay 50ms before next reading.
delay(50);
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
}

void LEDtimer() {
  digitalWrite(greenLED, LOW);
}

void coffeeOFF() {
  Blynk.virtualWrite(V1, LOW);
  digitalWrite(coffeeSwitch, HIGH);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED, HIGH);
  Blynk.notify("Kaffen er klar!");
  timer.setTimeout(120000L, LEDtimer);
}

BLYNK_WRITE(V1) {
  int pinValue = param.asInt();
  if (pinValue == 1)   {
    digitalWrite(coffeeSwitch, LOW);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(greenLED, LOW);
    coffeeTimer =  timer.setTimeout(720000L, coffeeOFF);
  }
  if (pinValue == 0) {
    digitalWrite(coffeeSwitch, HIGH);
    digitalWrite(yellowLED, LOW);
    timer.deleteTimer(coffeeTimer);
  }
}

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

Just for clarifying. The ultrasonic Sensor i want ti use for measure water level in Coffe Maker water tank. I’m using Wemos D1 Mini (ESP8266).

…haven’t used it with blynk, but two things:

  • the standard SR04 model is for 5V, so a voltage devider is needed for the GPIO on the wemos, connected to the echo pin.
  • can’t tell if your code is driving it correctly. I’d use / try to incorporate the new-ping lib from here: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home

I tried the library you linked to and that works fine also, if i not include my own code.

So the first code works, but not the bottom code:

#include <NewPing.h>

#define TRIGGER_PIN  D5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     D6  // 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(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(1000);                     // 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");
}

Do not work:

#include <NewPing.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


#define TRIGGER_PIN  D5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     D6  // 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.

//Put WiFi cridentials and token here----------------------------------

char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";

//---------------------------------------------------------------------

int coffeeSwitch = 13;
int yellowLED = 4;
int greenLED = 5;

int coffeeTimer;

BlynkTimer timer;

void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(115200);
  pinMode(coffeeSwitch, OUTPUT);
  digitalWrite(coffeeSwitch, HIGH);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  timer.setInterval(1000L, waterLevel);
  
}

void waterLevel(){
  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");
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
}

void LEDtimer(){
  digitalWrite(greenLED, LOW); 
}

void coffeeOFF() {
 Blynk.virtualWrite(V1, LOW);
 digitalWrite(coffeeSwitch, HIGH); 
 digitalWrite(yellowLED, LOW); 
 digitalWrite(greenLED, HIGH); 
 Blynk.notify("Kaffen er klar!");
 timer.setTimeout(120000L, LEDtimer);
 }

BLYNK_WRITE(V1) {
  int pinValue = param.asInt();
  if (pinValue == 1)   {
    digitalWrite(coffeeSwitch, LOW);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(greenLED, LOW); 
   coffeeTimer =  timer.setTimeout(720000L, coffeeOFF);
  }
  if (pinValue == 0) {
    digitalWrite(coffeeSwitch, HIGH); 
    digitalWrite(yellowLED, LOW); 
    timer.deleteTimer(coffeeTimer); 
  }
}

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

Does your serial monitor print “Ping” at all?

Try removing all but the water height function. Do not even enable Serial.begin. Instead of printing value, put a display widget in your app, assign the read to a variable and do a Blynk virtualWrite if that variable to the display widget.

#include <NewPing.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


#define TRIGGER_PIN  D5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     D6  // 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.

//Put WiFi cridentials and token here----------------------------------

char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";

//---------------------------------------------------------------------
float distanceWater = 0.0;
BlynkTimer timer;

void setup() {
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, waterLevel);
  
}

void waterLevel(){
  distanceWater = (sonar.ping_cm());
  Blynk.virtualWrite(V4,distanceWater);
}


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

Yes, the last code work. It writes to the app widget and to Serial mnitor when i enable it. But not when i add my own code.

There’s the path to your solution . . . Start with the working code, then slowly add one line (or one function) at a time, and check results.

When the code fails you should have narrowed it down to a small section, rather that “all my code”

cul
billd

Yes, i know that. I just wonder if there was an obvious error in my code. But since it isn’t, i’m going to try that metod :slight_smile:

Hello,
i ´ve got a problem with my Blynk app
I use ESP32 and ESP8266 board.
In app I use Level V Settings with HC-SR04.
I havent had a problem yet
But since last week it didnt work.
I Think it might be a problem in reading rate.
My scatch is still the same And I havent changed anything.
I wonder if you might change something there.
I see the values in my serial monitor But i Really couldnt see it in the app.

Thank your for checking

Daniel

See this topic…

Pete.