ESP8266 Auto Watering Circuit-low level trigger relay will turn on but not off - Page 1
Forum section: electronics / beginners

author: LEDfoot
author_rank: Contributor
timestamp: October 13, 2022, 12:56:32 am
content: 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 belowconst int WaterValue = 355; // you might need to calibrate this number, instruction is down belowconst 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);}}

author: james_s
author_rank: Super Contributor
timestamp: October 13, 2022, 01:07:19 am
content: I think the first thing you should do is determine whether the problem is hardware or software. What happens if you connect an LED (and resistor) between the IO pin you're using and ground? Does it turn on and off as expected under software control?It's been a while since I've worked with the ESP8266 but I recall most/all of the IO pins have multiple uses and not all have exactly the same capabilities. Some boards also have things like onboard indicator LEDs wired to one or more pins which can interfere with other uses.

author: floobydust
author_rank: Super Contributor
timestamp: October 13, 2022, 02:47:04 am
content: The relay module board requires 5V power for the relay coil, I would not expect it to work with only 3.3V power. Using USB power, the relay coil needs 89mA so that is not enough left to power the ESP8266 or valve. The relay signal input side should be OK with 3.3V drive from the ESP8266 and a logic "1" should turn on the relay. These low cost chinese boards are convenient but have no decent specs.Your code should use an "else" for the soilmoisture evaluation, as it will flip out on/off/on/off... if it equals 50. I thought the relay driver transistor is on with 3.3V driving it?

author: LEDfoot
author_rank: Contributor
timestamp: October 17, 2022, 10:59:30 pm
content: Thanks for your reply. For reasons I cannot explain, the problem seems to have gone away without my changing anything about the circuit or the software. I'm in the process of assembling the final wiring to make the design more durable and will see if the magical fix persists.

author: LEDfoot
author_rank: Contributor
timestamp: October 17, 2022, 11:04:13 pm
content: Quote from: floobydust on October 13, 2022, 02:47:04 amThanks for your insightful comments. I know my USB power supply says 2.1A. Is the ESP8266 actually using enough power such that fewer than 89mA remain to energize the coil?You're right about the "no decent specs". I found a datasheet for the relay on the board but not the whole board design.Thanks for noticing the conflicting logic at the 50% data point. I have fixed it.As for whether the relay driver transistor is on with 3.3V, I know that if I pull the control pin from the relay board, the pump shuts off if it is in the on position.

author: james_s
author_rank: Super Contributor
timestamp: October 18, 2022, 12:21:41 am
content: Quote from: LEDfoot on October 17, 2022, 11:04:13 pmThat should be fine, as long as your USB cable is sufficient, some are made with very thin cheap wires. As I recall, the ESP8266 can draw significant current for brief pulses but the average power draw is pretty low.

author: floobydust
author_rank: Super Contributor
timestamp: October 18, 2022, 01:05:16 am
content: You might be using the wrong contacts on the relay, I see some versions of the board have NC at the top.edit: OP cross-posted over herehttps://forum.allaboutcircuits.com/threads/esp8266-automatic-watering-circuit-low-level-trigger-relay-will-turn-on-but-wont-turn-off.189686/