74HC4051 8:1 mux displaying the data.

hi all,
im working with 8 channels mux with arduino, the 8 channels are supposed to connect to 8 sensors.
my question is, i want a trick such that when only one sensor is connected i read and display one sensor data.
i dont want to display somthing like:

Sensor 1:2v Sensor 2: 0v Sensor 3:0v Sensor 4:0v Sensor 5:0v Sensor 6:0v Sensor 7:0v Sensor 8:0v;

i want just: Sensor 1:2v thats all.

when 4 i read and display the 4 sensors ( without reading the unconnected channels) and when i connect 8 sensors i read and display the 8.

is there any trick to do this?
i thought about sometrick to detect eather the sensor is connected or not using a pulldown/up resistor but in my case this is not a solution because 0v could be a reading value not just and empty value. also i could not compare to "0" because of the same reason.
any help please? thanks in advance.

Hi,
What code do you have now?
What are display are you using?

Thanks.. Tom.. :slight_smile:

@TomGeorge thanks for the reply.
the code is a simple standard mux code.

int pin_Out_S0 = 2;
int pin_Out_S1 = 4;
int pin_Out_S1 = 5;

float pin_In_Mux1 = 34;
float Mux1_State[8] = {0};

void setup() {
  pinMode(pin_Out_S0, OUTPUT);
  pinMode(pin_Out_S1, OUTPUT);
  pinMode(pin_Out_S2, OUTPUT);
  pinMode(pin_In_Mux1, INPUT);
  Serial.begin(115200);
}

void loop() {
  updateMux1();

  
  String p=" ; ";
      
String  msg=String(Mux1_State[0]) + p + String(Mux1_State[1]) + p + String(Mux1_State[2]) + p + String(Mux1_State[3]);


Serial.println(msg);


  delay(1000);
}

void updateMux1 () {
  for (int i = 0; i < 4; i++){
    digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
    digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
    digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
    Mux1_State[i] = analogRead(pin_In_Mux1)* (3.300 / 4095.00);

  }
}

I can think of one, but not sure if would work. Might only work if your sensors are, for example, LDR or thermistors, with a known fixed resistor to form a voltage divider. Where the sensor is missing, the fixed resistor should still need to be present, pulling the mux input to Vcc (not ground). The technique would be to take two readings on each channel. One with the internal pull-up enabled and one without. Reading made with the internal pull-up would not be very accurate, since the internal pull-ups vary from chip to chip, with temperature, supply voltage etc. So you would record the value made without the pull-up enabled. But if the sensor is present, the two readings would be different. If the sensor is missing, the two readings would be the same, or both very close to Vcc.

@PaulRB so i connect each sensor to 2 pins? or how can i enable the internal pullup?

Hi,
What display are you using?
What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

@TomGeorge for now just serial displaying. but im will use wifi or somthing similar.
model: esp32 programmed with arduino IDE.

in my case this is not a solution because 0v could be a reading value

For many analog sensors, such as LDR and thermistors, 0V will not be a reading. This type of sensor never has zero or infinite resistance. If we suppose an LDR has 10R resistance in bright sunlight and 10M resistance in complete darkness, and we use a fixed 10K resistor to make a voltage divider, then the output voltage would be 10/(10000+10)*5=0.005V in bright light and 10000000/(10000000+10000)*5=4.995V in darkness. The ADC readings would never be below 5 or above 1020, unless the sensor was not connected. So that might work.

For any type of sensor where the resistance could be exactly zero, you could place a low value resistor in series with the sensor so that zero can never be a reading unless the sensor is removed.

But why do you need a mux with esp32? Does not not have 8 analog inputs?

Hi,
Can you please post a schematic?

The ESP32 is 3.3v, and its inputs are not 5V tolerant.
What do you have as the supply voltage for the 4051?

Tom.... :slight_smile:

@PaulRB thanks for the idea, i need mux because the other pins are busy thats all.
@TomGeorge im suppling the 4051 with 3.3v ( the on resistance will be higher compared to 5v)

PaulRB:
But why do you need a mux with esp32? Does not not have 8 analog inputs?

I think it depends on the board you purchase as to how many ADC pins you have.
Basically an ESP32 has available.

Analog to Digital Converter (ADC)
The ESP32 has 18 x 12 bits ADC input channels (while the ESP8266 only has 1x 10 bits ADC). These are the GPIOs that can be used as ADC and respective channels:

  • ADC1_CH0 (GPIO 36)
  • ADC1_CH1 (GPIO 37)
  • ADC1_CH2 (GPIO 38)
  • ADC1_CH3 (GPIO 39)
  • ADC1_CH4 (GPIO 32)
  • ADC1_CH5 (GPIO 33)
  • ADC1_CH6 (GPIO 34)
  • ADC1_CH7 (GPIO 35)
  • ADC2_CH0 (GPIO 4)
  • ADC2_CH1 (GPIO 0)
  • ADC2_CH2 (GPIO 2)
  • ADC2_CH3 (GPIO 15)
  • ADC2_CH4 (GPIO 13)
  • ADC2_CH5 (GPIO 12)
  • ADC2_CH6 (GPIO 14)
  • ADC2_CH7 (GPIO 27)
  • ADC2_CH8 (GPIO 25)
  • ADC2_CH9 (GPIO 26)

Tom.. :slight_smile:

yes mr@TomGeorge
i said that the other pins are used. i need a mux this is a must.
my qst is how to read a value only when a sensor is connected.
thanks

Hi,
The @PaulRP suggestion is probably the best.

Tailoring the sensor output between two limits and if you detect an output outside those limits then the sensor must be absent.

Tom... :slight_smile:

one more question Mr @TomGeorge
if i supply the 4051 with external 5v to minimize to Ron, and i connect the grounds together.
this wont cause a problem right ?

also i need external 5 because some sensors operate at 5v. of course ill divide their output.

Hi,
Connecting gnds together is essential for your control of the 4051 by the ESP32.
So gnds connected is good.

Tom... :slight_smile:

@PaulRB @TomGeorge
what if my sensor is just a voltage divider. for example a PV panel voltage. somtimes its really 0 (at night).

(this img is just an example not mine)

hello

So you want a way to tell if a pv panel is connected or not connected at, night? Why is that important?