Hi! We’re currently working with a project entitled Hydroponics: Water Level Monitoring and Irrigation. When the hc-sr04 sensed that the water level is low, it will send a notification in blynk and automatically open the water pump. I’m using NodeMCU, 6V-9V water pump, 5V 1ch relay. We already have a code but is not tested yet. We’re having a debate whether to connect
(a.) a 3V supply to the nodemcu, 5V supply to the relay and another 6V supply to the water pump or (b.) do we just supply the 3V in nodemcu, and 5V in relay? We are afraid that we might damage the nodemcu. We connect a 2N2222 transistor and 4.7kohm resistor in the relay.
And if possible, we would really appreciate it if you check our code. We already compiled it in Arduino IDE and it has no error, but we are still hesitant if it will really work.
//Define Pin Numbers
const int trigPin = D1;
const int echoPin = D2;
const int PumpPin = D5; //relay --OUTPUT
//Define Variables
long duration;
int distance;
#include <ESP8266WiFi.h>
#include <Blynk.h>
#include <BlynkSimpleEsp8266.h>
//WiFi Connection//
char auth [] =" ";
char ssid [] =" ";
char pass [] =" ";
//TIMER//
BlynkTimer timer;
//BLYNK//
//PINV2 IS LEVEL V WHICH MEASURES DISTANCE
void MeasureCm()
{
//clears the trigPin
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
//sets the trigPin on HIGH state for 10 microseconds
digitalWrite (trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
//Calculating the distance and volume
distance=duration*0.034/2;
{
if(distance>=10)
{
Blynk.notify("Water is low. Water Pump On."); //notifies the Blynk user when the water level is low
digitalWrite(PumpPin, LOW); // To be used with Relay module (inverted logic: activate with LOW)
}
else
{
if(distance<10)
{
Blynk.notify("Water is high. Water Pump Off."); //notifies the Blynk user when the water level is high
digitalWrite(PumpPin, HIGH); // To be used with Relay module (inverted logic)
}
}
}
}
void sendData()
{
//SEND DATA TO BLYNK
Blynk.virtualWrite(V2,distance); //virtual pin 2 in Blynk -- Level V
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth,ssid,pass);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(PumpPin,OUTPUT);
digitalWrite(PumpPin,HIGH); // To be used with Relay module (inverted logic: normally HIGH)
timer.setInterval(5000,sendData);
}
void loop()
{
timer.run(); //initiates SimpleTimer
Blynk.run(); //initiates Blynk
}