0
\$\begingroup\$

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));
}
\$\endgroup\$
  • \$\begingroup\$ I don't see anything connected to the A0. \$\endgroup\$ – Eugene Sh. Jun 28 '17 at 18:37
  • \$\begingroup\$ It's connected to A0 of the Nodemcu board (behind the two diodes, the board itself is not shown in the schematic) \$\endgroup\$ – Lukas Jun 28 '17 at 18:40
  • \$\begingroup\$ Show the relevant schematic then. \$\endgroup\$ – Eugene Sh. Jun 28 '17 at 18:52
2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.