Soil moisture giving false values when connected to 74hc4067 16 channel multiplexer

hi guys, I connected four soil moisture sensors to a 16-channel multiplexer (74hc4067). I’m not getting the raw values as I suppose to, Dry =1024 and wet = 744(my value). While writing this, i found out that BlynkEdgent.run(); is the one causing this.

#define BLYNK_TEMPLATE_ID "TMPL4HxPZsxa"
#define BLYNK_DEVICE_NAME "LED"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG


#include<DHT.h>
#include <ESP8266WiFi.h>

#include "BlynkEdgent.h"


#define DHTTYPE DHT22
#define DHTPIN 13 //D7

#define S0 D1                             
#define S1 D2                             
#define S2 D3
#define S3 D4  
#define SIG A0 

//#define pump1 15//D5


DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

int mois;
int mois1;
int mois2;
int mois3;

void setup()
{
 
  //pinMode(pump1, OUTPUT);
  Serial.begin(115200);
  BlynkEdgent.begin();
  
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(SIG, INPUT);
  
  
  
  dht.begin();
  
  timer.setInterval(100L,sensors);
  //timer.setInterval(100L,readSensor);
}

void loop() {

  digitalWrite(S0,LOW);
  digitalWrite(S1,LOW);
  int mois = analogRead(SIG);
  //mois = map(mois,744, 1024, 0, 100); //744 = wet; 1024= dry
  mois = map(mois,1024, 0, 0, 100);
  /*if (mois < 20)
  {
    digitalWrite(pump1, LOW); //on
  }
  else if(mois >85){
    digitalWrite(pump1,HIGH); //off
  }*/

  digitalWrite(S0,HIGH);  digitalWrite(S1,LOW);
  int mois1 = analogRead(SIG);
  //mois = map(mois,744, 1024, 0, 100); //744 = wet; 1024= dry
  mois1 = map(mois1,1024, 639, 0, 100);

  digitalWrite(S0, LOW); digitalWrite(S1,HIGH);
  int mois2 = analogRead(SIG);
  //mois = map(mois,744, 1024, 0, 100); //744 = wet; 1024= dry
  mois2 = map(mois2,1024, 639, 0, 100);

  digitalWrite(S0,HIGH); digitalWrite(S1,HIGH);
  int mois3 = analogRead(SIG);
  //mois = map(mois,744, 1024, 0, 100); //744 = wet; 1024= dry
  mois3 = map(mois3,1024, 639, 0, 100);

     Serial.print("Sensor 0 : ");Serial.println(mois);         
    Serial.print("Sensor 1 : ");Serial.println(mois1);          
    Serial.print("Sensor 2 : ");Serial.println(mois2);         
    Serial.print("Sensor 3 : ");Serial.println(mois3); 

    delay(2000);
    
    BlynkEdgent.run();
    timer.run();
     }

/*void readSensor(){
  
  Blynk.virtualWrite(V1,mois);
  Blynk.virtualWrite(V2,mois1);
  Blynk.virtualWrite(V3,mois2);
  Blynk.virtualWrite(V4,mois3);
  
}*/

void sensors(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();

    if (isnan (h) || isnan (t)) {
      Serial.println("Failed to read DHT sensor");
      return;
    }
    Blynk.virtualWrite(V6,h);
    Blynk.virtualWrite(V7,t);
    Blynk.virtualWrite(V1,mois);
    Blynk.virtualWrite(V2,mois1);
    Blynk.virtualWrite(V3,mois2);
    Blynk.virtualWrite(V4,mois3);
}

You’ve deleted all of the board types from the example sketch, rather than un-commenting one of them, so the custom board type will be in use.

That means you have a pin conflict caused by Settings.h, unless you’ve changed the default custom board type definition.

You should read the "Defining your physical switch and LED` section of this post…

Pete.

Hi pete, I just found out that the map function cpp mois1 = map(mois1,1024, 0, 0, 100); is the one giving false values or 0 value. Since i have 4 relays and 4 analog sensors, I don’t think I will have enough pins if I uncomment the board I’m using or maybe I’m wrong.

first of all, you should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

The sketch is using pins for Edgent, whether you like it or not.
Maybe you should re-read the info in the link.

Pete.

1 Like