Problem with coding HC4051

Hi everyone, i hope i can get somehelp on how getting this to work.

I have multiple Sensors (32 of these) i want to read their values. I am using a master multiplexer HC4051 connected to 4 slave multiplexers, this is my wirings :

i tested it using one single multiplexer and it workes as intended, but when adding the slave i cannot getting it to work, i am not sure how to code it.

When using this code :

// 74XX4051 ADDRESS PINS :
#define  M_S0 2
#define  M_S1 3
#define  M_S2 4

#define  S_S0 7
#define  S_S1 6
#define  S_S2 5

// 74XX4051 ANALOG PIN :
#define  Z 0


void setup(){

        // CONFIGURE ADDRESS PINS
        pinMode(M_S0, OUTPUT);
        pinMode(M_S1, OUTPUT);
        pinMode(M_S2, OUTPUT);

        pinMode(S_S0, OUTPUT);
        pinMode(S_S1, OUTPUT);
        pinMode(S_S2, OUTPUT);

        // CONFIGURE SERIAL
        Serial.begin(57600);
}

void loop () {

int value;

        // LOOP THROUGH ALL THE ADDRESSES OF THE MASTER 
        for ( byte count = 0; count < 8 ; count++ ) {

                // SET THE ADDRESS
                digitalWrite(M_S0, bitRead(count, 0) );   
                digitalWrite(M_S1, bitRead(count, 1) );
                digitalWrite(M_S2, bitRead(count, 2) );  // => 000 - Y1-Z
                
                // LOOP THROUGH ALL THE ADDRESSES OF THE SLAVES
                for ( byte count_1 = 0; count_1 < 32 ; count_1++ ) {

                    digitalWrite(S_S0, bitRead(count_1, 0) );   // => 000,000,000,000 => Z1, Z2, Z3, Z4
                    digitalWrite(S_S1, bitRead(count_1, 1) );
                    digitalWrite(S_S2, bitRead(count_1, 2) );

                    // READ THE ANALOG VALUE
                    value = (bitRead(count_1, 2),bitRead(count_1, 1),bitRead(count_1, 0));

                }

                // READ THE ANALOG FOR THE MASTER ADRESSE
                value = analogRead(Z);

                // SERIAL OUTPUT
                // print : ### value
                Serial.print(bitRead(count, 2));
                Serial.print(bitRead(count, 1));
                Serial.print(bitRead(count, 0));
                Serial.print(' ');
                Serial.println(value);

                delay(100);
        }

}

I' am only getting random readings really (The reading LED for the moisture sensor does not switches on when the reading is done)
What i am doing wrong ? .
Any help will be appreciated.

Pin 6 should be connected to ground.

Also each layer only has 8 addresses not 32.

Are the pins connected according to the diagram or according to the defines in the sketch? The two don't match.

Why step through all 8 inputs on the master when you only have 4 slaves?

value = (bitRead(count_1, 2),bitRead(count_1, 1),bitRead(count_1, 0));

The commas don't paste the three bits together into a value. You probably meant:

  value = count_1 & 0x07;

You don't have the analogRead() inside the loop that loops through the slave inputs so you will only be reading input 7 of the first four slaves followed by the four unconnected inputs of the master.

If you want to display the first 32 inputs, try:

void loop () {

  int value;

  // LOOP THROUGH ALL THE ADDRESSES OF THE MASTER
  for ( byte count = 0; count < 32 ; count++ ) {

    // SET THE ADDRESS BITS
    digitalWrite(S_S0, bitRead(count, 0) );   // Slave A0
    digitalWrite(S_S1, bitRead(count, 1) );   // Slave A1
    digitalWrite(S_S2, bitRead(count, 2) );   // Slave A2

    digitalWrite(M_S0, bitRead(count, 3) );   // Master A0
    digitalWrite(M_S1, bitRead(count, 4) );   // Master A1
    digitalWrite(M_S2, bitRead(count, 5) );   // Master A2

    // READ THE ANALOG VALUE
    value = analogRead(Z);

    // SERIAL OUTPUT
    Serial.print(count, BIN);
    Serial.print('\t');
    Serial.println(value);
  }

  delay(3000);
}

Thanks for the answer, i still cant get it work.

I updated my code as suggested :

// 74XX4051 ADDRESS PINS :
#define  M_S0 7
#define  M_S1 6
#define  M_S2 5

#define  S_S0 2
#define  S_S1 3
#define  S_S2 4

// 74XX4051 ANALOG PIN :
#define  Z 0


void setup(){

        // CONFIGURE ADDRESS PINS
        pinMode(M_S0, OUTPUT);
        pinMode(M_S1, OUTPUT);
        pinMode(M_S2, OUTPUT);

        pinMode(S_S0, OUTPUT);
        pinMode(S_S1, OUTPUT);
        pinMode(S_S2, OUTPUT);

        // CONFIGURE SERIAL
        Serial.begin(57600);
}

void loop () {

  int value;

  // LOOP THROUGH ALL THE ADDRESSES
    for ( byte count = 0; count < 32 ; count++ ) {

    digitalWrite(M_S0, HIGH); //TURN THE SENSOR ON FOR READING
    digitalWrite(M_S1, HIGH);
    digitalWrite(M_S2, HIGH);
    digitalWrite(S_S0, HIGH);
    digitalWrite(S_S1, HIGH);
    digitalWrite(S_S2, HIGH);
    
    // SET THE ADDRESS BITS
    digitalWrite(S_S0, bitRead(count, 0) );   // Slave A0
    digitalWrite(S_S1, bitRead(count, 1) );   // Slave A1
    digitalWrite(S_S2, bitRead(count, 2) );   // Slave A2

    digitalWrite(M_S0, bitRead(count, 3) );   // Master A0
    digitalWrite(M_S1, bitRead(count, 4) );   // Master A1
    digitalWrite(M_S2, bitRead(count, 5) );   // Master A2
    delay(50);

    // READ THE ANALOG VALUE
    value = analogRead(Z);
    
    digitalWrite(M_S0, LOW); //TURN THE SENSOR OFF WHEN READING IS DONE
    digitalWrite(M_S1, LOW);
    digitalWrite(M_S2, LOW);
    digitalWrite(S_S0, LOW);
    digitalWrite(S_S1, LOW);
    digitalWrite(S_S2, LOW);
    
    // SERIAL OUTPUT
    Serial.print(count, BIN);
    Serial.print('\t');
    Serial.println(value);
  }

  delay(3000);
}

1- All my sensors are in the air, so i should expect a "0" reading. but i'am getting really random reading from the serial monitor every cycle ! (4 of my sensors are not connected to the multiplexer, does make any interferance ?)

2- I added

digitalWrite(M_S0, HIGH); ....... digitalWrite(M_S0, LOW);

to only power the sensor when taking a reading, but it doesnt work. When having my sensor into the water, the LED is lighten all the time.

stregoi:
1- All my sensors are in the air, so i should expect a "0" reading. but i'am getting really random reading from the serial monitor every cycle ! (4 of my sensors are not connected to the multiplexer, does make any interferance ?)

What are your sensors and how are they providing a low impedance path to ground (0V) when 'in the air'? It sound like you might mean 'unconnected' which would allow your inputs to float and thus give fairly random results. INPUTS THAT ARE NOT CONNECTED TO A CURRENT SOURCE OR CURRENT SINK ACT AS ANTENNAS THAT PICK UP NOISE.

Hi,

Can you please post a complete copy of your circuit, including pin numbers, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Thanks guys .. i am using a moisture sensor as this one :

So when not plugged into the soil, i am expecting a "0" value, when being in water "1023", but i am only getting random reading.

And this is my schematics

Hi,
Sorry the resolution of the schematic makes it impossible to read any writing or pin numbers.

Tom... :)p

So when not plugged into the soil, i am expecting a "0" value

So expect random values, because that is what you both should get and are getting.

I guess i found out what is my problem !!!
Unused MUX pins should be grounded .. i will try that and see how it turns !

I am uploading a better resolution : sketch here

I notice the total lack of any decoupling capacitors. That is just asking for troubled.

Grumpy_Mike:
I notice the total lack of any decoupling capacitors. That is just asking for troubled.

Well, you could have said that, and Helped a bit !! ... after all, I made this post because i needed help and coudnt figure it myself. Some of us are newbee's and not that smart at electronics as you are.

Well, you could have said that,

I thought I just had said it. This is the first time you posted a schematic with enough detail to treat it seriously.

and Helped a bit

Forget it a bit, you arrogant ****

Your sketch says:

#define  M_S0 7
#define  M_S1 6
#define  M_S2 5

#define  S_S0 2
#define  S_S1 3
#define  S_S2 4

Your schematic seems to show:
Pin 5 -> Master A (pin 11, S0, LSB)
Pin 6 -> Master B (pin 10, S1)
Pin 7 -> Master C (pin 9, S2, MSB)
Pin 8 -> Slave A (pin 11, S0, LSB)
Pin 9 -> Slave B (pin 10, S1)
Pin 10 -> Slave C (pin 9, S2, MSB)

The only pin that matches is Pin 6 -> Master B / Master pin 10 / Master S1 so they can't both be correct.