I'm having a problem trying to run a bunch of code, using an "if" state which uses a variable which should get a value from a soil moisture sensor.

I'm using the sensor digitally, so it should just sense a value between the 2 discrete "0" and "1". However, even if I think my code is ok, it simply doesn't work, as the value is always "0"(default value is 0 for the global variable "sms" i use). The code I'm posting below is without an "else" state that is following the first "if". But my problem is inside the first "if" state. Please note that jumper wires are ok and also I've tried 3 different soil moisture sensors, two of which were brand new. Any ideas on what's wrong?

//Start of program

char auth[] = "**************";
char ssid[] = "88888";
char pass[] = "99999999";

int Relay1 = D1;
int Relay2 = D2;
int val2;
float val3;
int sensor_pin = D7;
int pinValue;
SimpleTimer timer;
elapsedMillis timeElapsed;

void setup()
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Blynk.begin(auth, ssid, pass);
  pinMode(Relay1, OUTPUT); //Setup Relay pin as output pin
  pinMode(Relay2, OUTPUT); //Setup Relay pin as output pin
  pinMode(sensor_pin, INPUT);
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);

  Blynk.virtualWrite(8, level);

  pinValue = param.asInt();

void loop()
  if (pinValue == 0)
    some code here
    if ( sms == 0)
      if (val2 <= 14)
        code here          }
        digitalWrite(Relay2, LOW); // Pump2 ON

    else //(sms == 1)
      some code here

TE215 Moisture Sensor

  • 1
    I mean physical wires which were used to connect sensor to arduino. – Galinho Salonica Jan 29 '18 at 3:42
  • 1
    Code was reduced. – Galinho Salonica Jan 29 '18 at 3:42
  • 1
    My mistake. I just erased it in order to reduce code. Now you can see where sms gets a value. – Galinho Salonica Jan 29 '18 at 10:15
  • 1
    What voltage does the sensor put out? If you measure the D7 pin with a meter, is it ever above the 3 volt logic HIGH threshold to be interpreted as a 1? – Dave X Jan 29 '18 at 10:58
  • 1
    I would put the sensor in sum very wet soil and have some code like this loop(){Serial.println(digitalRead(sensor_pin));delay(500);} and see what it says. It might be that you have to tweak the pot on the board to set the sensitivity and it only goes high when it passes a certain value. – Code Gorilla Jan 29 '18 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.