Sensor with relay issue

hi evrybody
cane someone please help my by my code ?
i truing to turn the relay on when the sensor reach the threshold
all the leds are working on series but the relay not

if ( distance <= distance_threshold1 )
  {
    digitalWrite(LED1, HIGH);
    digitalWrite(relay, HIGH);
    digitalWrite(buzzer, LOW);
  }
  else
  {
    digitalWrite(LED1, LOW);
    digitalWrite(relay, LOW);
    digitalWrite(buzzer, LOW);
  }
  if ( distance <= distance_threshold2 )
  {
    digitalWrite(LED2, HIGH);
    digitalWrite(relay, HIGH);
    digitalWrite(buzzer, LOW);
  }
  else
  {
    digitalWrite(LED2, LOW);
    digitalWrite(relay, LOW);
    digitalWrite(buzzer, LOW);
  }
  if ( distance <= distance_threshold3 )
  {
    digitalWrite(LED3, HIGH);
    digitalWrite(relay, LOW);
    digitalWrite(buzzer, LOW                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        );
  }
  else
  {
    digitalWrite(LED3, LOW);
    digitalWrite(relay, LOW);
    digitalWrite(buzzer, LOW);
  }
  if ( distance <= distance_threshold4 )
  {
    digitalWrite(LED4, HIGH);
    digitalWrite(relay, HIGH);
    digitalWrite(buzzer, HIGH);
  }
  else
  {
    digitalWrite(LED4, LOW);
    digitalWrite(relay, LOW);
    digitalWrite(buzzer, LOW);
  }

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.


Do you know how esle if( . . . ) works ?



  if ( distance <= distance_threshold3 )
  {
    digitalWrite(LED3, HIGH);
    digitalWrite(relay, LOW);
    digitalWrite(buzzer, LOW                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        );
  }

This would throw an error.

no .. thats why i asked for help

The way things work here:

You give us the information we needed so we can offer the best possible help.

Please show your complete code. We e.g. don't know what the threshols are.

You refer to threshold but there are three thresholds in your code so please explain what you exactly want to achieve.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

have you set the pinmode of relay in setup?

why don't you post a full compilable sketch so we can see what you have done?

This is my entire code

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define trigPin 5
#define echoPin    4

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "..";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "W.";
char pass[] = "100";

int LED1 = 14;
int LED2 = 12;
int LED3 = 13;
int LED4 = 15;
const int relay = 0;
const int distance_threshold1 = 30;
const int distance_threshold2 = 40;
const int distance_threshold3 = 50;
const int distance_threshold4 = 60;
float duration, distance;

WidgetLCD lcd2(V4); // LCD untuk Blynk
BlynkTimer timer; //Timer untuk Blynk

void Sensor()
{

while( true ) {

duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(15);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;

if ( distance <= distance_threshold1 )
  {
    digitalWrite(LED1, HIGH);
  }
  else
  {
    digitalWrite(LED1, LOW);
  }
  if ( distance <= distance_threshold2 )
  {
    digitalWrite(LED2, HIGH);
  }
  else
  {
    digitalWrite(LED2, LOW);
  }
  if ( distance <= distance_threshold3 )
  {
    digitalWrite(LED3, HIGH);
  }
  else
  {
    digitalWrite(LED3, LOW);
  }
  if ( distance <= distance_threshold4 )
  {
    digitalWrite(LED4, HIGH);
  }
  else
  {
    digitalWrite(LED4, LOW);
  }

Serial.print("Ultrasonic detect movement");
Serial.print("Distance: ");
Serial.print(distance, 1); Serial.println(" cm");
lcd2.clear();
lcd2.print(3,0, "ULTRASONIC");
lcd2.print(3,1, distance);
lcd2.print(11,1, "CM");
delay(250);

Blynk.virtualWrite(V4, distance);

delay(250);
}
}

void setup() {
Serial.begin( 19200 ); // Start the serial port

pinMode(LED1,OUTPUT); //Define the pin mode
pinMode(LED2,OUTPUT); //Define the pin mode
pinMode(LED3,OUTPUT); //Define the pin mode
pinMode(LED4,OUTPUT); //Define the pin mode
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(relay, OUTPUT);

Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, Sensor);

}

void loop() {
Blynk.run();
timer.run(); // running timer every second


}```

This is my entire code

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define trigPin 5
#define echoPin    4

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "..";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "W.";
char pass[] = "100";

int LED1 = 14;
int LED2 = 12;
int LED3 = 13;
int LED4 = 15;
const int relay = 0;
const int distance_threshold1 = 30;
const int distance_threshold2 = 40;
const int distance_threshold3 = 50;
const int distance_threshold4 = 60;
float duration, distance;

WidgetLCD lcd2(V4); // LCD untuk Blynk
BlynkTimer timer; //Timer untuk Blynk

void Sensor()
{

while( true ) {

duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(15);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;

if ( distance <= distance_threshold1 )
  {
    digitalWrite(LED1, HIGH);
  }
  else
  {
    digitalWrite(LED1, LOW);
  }
  if ( distance <= distance_threshold2 )
  {
    digitalWrite(LED2, HIGH);
  }
  else
  {
    digitalWrite(LED2, LOW);
  }
  if ( distance <= distance_threshold3 )
  {
    digitalWrite(LED3, HIGH);
  }
  else
  {
    digitalWrite(LED3, LOW);
  }
  if ( distance <= distance_threshold4 )
  {
    digitalWrite(LED4, HIGH);
  }
  else
  {
    digitalWrite(LED4, LOW);
  }

Serial.print("Ultrasonic detect movement");
Serial.print("Distance: ");
Serial.print(distance, 1); Serial.println(" cm");
lcd2.clear();
lcd2.print(3,0, "ULTRASONIC");
lcd2.print(3,1, distance);
lcd2.print(11,1, "CM");
delay(250);

Blynk.virtualWrite(V4, distance);

delay(250);
}
}

void setup() {
Serial.begin( 19200 ); // Start the serial port

pinMode(LED1,OUTPUT); //Define the pin mode
pinMode(LED2,OUTPUT); //Define the pin mode
pinMode(LED3,OUTPUT); //Define the pin mode
pinMode(LED4,OUTPUT); //Define the pin mode
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(relay, OUTPUT);

Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, Sensor);

}

void loop() {
Blynk.run();
timer.run(); // running timer every second


}


You are defining relay as connected on pin D0 (zero)


const int relay = 0;
. . .
pinMode(relay, OUTPUT);


Pin 0 on an Arduino UNO is for serial communications.

Don’t use pin D0 or D1.
Use D2 thru D13.

In the sketch in post #8, do not see where you are actually doing a digitalWrite to relay :thinking:


You will need to add:

digitalWrite(relay, HIGH);
Or
digitalWrite(relay, LOW);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.