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.

#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;
Serial.println("Poti 1: "+String(sensorValue));

delay(1000);

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

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

• I don't see anything connected to the A0. – Eugene Sh. Jun 28 '17 at 18:37
• It's connected to A0 of the Nodemcu board (behind the two diodes, the board itself is not shown in the schematic) – Lukas Jun 28 '17 at 18:40
• Show the relevant schematic then. – Eugene Sh. Jun 28 '17 at 18:52

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.

I think you have mixed up MS bit with LS bit!

When you send a byte to the IO expander, the MS bit of the byte you send corresponds to P7 and not to P0 as you probably think.

So when you send 0b01111111, you set P7 to LOW and all others to HIGH and when you send 0b11101111, you set P4 to LOW and all others to HIGH.

You should instead use 0b11111110 and 0b11110111 for potentiometer 1 and 2, respectively.

• Thanks, you are right, I did it the wrong way. But unfortunately the signals are still mixing up. – Lukas Jul 3 '17 at 21:00