Using a HC4067 for 2 analogs read

hey, it s my 1st time using a multiplexer and i m having isues with understanding clearly how it works
I m working with an esp8266 who only has 1analog pin and i need to have 2analog reads so I chose an HC4067 (1to16 multiplexer) to be able to do both reads
Beacause I m using only 1 bit I only need to connect S0 to my board and have connected S1,s2,S3 to ground for it to stay 0
But my reads are really weird and constant I don t really understand
Here is thje portion of code I m curently working on but isnt working:

#define AOUT_PIN A0   // Broche commune du multiplexeur
#define SELECT_PIN D1 // Sélectionne le capteur (S0 du multiplexeur)
#define POWER_PIN  D7 // Alimentation du capteur de niveau d'eau

void setup() {
  pinMode(SELECT_PIN, OUTPUT);
  pinMode(POWER_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  float soilMoisture, waterLevel;
  
  // read soil humidity(Canal 0)
  digitalWrite(SELECT_PIN, LOW);  // select C0
  delay(100);
  float anasm = analogRead(AOUT_PIN);
  soilMoisture = ((1024 - anasm) / 1024) * 100; //transform read in %
  
  // read water level (Canal 1)
  digitalWrite(SELECT_PIN, HIGH); // Sélectionne C1
  digitalWrite(POWER_PIN, HIGH);
  delay(100);
  waterLevel = analogRead(AOUT_PIN);
  digitalWrite(POWER_PIN, LOW);

  // Affichage des valeurs
  Serial.print("Soil Humidity : "); Serial.print(soilMoisture); Serial.println(" %");
  Serial.print("water level : "); Serial.println(waterLevel);

  delay(1000);
}

and the reads :
15:10:15.213 -> Soil Humidity : 99.02 %

15:10:15.213 -> water level : 1024.00

15:10:16.414 -> Soil Humidity : 99.02 %

15:10:16.414 -> water level : 1024.00

15:10:17.614 -> Soil Humidity : 99.02 %

15:10:17.614 -> water level : 1024.00

Are you powering the 4067 with 5V or 3.3V?
Does your ESP8266 board have a voltage divider on the analog input?
What is the voltage range you are trying to measure?

Does you code work correctly if you don't use the 4067?

1 Like

Post an annotated schematic showing exactly how you have wired this. While doing this it will answer @jim-p questions and what I need to know.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.