Tow or more analog sensor with NodeMCU

Hi
I want to use two or more analog sensor in my project but NodeMcu has only one analog pin !
I can handle this with IC4051 multiplexer . but i am confused in coding.

#include <SPI.h>
#include <ESP8266WiFi.h> //ESP8266 WiFi connection library
#include <ThingerESP8266.h> [//THINGER.IO](https://thinger.io/) library
#include “DHT.h”
#define DHTPIN D4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <Servo.h>
Servo myservo;
// [Thinger.io](http://thinger.io/) connection parameters
#define user “ <strong><strong>"
#define device_Id "</strong></strong> ”
#define device_credentials “ <strong><strong>"
ThingerESP8266 thing(user, device_Id, device_credentials);
int r,s;
//Analog sensors ****************************************************************
void rain_sand(){
digitalWrite(D0,LOW);
digitalWrite(D1,LOW);
digitalWrite(D2,LOW);
r=analogRead(A0);
thing[“RAIN”] >> [](pson& out){
out = r;
};
delay(10);
digitalWrite(D0,HIGH);
digitalWrite(D1,LOW);
digitalWrite(D2,LOW);
s=analogRead(A0);
thing[“SAND”] >> [](pson& out){
out = s;
delay(10);
};
}
//*****************************************************************************
void setup() {
myservo.attach(D8);
pinMode(D3,INPUT);
pinMode(D5,OUTPUT);
pinMode(D6,OUTPUT);
pinMode(D7,OUTPUT);
thing.add_wifi("</strong></strong> ", “****”);
//servo--------------------------------------------------------------------------------
thing[“servo”] << [](pson& in){
if(in.is_empty()){
in = myservo.read();
}
else{
myservo.write(in);
}
};
//Relay 1-------------------------------------------------------------------------------
thing[“Relay_1”] << [](pson& in){
if(in.is_empty()){
in = (bool) digitalRead(D5);
}
else{
digitalWrite(D5, in ? HIGH : LOW);
}
};
//Relay 2---------------------------------------------------------------------------
thing[“Relay_2”] << [](pson& in){
if(in.is_empty()){
in = (bool) digitalRead(D6);
}
else{
digitalWrite(D6, in ? HIGH : LOW);
}
};
//Relay 3---------------------------------------------------------------------------
thing[“Relay_3”] << [](pson& in){
if(in.is_empty()){
in = (bool) digitalRead(D7);
}
else{
digitalWrite(D7, in ? HIGH : LOW);
}
};
//DHT-----------------------------------------------------------------------------------
thing[“dht11”] >> [](pson& out){
// Add the values and the corresponding code
out[“humidity”] = dht.readHumidity();
out[“celsius”] = dht.readTemperature();
};
//LDR-----------------------------------------------------------------------------------
thing[“LDR”] >> [](pson& out){
out = (bool) digitalRead(D3);

};
}
void loop() {
rain_sand();
thing.handle();
}

this is my code but don’t work correctly .Changing the value of a sensor causes the value of all sensors to change!! i put this viod to void setup but did’nt work :weary:

Hello @ali_ht97,

Unfortunately I have never worked with this multiplexer, however, the integration code seems to be well.
I strongly recommend you using an ESP32, that counts with a lot of analog pins, making your hardware and software much easier.

Hope it helps, best!

Tnx
I want to do this with an analog pin to overcome the nodemcu limitations .
this mutiplexer and function (void rain_sand) worked well in serial monitor. but i cant handle it with thinger .
so i’m working on it and share the good result with you soon :blush:

Hello @ali_ht97,

I was wrong telling you the code is fine, there’s actually a serious mistake in your code: rain_sand() routine cannot be called from the loop or must not contain the Rain and SAND resources. This situation is making your code to re-define these resources in each loop execution.

So put these resources in the setup and actualize de variables globally, then tell us the behavior of the system and we will try to continue from there.

Best

#include <SPI.h>
#include <ESP8266WiFi.h>       
#include <ThingerESP8266.h>   
#define user "***"
#define device_Id "***"
#define device_credentials "***"
ThingerESP8266 thing(user, device_Id, device_credentials);
int r,s;
void rain_sand(){
  digitalWrite(D5,LOW);
  digitalWrite(D6,LOW);
  digitalWrite(D7,LOW);
  r = analogRead(A0);
  delay(10);
   digitalWrite(D5,HIGH);
  digitalWrite(D6,LOW);
  digitalWrite(D7,LOW);
  s = analogRead(A0);
  delay(10); 
 }
void setup() {
 pinMode(D5,OUTPUT);
 pinMode(D6,OUTPUT);
 pinMode(D7,OUTPUT);
 thing.add_wifi("***", "***");
 rain_sand();
thing["RAIN"] >> [](pson& out){
  out = r;
  };
  
 thing["SAND"] >> [](pson& out){
 out = r;
  };
} 
void loop() {
  thing.handle();
}

If you mean something like the above code, unfortunately the result is still bad. I also wrote this code in other ways, but they all work the same. By changing a value, all values change.
Below is the code I wrote for display on the serial monitor that works correctly.


#include <SPI.h>
#include <ESP8266WiFi.h>
int sensor = A0;
int rain,sand;
void setup() {
 pinMode(D0,OUTPUT);
 pinMode(D1,OUTPUT);
 pinMode(D2,OUTPUT);
Serial.begin(9600);  
}

void loop() {
  digitalWrite(D0,LOW);
  digitalWrite(D1,LOW);
  digitalWrite(D2,LOW);
rain=analogRead(sensor)/10;
delay(10);
  digitalWrite(D0,HIGH);
  digitalWrite(D1,LOW);
  digitalWrite(D2,LOW);
sand=analogRead(sensor)/10;
delay(10);
Serial.print("rain :");
Serial.println(rain);
Serial.print("sand :");
Serial.println(sand);
delay(2000);
}
#include <SPI.h>
#include <ESP8266WiFi.h>       
#include <ThingerESP8266.h>   

#define user "***"
#define device_Id "***"
#define device_credentials "***"

ThingerESP8266 thing(user, device_Id, device_credentials);

int r,s;

void rain_sand(){
  digitalWrite(D5,LOW);
  digitalWrite(D6,LOW);
  digitalWrite(D7,LOW);
  r = analogRead(A0);
  delay(10);
  digitalWrite(D5,HIGH);
  digitalWrite(D6,LOW);
  digitalWrite(D7,LOW);
  s = analogRead(A0);
  delay(10); 
}

void setup() {
 pinMode(D5,OUTPUT);
 pinMode(D6,OUTPUT);
 pinMode(D7,OUTPUT);
 thing.add_wifi("***", "***");

thing["RAIN"] >> [](pson& out){
  out = r;
 };
  
 thing["SAND"] >> [](pson& out){
 out = r;
  };
} 

void loop() {
  thing.handle();
 rain_sand();
}