[SOLVED] Ultrasonic sensor HC-SR04 arduino

Congratulations to the authors for the project.
If you can help someone sent him schetch for the ultrasonic sensor HC-SR04.
App on the set virtual pin N 8 or to taste just change the schetch.

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

char auth[] = "xxxxxxx";

#define echoPin 8 // Echo Pin
#define trigPin 9 // Trigger Pin

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

void setup() {
 Serial.begin(9600);
 Blynk.begin(auth);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 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(500);
 Blynk.run();
}

BLYNK_READ(V8)
{
Blynk.virtualWrite(8,distance);
}
1 Like

You are using a delay in the loop(). That is not a good idea with Blynk. It’s better to use the SimpleTimer library and time that to go off at certain intervals. There are some examples with Blynk how to use that (push data or send data, one of them has a Timer example).

1 Like

Thanks for your report … sorry but I’m learning … I think I’ve solved send the code …
the code works perfectly, but I have a doubt, the part to blynk BLYNK_READ (V8) is correct? positioned exactly on the code?

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

char auth[] = "xxxxxxxxx";

SimpleTimer timer;

#define echoPin 8 // Echo Pin
#define trigPin 9 // Trigger Pin

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

void RepeatTask()
{
  /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 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);
}

void setup() {
 Serial.begin(9600);
 Blynk.begin(auth);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 timer.setInterval(1000, RepeatTask);
}

BLYNK_READ(V8)
{
Blynk.virtualWrite(8,distance);// virtualpin 8 distance
}

void loop() {
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}
2 Likes

That is fine there. I usually put them even before setup(), but this should work fine too. Does it do what it needs to do? I assume you want to write the value of distance to a display widget every x-seconds?

It just works correctly ok :wink:

Thanks !!
Dude this code is so useful for my college project thank u.

Tks

hi,

Please help me using above code shall I able to see data in Graph via Virtual Pin.

I having NodeMCU 826612E

Thanks

Closing really old, and solved, topic.

This topic is almost two years old. Please create a new topic, show your work so far and make your Blynk related questions clear and concise. Thank you.