Electrical – Multiple analog inputs on A0 pin (ESP nodemcu) with PCF8574

#nodemcuexpanderi2cpins

I am trying to read out several analog values on one analog input on the nodemcu.

As sensors I am using two potentiometers which are connected to GND via a PCF8574 8-bit I/O expander.

With the code shown below I get for both sensors always the same value.So if I change the value of Poti 1, the value for Poti 2 (shown in serial monitor) will also change.

What am I doing wrong? Any help is appreciated.enter image description here

#include <Wire.h>
#define analogPin A0

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  measurePotis();
  delay(1000);
}

//Measure Soil Moisture
void measurePotis(){
  Wire.beginTransmission(0x027);
  Wire.write(0b01111111);
  Wire.endTransmission();

  int sensorValue;
  sensorValue = analogRead(analogPin);
  Serial.println("Poti 1: "+String(sensorValue));

  delay(1000);

  Wire.beginTransmission(0x027);
  Wire.write(0b11101111);
  Wire.endTransmission();

  sensorValue = analogRead(analogPin);
  Serial.println("Water Poti 2: "+String(sensorValue));
}

Best Answer

As clearly shown in the datasheet (see figure 2), the PCF8574 is a digital port expander. As you have found, this device has no capability to switch analog signals.

You will need an analog switch or multiplexer for this purpose.