I have a relay that should be switched off when the analog value is too high by the Arduino code:
int digitalPin = 4; // for the relay
int analogPin = A0; // for the soil moisture sensor
int digitalVal; // digital readings
int analogVal; // analog readings
void setup()
{
pinMode(digitalPin, OUTPUT);
digitalWrite(digitalPin, LOW);
Serial.begin(9600);
}
void loop()
{
analogVal = analogRead(analogPin); // read the value returned by the soil moisture sensor
if (analogVal < 400) { // if the soil moisture sensor returns a value < 400
digitalWrite(digitalPin, HIGH); // The water pump waters the plant
} else {
digitalWrite(digitalPin, LOW); // The water pump stops watering
}
Serial.println(analogVal);
delay(100);
}
But, as you can see on the video, despite going above the 400 threshold, having the relay pin switching off, it continues to pump.
Here is my architecture:
I just changed the relay to an Elegoo 4:
I am following this tutorial.


isn't it what I did?... no, the wiring diagram shows otherwise – jsotola Jun 22 '23 at 19:33you mean that the green and the light blue wires should be the same?... no ... your code shows the control pin to be pin 4 ... your wiring diagram show a connection at pin 2 – jsotola Jun 22 '23 at 22:43