Hello, I'm new to using microcontrollers with relays. I purchased a kit on Amazon -
https://www.amazon.com/dp/B09BMVS366?psc=1&ref=ppx_yo2ov_dt_b_product_details - that I want to control with an ESP8266 (specifically NodeMCU Amica / ESP8266MOD) following these instructions:
https://poanchen.github.io/blog/202...system-that-is-connected-to-azure-iot-central. I am able to upload the software to the ESP8266, monitor the serial port to see the capacitive moisture sensor's reading and to calibrate the software to trigger the relay, which controls the pump. However, while both the software (via the serial monitor) and the relay (via the indicator lights) says I am turning the pump on and off, the relay stays on. I can get it to turn off by pulling the "In" pin or the VCC but it won't stop as it is supposed to do. I am using the "Vin" pin to power the relay, which measures 4.6V. I also tried powering it with the 3.3V pin from the 8266 and both result in the aforementioned situation.
Here's the code I'm using:
const int AirValue = 733; // you might need to calibrate this number, instruction is down below
const int WaterValue = 355; // you might need to calibrate this number, instruction is down below
const int DrySoilMoisturePercentage = 50;
const int SoilMoisturePin = A0;
const int RelayPin = D2;
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
void setup() {
Serial.begin(9600);
pinMode(SoilMoisturePin, INPUT);
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, HIGH);
}
void loop() {
soilMoistureValue = analogRead(SoilMoisturePin);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
Serial.print(soilmoisturepercent);
Serial.println("%");
if (soilmoisturepercent <= DrySoilMoisturePercentage) {
Serial.println("Water pumps running...");
digitalWrite(RelayPin, LOW);
delay(1000);
}
if (soilmoisturepercent >= DrySoilMoisturePercentage) {
Serial.println("Water pumps stopped...");
digitalWrite(RelayPin, HIGH);
delay(1000);
}
}