How to get usable value in arduino from "Sens Comp - Smart sensor".

Sens Comp - smart sensor is an ultrasonic ranging module

Objective : TO measure height of humans.

Issue : Had interfaced the sensor with node mcu (esp 8266 - 12E ) board. The output that i get isn't in a usable way .

kindly guide me in which part i'm going wrong.

Sensor descriptions :

http://www.senscomp.com/ultrasonic-sensors/complete-ultrasonic-ranging-solutions.php

The following is the code that i used

#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);
}

The output that i get is as follows

using CTRL-T in the IDE makes the code more readable

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);
}

the output is as follows

Distance:cm -2807
3049
Distance:cm -2799
2821
Distance:cm -2571
2822
Distance:cm -2572
2820
Distance:cm -2570
2820
Distance:cm -2570
2823
Distance:cm -2573
2821
Distance:cm -2571
2822
Distance:cm -2572
2821
Distance:cm -2571
2821
Distance:cm -2571
2820
Distance:cm -2570
2822
Distance:cm -2572
2821
Distance:cm -2571
2822
Distance:cm -2572
2822
Distance:cm -2572
2823
Distance:cm -2573
2824
Distance:cm -2574
2822
Distance:cm -2572
2823
Distance:cm -2573
2822
Distance:cm -2572
2824
Distance:cm -2574
2823
Distance:cm -2573
2822
Distance:cm -2572
2823
Distance:cm -2573
2823
Distance:cm -2573
2823
Distance:cm -2573
2824
Distance:cm -2574
2823
Distance:cm -2573
2822
Distance:cm -2572
2824
Distance:cm -2574
2823
Distance:cm -2573
2824
Distance:cm -2574
2824
Distance:cm -2574

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.

The code was made to measure height of humans.

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.

raghulsuraj:

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.

The sensor is from company named Sens comp
and the sensor name is Smart sensor

http://www.senscomp.com/pdfs/smart-sensor-spec.pdf

the above link is the technical documentation of the sensor.

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?

The sensor reads values till trig pin is high that,s why after the reading is done setting it to low.

Kind guide me if i'm wrong any were.

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?

Kind guide me if i'm wrong any were.

It is wrong to set the trigger pin (INIT pin) low 10 us after triggering. That is why the sensor is not working.

Follow the instructions in the sensor data sheet.

@wv marley the code is not for Sens comp - smart sensor. It's for HC -SR 04

(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.

@jremington

the corrections were made and sensor stared to behave erratically.

the output is as follows

either the output is 0 or 200 or 250 as a constant output

Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0
Distance:cm 0

Posting code would make more sense than this output (which can also do with code tags, by the way).

@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) .

Kindly assist me if i'm going wrong some were.

The main code is as follows

#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[] = "xxxxxxxxxxxxx";
// defines variables
long duration;
int distance;
char ssid[] = "xxx";
char pass[] = "xxxxxxxx";
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);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  if (distance > 1)
  {
    value = height - distance;
    Serial.println(distance);
  }
  Serial.print("Distance:cm  ");
  Serial.println(value);
  Blynk.virtualWrite(V4, value);
  Blynk.run();
  delay(100);
}

Code with corrections made that tend to make the sensor behave erratically is as follows

the output for the below code is either 0 or 200 or 250

#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[] = "XXXXXXXXXXXX";
// defines variables
long duration;
int distance;
char ssid[] = "XXX";
char pass[] = "XXXXXXXX";
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);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  if (distance > 1)
  {
    value = height - distance;
    Serial.println(distance);
  }
  Serial.print("Distance:cm  ");
  Serial.println(value);
  Blynk.virtualWrite(V4, value);
  Blynk.run();
  delay(100);
}

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.