How to put if else statement to use ultrasonic sensor with relay

Hey guys, I am am trying to make a code which automatically controls relay on basis of ultrasonic sensor measurements. I am using a nodeMCU as a microcontroller rather than an arduino. The relay and the ultrasonic sensor is being powered from a 5V source and nodemcu from a 3.3V Source. I don't think none of the sensors are interfering with each other as all the sensors are kept at a distance.Due to the random values my relay is being latched without any reason. Can someone please help me out with the problem. I don't want these values(above 3000) being shown in the serial monitor. Please help me in the code.

#define TRIGGER 5
#define ECHO    4
#define RELAY   0

void setup() {
  Serial.begin(9600);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
  pinMode(RELAY, OUTPUT);
}
void loop() {
  long duration, distance;
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(10);

  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance < 30 ){
    digitalWrite(RELAY, HIGH);
  }else{  
    digitalWrite(RELAY, LOW);
  }

  Serial.print("Centimeters: ");
  Serial.println(distance);

}

Put a delay(40) at the end of loop().

Report back.

#define RELAY 0Oops

(uncompiled, untested)

const byte TRIGGER = 5;
const byte ECHO  =  4;
const byte  RELAY =   6;. 

void setup() {
  Serial.begin(9600);
  pinMode(TRIGGER, OUTPUT);
  digitalWrite (TRIGGER, LOW);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
  pinMode(RELAY, OUTPUT);
}
void loop() 
{
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);
  unsigned long duration = pulseIn(ECHO, HIGH);
  long distance = (duration / 2) / 29.1;

  digitalWrite(RELAY,  (distance < 30) ? HIGH : LOW);

  Serial.print(distance);
  Serial.println(F("centimetres"));
  delay (40);
}

Tried but same result.

What kind of sensor is this?

Thirty meter range is a lot more than any I have used.

It is a normal hc-sr04 ultrasonic sensor

IIRC, that has a maximum range of about 4m. Something is wrong, either wiring or math I suspect.

Hello my friend,
Try this program out on your device. I believe it is in working order, I am not 100% sure.

int Nose=0;
int trigger_output[4] = {Nose, 9};//    echo 7 trig 8
int echo_input[4] = {7,6};//

int range;
unsigned long distance[2], cm[2], timeout[5];
void setup()
{
  Serial.begin (9600);
  //------------------------------------
  // connect OUTPUT pins to other devices with 470Ω or 1k resistors,
//  pinMode (trigger_output[0, 1],OUTPUT);
// Often it is useful to steer an input pin to a known state if no input is present.
// This can be done by adding a pullup resistor (to +5V), or a pulldown resistor
//(resistor to ground) on the input. A 10K resistor is a good value for a pullup or pulldown resistor.  
  pinMode (echo_input[0],INPUT);
  digitalWrite(trigger_output[0], LOW);   delayMicroseconds(2);
  digitalWrite(trigger_output[0], HIGH);  delayMicroseconds(10);
  digitalWrite(trigger_output[0], LOW);
  //-----------------------------------
  
  pinMode(trigger_output[0], OUTPUT);
  pinMode(echo_input[0], INPUT);
//  pinMode(trigger_output[1], OUTPUT); 
//  pinMode(echo_input[1], INPUT);
  // range = trig - echo
  // iQ4 foxtel

}
void loop() {
  digitalWrite(trigger_output[0], LOW); 
  delayMicroseconds(2);
  digitalWrite(trigger_output[0], HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger_output[0], LOW);
  pinMode(echo_input[0], INPUT);

  distance[0] = pulseIn(echo_input[0], HIGH);
  cm[0] = (distance[0] / 2) / 28;
  Serial.print(cm[0]);
  Serial.print(" cm");
  Serial.println();
  delay (10);

 // digitalWrite(trigger_output[1], LOW);  delayMicroseconds(2);
 // digitalWrite(trigger_output[1], HIGH); delayMicroseconds(10);
 // digitalWrite(trigger_output[1], LOW);
//  pinMode(echo_input[1], INPUT);

 // distance[1] = pulseIn(echo_input[0], HIGH);
 // cm[1] = (distance[1] / 2) / 28;
 // Serial.print(cm[1]);
 // Serial.print(" cm");
 // Serial.println();
 // delay (5);

//  digitalWrite(trigger_output[1], LOW);
//  delayMicroseconds(2);
//  digitalWrite(trigger_output[1], HIGH);
//  delayMicroseconds(10);
//  digitalWrite(trigger_output[1], LOW);
//  pinMode(echo_input[1], INPUT);
//  distanceA = pulseIn(echo_input[1], HIGH);
//  cmA = (distanceA / 2) / 28;
//  Serial.print(cmA);
//  Serial.print(" cmA");
//  Serial.println();
//  delay (5);
}

Hi,
Have you googled.. nodeMCU hc-sr04 ultrasonic sensor

Have you tried the code form this link?

Tom... :slight_smile: