Hi guys.
I'm currently working on a project that uses relay and water level sensor. The problem is, the relay won't switch and I was wondering whether its the program issue or hardware issue.
I'm unable to give you guys the picture, but the wiring is like this:
Relay 2 channel
DC + -> VCC of 5V Power Supply
DC - -> GND of 5V Power Supply
IN1 -> D3 NodeMCU
IN2 -> D4 NodeMCU
When I first power it up, the relay definitely switch, but when reaching the logic of the program, the relay stay silent.
What I want to achive is, when the program starts working R1 is on, but when the wLevel reach 2cm, R1 OFF and R2 On activating the water pump till the water is empty, then R1 is on again.
This is the whole code
#include <Wire.h>
#include <Blynk.h>
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//Initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);
char auth[] = "xxx";//Enter your Auth token
char ssid[] = "xxx";//Enter your WIFI name
char pass[] = "xxx";//Enter your WIFI password
BlynkTimer timer;
#define pinSensor A0  // mendefinisikan pin A0 sebagai pin yang berhubungan dengan sensor
#define PIN_RELAY_1  D3 // the Arduino pin, which connects to the IN1 pin of relay module
#define PIN_RELAY_2  D4 // the Arduino pin, which connects to the IN1 pin of relay module
int sensorValue = 0; // variable untuk menampung nilai baca dari sensor dalam bentuk integer
float tinggiAir = 0; // variabel untuk menampung ketinggian air
float sensorVoltage = 0; // untuk menampung nilai ketinggian air
int nilaiMax = 686; // nilai "sensorValue" saat sensor terendam penuh kedalam air, bisa dirubah sesuai sensor dan jenis air yang anda pakai
float panjangSensor = 4.0 ; // 4.0 cm
void setup() {
  Serial.begin(115200); 
  lcd.begin(); 
  lcd.backlight();
  pinMode(PIN_RELAY_1, OUTPUT);
  pinMode(PIN_RELAY_2, OUTPUT);
  digitalWrite(PIN_RELAY_1, HIGH);
  digitalWrite(PIN_RELAY_2, HIGH);
  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  lcd.setCursor(0, 0);
  lcd.print("Water level");
  lcd.setCursor(4, 1);
  lcd.print("Monitoring");
  delay(4000);
  lcd.clear();
  timer.setInterval(100L, wLevel);
}
void wLevel() {
   sensorValue = analogRead(pinSensor); // membaca tengan dari sensor dalam bentuk integer
  tinggiAir = sensorValue*panjangSensor/nilaiMax;
  sensorVoltage = sensorValue*5.0/686;
//sm
  Serial.print("Sensor Value = ");
  Serial.println(sensorValue);
  Serial.print("Sensor Voltage = ");
  Serial.println(sensorVoltage);
  Serial.print("Tinggi Air = ");
  Serial.println(tinggiAir);
  Serial.println();
  
  // Display ke LCD
  lcd.setCursor(0,0);
  lcd.print("WLevel:");
  lcd.print(tinggiAir);
  lcd.setCursor(12,0);
  lcd.print("cm");
  delay(1000);
  Blynk.virtualWrite(V0, tinggiAir);
  Blynk.run();
  if (tinggiAir >= 2) // if wlevel more than 2 cm, then
     {
        digitalWrite(PIN_RELAY_1, HIGH);
        digitalWrite(PIN_RELAY_2, LOW);
        lcd.setCursor(0,1);
        lcd.print("F:OFF P:ON");
        Blynk.logEvent("wlevel","wLevel more than 2cm"); 
        delay(7000);       
     }
  else
     {
       digitalWrite(PIN_RELAY_1, LOW);
       digitalWrite(PIN_RELAY_2, HIGH);
        lcd.setCursor(0,1);
        lcd.print("F:ON P:OFF");
        delay(2000);
     }
}
void loop() {
      Blynk.run();
     timer.run();
}
            




