x4 Soil Moisture + DHT11 with WI-FI / 2560

Hey guys , i'm new to this and just received my mega 2560 R3 with some gadgets and until now i was fine with all this tutorials online and everything works fine except soil moisture :) The problem is this :

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
   Serial.begin(9600);  
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  delay(1000);          
  Serial.print("sensor = " );                       
  Serial.println(sensorValue);                   
}

This is my basic code using for this test and the results are: SENSOR - 1023 ? Should be in air 0 i guess and in water up to 900 but ... 1023 , when i put it in water its going down about 280 - 300. Any ideas whats wrong :) My second problem its how to attach 4 soil moisture sensors in one board + dht sensor + wifi shield :D thanks and sorry for my english :D also the other thing i was wondering its should i delete every time when i upload something on the board and how to do it :)

The very cheap soil moisture sensors use two pieces of metal and measure resistance between them. Often one of the metal is connected to 5V. That is a very inaccurate way to measure the soil moisture, because of electrolysis. I use two pieces of metal tape, and measure the resistance in both directions, and I average the result. Even better is to use a capacitive soil moisture sensor : http://wemakethings.net/chirp/

When you want to connect all of that to the Arduino Mega 2560 board, then you start with the wifi shield, and check the reference which pins are used. The Mega has enough pins en enough memory, so you could add many more DHT sensors and soil moisture sensors.

When you upload a new sketch, the previous sketch is automatically deleted/overwritten. There is no need to delete something.

Very helpful thans so mutch, yesterday i put the sensor in soil to see how is it working and today the sensor was already with some corrosion on it :@ ill read little bit more about all this Arduino. I was thinking for sensor shield as well but don't have actual plan atm

I started to measure the soil conductivity in both directions after reading this : http://gardenbot.org/howTo/soilMoisture/ This also uses both directions : http://www.seeedstudio.com/recipe/103-arduino-cheap-soil-moisture-system-without-corrosion.html

For the probes, I'm using Copper Nickel adhesive tape. It should not corrode (I hope). It was very cheap on Ebay a while ago, but it has become expensive: http://www.ebay.com/itm/121739046153

Ok well done cheap and working. I was searching online for any high quality sensors but null... only ready to use gadgets to measure the soil :) don't need and so expensive as well. Any way i was gonna ask u , how can i be sure the new build sensor will show correct , how to adjust ?

The capacitive sensor that I mentioned is more accurate.
But using two metal probes will allways be inaccurate. The number will not be the humidity but more a rough indication. The resistance/conductivity of the soil depends on the water, the nutrients and the temperature.
I used very wet soil and used that as 100% and very dry soil as 0%.

An other option is to use en electronic humidity sensor and put that in the soil : https://www.adafruit.com/products/1298

Well i don't need very accurate as u say: Very wet soil mean 100% and very dry soil mean 0% temperature isn't so important :) would you share with me code for soil measurment in 100% please :)

May be something like that

int soil=0;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  sensorValue = constrain(sensorValue, 485, 1023);
  // print out the value you read:
  //Serial.println(sensorValue);

  //map the value to a percentage
  soil = map(sensorValue, 485, 1023, 100, 0);
  
  // print out the soil water percentage you calculated:
  Serial.print(soil);
  Serial.println("%");


  delay(1000);        // delay in between reads for stability
}

But i have problem with mixing two codes to work together with DHT11 Test Code and this one :@

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN A1     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("B0lero BlackB0x Grow Monitoring");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}

I try many times but always error

Finaly :smiley:

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
int soil=0;
#include "DHT.h"

#define DHTPIN A1    


#define DHTTYPE DHT11   // DHT 11
 
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("B0lero Monitoring System");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

 // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  sensorValue = constrain(sensorValue, 485, 1023);
  //map the value to a percentage
  soil = map(sensorValue, 485, 1023, 100, 0);
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C\t\t ");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C\t\t ");
  Serial.print("Soil Humidity: ");
  Serial.print(soil);
  Serial.println("%");


  delay(2000);        // delay in between reads for stability

}

:smiley:

My sketch is only useful with the hardware. I have a few resistors of 10k and capacitors of 1nF.
I plan to make a tutorial of it, with photos of the metal tape that I use, but I don’t know when I will do that. Someday…

As you can see, I change the voltage of the two metal probes, and measure it 5 times.

  // Set 'A' and 'B' as output (and low).
  pinMode( pinSoilA, OUTPUT);
  pinMode( pinSoilB[soil], OUTPUT);

  // Alternate output pins, and read analog input.
  unsigned int totalA = 0;
  unsigned int totalB = 0;
  const int n = 5;                          // avarage of 5, maximaal 60 will fit in unsigned int
  for( int i=0; i<n; i++)
  {
    // 'A' side high and 'B' side low
    digitalWrite( pinSoilB[soil], LOW);
    digitalWrite( pinSoilA, HIGH);
    delayMicroseconds( 100);                 // 100us together with rest of code is enough for capacitor of 1nF
    totalA += 1023 - analogRead( pinSoilRead);  // upside down value, to compare it with totalB.

    // 'B' side high and 'A' side low
    digitalWrite( pinSoilA, LOW);
    digitalWrite( pinSoilB[soil], HIGH);
    delayMicroseconds( 100);
    totalB += analogRead( pinSoilRead);
  }
  digitalWrite( pinSoilB[soil], LOW);       // make everything low.
  delayMicroseconds( 300);                  // be sure to remove all charge in capacitors, no electrolysis wanted.
  
  // release all pins
  pinMode( pinSoilA, INPUT);
  pinMode( pinSoilB[soil], INPUT);

I’m glad you have a working sketch. I looks good. Just remember that the probes may corrode or the values might change due to electrolysis.