[SOLVED] Plant moisture Sensor

This project is to serve as a reminder to water my plant by turning on the green LED when water is need. Then, turning on the red LED when there is enough water. The code complies and uploads, but on my blynk dashboard, using the graph widget, it only shows single digits. These reading are also wrong, as it reflect the same value whether the moisture sensor is in water or out. Please help!! below is my code.

#define BLYNK_PRINT Serial
#include "blynk/BlynkSimpleParticle.h"

int  greenLed = D4;
int redLed = D3;

int moisturePin = A0;
int thresHold = 0;

char auth[] = ""; // Put your Auth Token here. (see Step 3 above)


void setup() {
  Serial.begin(9600); // See the connection status in Serial Monitor
  delay(5000);
 
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(moisturePin, INPUT);

}

void loop() {

 Blynk.run(); // All the Blynk Magic happens here...

  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  int sensorValue = analogRead(moisturePin);
   //Blynk.virtualWrite(1, moisturePin);
  Serial.println(sensorValue);
  
  
  if (sensorValue > thresHold){
    digitalWrite(greenLed,HIGH);  
    digitalWrite(redLed, LOW);  
   }
  else{
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
  }
}

Please read here: http://docs.blynk.cc/#blynk-main-operations-limitations-and-recommendations

Also:

** Wrap your code by pressing this magic button (Backtick`) 3 times (before the code and after it):**

This makes your code beautiful with highlighted syntax, like this:

//comment goes here
void helloWorld() {
   String message =  "hello" + "world";
}

You can also select the code and press </> button in the top menu:

Sorry, but I didn’t understand what your problem is.

Dmitriy

My problem is that the reading from the moisture sensor is not accurate, and I don’t know why. I am getting the same reading whether the sensor is dry or wet. I am trying to turn on a green LED when the plant need water and the red LED when the plant has enough water. Thanks for your help.

So maybe problem in wiring? Is sensor readings are correct without Blynk? Sensor doesn’t have any libs it should work with?

Dmitriy,

Thanks for getting back to me. The sensor reads correct when i remove all of my variable from the body of the of the code. The only library i included is the #include “blynk/BlynkSimpleParticle.h”. Is there another one I should use? From what you have seen, does my code look correct?

I don’t know. It depends on sensor you are using.

int sensorValue = analogRead(moisturePin);
Serial.println(sensorValue);

Do you get here what you want? Is “int thresHold=0” correct threshold?

Dmitriy,

It is a cheap, simple moisture sensor. I do not think it requires a library. I have tried the threshold at 0 and at 500. I used 0 because I wanted to see if one of my LED would turn on depending on which condition was met. I don’t understand what’s going on. I truly appreciate your help.

Dmitriy,

Can you please give me an example of how you would write this code i am trying to implement with Blynk? I am really want to learn how to work with Blynk.

Hello Teem, first of all I think you have to change:

//Blynk.virtualWrite(1, moisturePin);
  Serial.println(sensorValue);

into:

Blynk.virtualWrite(1,sensorValue);
  Serial.println(sensorValue);

And I think it is better to insert a timer outside de void loop()

Use the library simple Timer by incuding

#include <SimpleTimer.h>

Give it a name

SimpleTimer tijd;

Make this timer active in the

void setup ()
tijd.setInterval(60000, schrijven); //  Here you set interval (1 min) and which function to call, in this case schrijven.

Write a function, in this case schrijven, where you put all the code wich should happen every time period.

And call everything in the

void  loop()

Blynk.run();
tijd.run();

I hope this helps, greetings André

Andre,

Thanks for your input. I have attempted to the best of my ability to implement your suggestion. Below you will find my attempt. I keep getting an error. Can you please help me?

//#include "blynk/blynk.h"
//#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial
#include "blynk/BlynkSimpleParticle.h"
#include <SimpleTimer.h>


int  greenLed = D4;
int redLed = D3;

int moisturePin = A0;
SimpleTimer timer;


char auth[] = ""; // Put your Auth Token here. (see Step 3 above)

void ledIndicator(){
    int sensorValue = moisturePin;
    
    if (sensorValue > thresHold){
    digitalWrite(greenLed,HIGH);  
    digitalWrite(redLed, LOW);  
    
   }
   
  else{
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
  }
}


void setup() {
  Serial.begin(9600); // See the connection status in Serial Monitor
  delay(5000);
 
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(moisturePin, INPUT);
  timer.setInterval(60000, ledIndicator);
  
 

}

void loop() {
    
  int thresHold = 1000;
    
  
  //Blynk.virtualWrite(1, sensorValue);
 // Serial.println(sensorValue);
  
 Blynk.run(); // All the Blynk Magic happens here...
 ledIndicator();
  
}

Particle uses different library for timers.
use SPARKCOREPOLLEDTIMER

Check their example,
Make it work without Blynk
Incorporate Blynk

Good luck :wink: and keep us posted!

Or… why just not search on our forum? :smile:

I appreciate the timely response. I have been searching the forum and found the example you sent me before. I don’t want to simply copy and paste someone else’s code. I want to understand my mistakes and learn from them. I could copied the example and make modifications, but I would learn anything. I just want to understand the mistakes I am making in my little project, so I can expand. I will fix the timer library mistake I made. Thanks again!

Hello, I’m very new with blynk,
I wonder if it is not a problem about the :
delay(5000);
Perhaps this other Blink topic is inspirating ?
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

1 Like

I also think the delay(5000);
might be the poblem

@Pixinit-Studios Please look at the date of last post (and in this case, also the post prior to that :wink: ) before answering.