#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// defines pins numbers
const int trigPin = 2; //D4
const int echoPin = 0; //D3
int height = 250;
int value = 0;
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxx";
// defines variables
long duration;
int distance;
char ssid[] = "xxxxx";
char pass[] = "xxxxxxxxxx";
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
Blynk.begin(auth, ssid, pass);
}
void loop() {
long duration, distance;
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
if (distance > 1)
{
value = height - distance;
Serial.println(distance);
}
Serial.print("Distance:cm ");
Serial.println(value);
Blynk.virtualWrite(V4, value);
Blynk.run();
delay(100);
}
Please copy/paste actual output here - instead of poorly readable text on a photo of a screen on an external site. Also you have to explain what's wrong with that output, what do you expect to see?
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// defines pins numbers
const int trigPin = 2; //D4
const int echoPin = 0; //D3
int height = 250;
int value = 0;
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxx";
// defines variables
long duration;
int distance;
char ssid[] = "xxxxx";
char pass[] = "xxxxxxxxxx";
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
Blynk.begin(auth, ssid, pass);
}
void loop() {
long duration, distance;
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
if (distance > 1)
{
value = height - distance;
Serial.println(distance);
}
Serial.print("Distance:cm ");
Serial.println(value);
Blynk.virtualWrite(V4, value);
Blynk.run();
delay(100);
}
Best guess is that you miss a factor of 100 in your calculation, but it's just a guess for your lack of explanation of what's wrong with those numbers.
Have set a height as 250 cms
As the sensor measures the new value is stored in duration.
the difference between the height and duration is stored in value
The sensor is giving a random value and on presence of obstacle it's value is increasing
have no clue to obtain a usable value from the sensor.
have no clue to obtain a usable value from the sensor.
Well, start there, then. Get a simple piece of code that just reads the sensor and puts the measured distance on the Serial monitor.
Or provide a link to the sensor you actually use, not a page with a lot of such sensors.
The code looks like what you'd use for the HC-SR04 but I don't see that one on the page you link to, just lots of other sensors.
It also helps to figure out what values you should get. I asked twice, haven't got a reply to that part, so that probably means you even have no clue on what value you should be expecting. Without knowing what value to expect from a sensor, little chance to get it to do something useful for you.
echo output - TTL compatible logic evel output (0-5 VDC). Changes state when an echo signal is received.
Trig input - TTL compatible logic level input. Initializes a transmit/receive cycle on the low to high transition. This signal must remain high for the duration of target detection period
raghulsuraj:
Trig input - TTL compatible logic level input. Initializes a transmit/receive cycle on the low to high transition. This signal must remain high for the duration of target detection period
Then why do you set it LOW after 10 us in your code?
You set the pin low, then start measuring. That seems to contradict that datasheet quote. Where did you get the code you use? Is it meant for this specific sensor?
(Almost) all sensors work in a different way. You can't take code for one sensor and apply it to another. You have to look for a library that's specific for use with your sensor, and if nothing available, write your own based on the instructions in the data sheet.
@wvmarle had made the code after reading the instructions in data sheet
The data sheets talk about
.................... Under the topic - Single echo mode ......................
the INIT (trig pin) goes high and low to make subsequent transitions.
Echo output goes high till the INIT (trig pin) is high.
Till the INIT is high echo output gives values once INIT pin goes low the echo output stops. to make next transition the INIT pin has to made high again to continue the same process.
The code works in the same way sir ( to my knowledge) .
Again, you are not reading the data sheet. The maximum repetition rate is 5 Hz.
Two microseconds is not enough for the device to reinitialize.
void loop() {
long duration, distance;
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
Set the INIT pin low after receiving the echo, and wait at least 200 milliseconds before triggering another pulse.
Have you connected the grounds? Post a hand drawn wiring diagram.