I using a nodemcu v2 esp8266 for my automatic watering system. I used 3v - 5v DC pump for the watering system. The pump is controlled based on the soil moisture level ( or Blynk ). When the code was uploaded, the pump will start running. But when I put my soil moisture sensor into the water, the pump is still running, which means the pump is running non stop. The serial monitor shows that the soil moisture sensor and Blynk are able to control the pump, but the pump just keep running non stop. I've tried a lot of coding and connection but it just show the same. It's there any parts that I did something wrong?
*I use ( DC-DC 5v to 1.2 - 24V LED Digital Power Supply Module USB Step Up/Down ) as my power supply which connected to my laptop. Don't know that if it will affect the results.
#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_TEMPLATE_NAME "***"
#define BLYNK_AUTH_TOKEN "***"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define SOIL_MOISTURE_PIN A0
#define THRESHOLD_MOISTURE 10
#define PUMP_PIN D1
#define PUMP_SWITCH V6
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "***";
char pass[] = "***";
BlynkTimer timer;
bool isPumpOn = false;
void sendSensorData()
{
int soilMoisture = analogRead(SOIL_MOISTURE_PIN);
Serial.print("Soil Moisture ");
Serial.println(soilMoisture);
int soilMoisturePercentage = map(soilMoisture, 400, 1023, 100, 0);
Serial.print("Soil Moisture Percentage ");
Serial.println(soilMoisturePercentage);
Blynk.virtualWrite(V5, soilMoisturePercentage);
if (isPumpOn || soilMoisturePercentage < THRESHOLD_MOISTURE)
{
digitalWrite(PUMP_PIN, HIGH);
if (!isPumpOn) {
Blynk.logEvent("moisture_alert","Soil moisture is below the threshold!");
Serial.println("Soil moisture is below the threshold!");
}
}
else
{
if (!isPumpOn) {
digitalWrite(PUMP_PIN, LOW);
}
}
}
BLYNK_WRITE(PUMP_SWITCH)
{
isPumpOn = param.asInt();
if (isPumpOn) {
Serial.println("Pump manually turned ON");
} else {
Serial.println("Pump manually turned OFF");
}
}
void setup()
{
Serial.begin(9600);
pinMode(PUMP_PIN, OUTPUT);
Blynk.begin(auth, ssid, pass);
timer.setInterval(3000L, sendSensorData);
Blynk.virtualWrite(PUMP_SWITCH, isPumpOn);
Blynk.syncVirtual(PUMP_SWITCH);
}
void loop()
{
Blynk.run();
timer.run();
}
-
1reduce your program to minimum required to turn on pump motor and turn off after one second ... does the pump stop?jsotola– jsotola07/25/2025 07:56:49Commented Jul 25 at 7:56
2 Answers 2
The BLINK_WRITE(Virtual_pin)
is supposed to be used for the Blynk app writes values to the Virtual_pin
, that is, to send a COMMAND from the Blynk app to control something, but you are not using it to control the PUMP_PIN
.
#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_TEMPLATE_NAME "***"
#define BLYNK_AUTH_TOKEN "***"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define SOIL_MOISTURE_PIN A0
#define THRESHOLD_MOISTURE 10
#define PUMP_PIN D1
#define SENSOR_STATE V5
#define PUMP_SWITCH V6
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "***";
char pass[] = "***";
BlynkTimer timer;
// sendSensorData is timer event callback to update the Blynk app the sensor data
void sendSensorData()
{
int soilMoisture = analogRead(SOIL_MOISTURE_PIN);
int soilMoisturePercentage = map(soilMoisture, 400, 1023, 100, 0);
Serial.printf("Soil Moisture Raw %d, Percentage %d%%\n", soilMoisture, soilMoisturePercentage);
Blynk.virtualWrite(SENSOR_STATE, soilMoisturePercentage);
if (soilMoisturePercentage < THRESHOLD_MOISTURE)
{
Blynk.logEvent("moisture_alert", "Soil moisture is below the threshold!");
Serial.println("Soil moisture is below the threshold!");
}
}
}
// This function get called every time when the Blynk app writes values to the Virtual Pin (PUMP_SWITCH)
BLYNK_WRITE(PUMP_SWITCH)
{
int pumpCmd = param.asInt();
digitalWrite(PUMP_PIN, pumpCmd);
Serial.printf("Pump manually turned %s\n", (pumpCmd)? "ON" : "OFF");
}
void setup()
{
Serial.begin(9600);
pinMode(PUMP_PIN, OUTPUT);
Blynk.begin(auth, ssid, pass);
timer.setInterval(3000L, sendSensorData);
}
void loop()
{
Blynk.run();
timer.run();
}
The pump keeps running likely due to calibration issues. Check the raw sensor values using Serial.println()
. Adjust the threshold value for accuracy. The sensor’s output may be too high. Ensure the value is mapped correctly for the moisture level.
Your code could be simplified for better control. Combine both conditions for the pump logic. This prevents conflicts between manual and automatic control. Adjust your logic to only run the pump when necessary.
The LM393D Soil Moisture Sensor Board can help. It provides reliable, calibrated moisture readings with digital output. This sensor eliminates noise and improves accuracy. It simplifies integration for better automation. Check it out here: https://www.pcbway.com/project/shareproject/Customized_LM393D_Soil_Moisture_Sensor_Board_4985ec99.html