0

I've connected four YL-69 sensors to my arduino at-mega2560 on analogs pins 0-3. This is combined with four DHT11 sensors that are in digital pins.

The DHT work fine, and I can work one YL-69 moisture sensor that gives correct readings. But when I connected the others, the readings are just 100% for all. I'm sure I must have made a mistake in my code somewhere, just can't spot it.

I've posted the sketch I use below, and the results from serial monitor. (BTW i added a short delay of 10ms between each reading thinking that may be the issue, but it had no effect).

Serial Monitor:

Serial Monitor

Sketch:

#include "DHT.h"
#include <SPI.h>        // Include SPI library (needed for the SD card)
#include <SD.h>         // Include SD library

File dataFile;

#define DHTPIN0 10     // what pin we're connected to
#define DHTPIN1 11     // what pin we're connected to
#define DHTPIN2 12     // what pin we're connected to
#define DHTPIN3 13     // what pin we're connected to


// Uncomment whatever type you're using!

#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// How to connect sensors
// Connect pin 1 (on the left) of the sensor to +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

DHT dht0(DHTPIN0, DHTTYPE);
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);

int output_value ;
int output_value1 ;
int output_value2 ;
int output_value3 ;
int output_value4 ;

unsigned long time;

void setup() {
 Serial.begin(9600); 
 while (!Serial)
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
 Serial.println("DHTxx test!");
 dht0.begin();
 dht1.begin();
 dht2.begin();
 dht3.begin();
 delay(2000);
}

uint16_t line = 1;

void loop() {
 // Reading temperature or humidity takes about 250 milliseconds!
 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
 float h0 = dht0.readHumidity();
 float h1 = dht1.readHumidity();
 float h2 = dht2.readHumidity();
 float h3 = dht3.readHumidity();
 float t0 = dht0.readTemperature();
 float t1 = dht1.readTemperature();
 float t2 = dht2.readTemperature();
 float t3 = dht3.readTemperature();

 dataFile = SD.open("DHT11Log.csv", FILE_WRITE);
 //  if any result below is NaN (not a number) then something went wrong!
 if (dataFile) {
   output_value= analogRead(A0);
   delay(10);
   output_value1= analogRead(A1);
   delay(10);
   output_value2= analogRead(A2);
   delay(10);
   output_value3= analogRead(A3);
   delay(10);
   output_value4= analogRead(A4);
   output_value = map(output_value,1024,0,0,100);
   output_value1 = map(output_value,1024,0,0,100);
   output_value2 = map(output_value,1024,0,0,100);
   output_value3 = map(output_value,1024,0,0,100);
   output_value4 = map(output_value,1024,0,0,100);
   Serial.print("Moisture : ");
   Serial.print(output_value);
   Serial.println("%");
   Serial.print(output_value1);
   Serial.println("%");
   Serial.print(output_value2);
   Serial.println("%");
   Serial.print(output_value3);
   Serial.println("%");
   Serial.print(output_value4);
   Serial.println("%");
   Serial.print("Humidity  : sensor 0, "); 
   Serial.println(h0);
   Serial.print("  sensor 1, "); 
   Serial.println(h1);
   Serial.print("  sensor 2, "); 
   Serial.println(h2);
   Serial.print("  sensor 3, "); 
   Serial.println(h3);
   Serial.println(" %\t");
   Serial.print("temperature  : sensor 0, "); 
   Serial.println(t0);
   Serial.print("  sensor 1, "); 
   Serial.println(t1);
   Serial.print("  sensor 2, "); 
   Serial.println(t2);
   Serial.print("  sensor 3, "); 
   Serial.println(t3);
   Serial.println(" %\t");
   dataFile.print(line++);
   dataFile.print(":    Temperature = ");
   dataFile.print(",");
   dataFile.print(t0);
   dataFile.print(",");
   dataFile.print(t1);
   dataFile.print(",");
   dataFile.print(t2);
   dataFile.print(",");
   dataFile.print(t3);
   dataFile.print(",");
   dataFile.print("°C,    Humidity = ");
   dataFile.print(",");
   dataFile.print(h0);
   dataFile.print(",");
   dataFile.print(h1);
   dataFile.print(",");
   dataFile.print(h2);
   dataFile.print(",");
   dataFile.print(h3);
   dataFile.print(",");
   dataFile.println("%");
   dataFile.print(",");
   dataFile.print("Moisture : ");
   dataFile.print(",");
   dataFile.print(output_value);
   dataFile.print(",");
   dataFile.print(output_value1);
   dataFile.print(",");
   dataFile.print(output_value2);
   dataFile.print(",");
   dataFile.print(output_value3);
   dataFile.print(",");
   dataFile.print(output_value4);
   dataFile.close();

  }
  // if the file didn't open, print an error:
  else
    Serial.println("error opening DHT11Log.txt");
    delay(10000);
}
1
  • Those YL-69 moisture "sensors" are not reliable at all. There is a voltage on the metals in the soil. They will maybe work for hours or days or weeks, and then you have to look for something better.
    – Jot
    Commented Jan 18, 2019 at 10:20

1 Answer 1

2

I think that the problem is here:

output_value = map(output_value,1024,0,0,100);
output_value1 = map(output_value,1024,0,0,100);
output_value2 = map(output_value,1024,0,0,100);
output_value3 = map(output_value,1024,0,0,100);
output_value4 = map(output_value,1024,0,0,100);

replace them with:

output_value = map(output_value,1024,0,0,100);
output_value1 = map(output_value1,1024,0,0,100);
output_value2 = map(output_value2,1024,0,0,100);
output_value3 = map(output_value3,1024,0,0,100);
output_value4 = map(output_value4,1024,0,0,100);
3
  • Thank you so much! I didn't spot that at all, I will update my sketch tomorrow, i'll post back here if it all works ok. Thanks again
    – Paddy
    Commented Jan 18, 2019 at 0:45
  • @Paddy You’re welcome! Please, If I helped you, remember to mark question as resolved. :)
    – leoc7
    Commented Jan 18, 2019 at 7:10
  • Tested it there, and it works a treat. I'll now mark this as solved. Cheers!
    – Paddy
    Commented Jan 18, 2019 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.