Arduino and Mux CD74HC4067

Hello,

I have a connection setup involving - CD74HC4067 , Nano and IR proximity Sensors

C0 to C15 are connected to each of my IR sensor

When I test the output, it is giving values other than my expectation. My expectation is to clearly identify, which sensor became active. [ I think I am getting distance value like 344/345/346 etc], But I want active sensor i.e. sensor whose digitalread is 1.

Please can you help me with code.
The code I used is below - I dont know anything below, just trying

#define PIN_D_MUX_S0 8  // bit 7 of PORTB  
#define PIN_D_MUX_S1 9  // bit 6 of PORTB
#define PIN_D_MUX_S2 10 // bit 5 of PORTB
#define PIN_D_MUX_S3 11 // bit 4 of PORTB
#define PIN_A_MUX_SIG 3 // This pin will read the input from the mux. 

....setup

 pinMode(PIN_D_MUX_S0,   OUTPUT);
  pinMode(PIN_D_MUX_S1,   OUTPUT);
  pinMode(PIN_D_MUX_S2,   OUTPUT);
  pinMode(PIN_D_MUX_S3,   OUTPUT);

....loop


 for (byte i=0; i<MUX_CH_COUNT; i++) {
    PORTB = (PORTB & B11110000) | i;
    short val = analogRead(PIN_A_MUX_SIG);
    // "val" holds the value for input "i", so you can insert your custom code here.
    
    // Print the values...
    Serial.print(i);    
    Serial.print(": ");
    Serial.print(val);
    Serial.println(" | "); 
}

Are the sensors analog or digital?

It is - IR Infrared Obstacle Avoidance Sensor Module

Before introducing Mux, I have used most of the Nano pins and detected active IR sensor as below

//#define SENSOR2 3
....

 if(digitalRead(SENSOR2)==0){
    ...
  }
  else{
    ...
  }

So I think it is digital.

Then why are you doing an analog read?

My earlier setup was - IR out pin connected to Nano pins

But I want to use multiplexer and use only 1 pin of Arduino Nano.

Mux sig is connected to Nano . I want to see, if I can get the IR that became active

I hope it is possible.

You won't answer my question? Do you know the difference between an analog and digital sensor?

I know this happens in forums. i.e. the kind of counter questions, even when known details are provided , and can be interpreted

I now digged information from ali express

OUT board digital output interface (0 and 1)

Digital is the answer

hope this will satisfy your demand

unbelievable that, code also doesn't give you the answer

#define SENSOR2 3
....

 if(digitalRead(SENSOR2)==0){
    ...
  }
  else{
    ...
  }

Speaking of code, did you ever post your entire sketch? At the moment, I can only see some snippets. That's not enough to troubleshoot.

I saw some analog stuff in here, that is why I asked if digital or analog:

#define PIN_A_MUX_SIG 3 // This pin will read the input from the mux.

....setup

 pinMode(PIN_D_MUX_S0,   OUTPUT);
  pinMode(PIN_D_MUX_S1,   OUTPUT);
  pinMode(PIN_D_MUX_S2,   OUTPUT);
  pinMode(PIN_D_MUX_S3,   OUTPUT);

....loop


 for (byte i=0; i<MUX_CH_COUNT; i++) {
    PORTB = (PORTB & B11110000) | i;
    short val = analogRead(PIN_A_MUX_SIG);

First and last line

Is my question not clear? no ping-pong please

I am using a analog multiplexer - I am looking for code only.

CD74HC4067 74HC4067 16-Channel Analog Digital Multiplexer Board Module

krisferrari:
Hello,

I have a connection setup involving - CD74HC4067 , Nano and IR proximity Sensors

C0 to C15 are connected to each of my IR sensor

When I test the output, it is giving values other than my expectation. My expectation is to clearly identify, which sensor became active. [ I think I am getting distance value like 344/345/346 etc], But I want active sensor i.e. sensor whose digitalread is 1.

Please can you help me with code.
The code I used is below - I dont know anything below, just trying

#define PIN_D_MUX_S0 8  // bit 7 of PORTB  

#define PIN_D_MUX_S1 9  // bit 6 of PORTB
#define PIN_D_MUX_S2 10 // bit 5 of PORTB
#define PIN_D_MUX_S3 11 // bit 4 of PORTB
#define PIN_A_MUX_SIG 3 // This pin will read the input from the mux.

....setup

pinMode(PIN_D_MUX_S0,  OUTPUT);
  pinMode(PIN_D_MUX_S1,  OUTPUT);
  pinMode(PIN_D_MUX_S2,  OUTPUT);
  pinMode(PIN_D_MUX_S3,  OUTPUT);

....loop

for (byte i=0; i<MUX_CH_COUNT; i++) {
    PORTB = (PORTB & B11110000) | i;
    short val = analogRead(PIN_A_MUX_SIG);
    // "val" holds the value for input "i", so you can insert your custom code here.
   
    // Print the values...
    Serial.print(i);   
    Serial.print(": ");
    Serial.print(val);
    Serial.println(" | ");
}

I think the below tutorial code may work

https://www.critics-corporation.com/RaspberryPi/cd74hc4067-multiplex-16-canali-analogici-digitali-arduino-sketch?doing_wp_cron=1581812820.5356559753417968750000

That one also does an analog, not digital read. Are you asking how to convert it to read your digital sensors?

I will try to reframe my question -

I have 16 IR proximity digital sensors, and I want to read multiplexer SIG, to get the active IR sensor out of the 16.

You can use an analog mux to switch digital signals, bearing in mind the bandwidth and impedance differences between an analog and digital mux. Just connect the mux output to an Arduino pin that has been configured as a digital input, and read the digital state using the digitalRead() function.

I don't understand your suggestion. It looks like I tried it...and it was always 1

what I want - A number 0...15 representing one of the 16 IR sensors, that is active [ below is a prototype with only 3 sensors]

I dont know - how to read SIG pin or the Arduino D0/Rx pin to identify which IR sensors Digital Read went 0.

sketch:

#define MUX_CH_COUNT 3 // Reduce this number if you use less channels
#define PIN_D_MUX_S0 8  // bit 7 of PORTB  
#define PIN_D_MUX_S1 9  // bit 6 of PORTB
#define PIN_D_MUX_S2 10 // bit 5 of PORTB
#define PIN_D_MUX_S3 11 // bit 4 of PORTB
#define PIN_A_MUX_SIG 0 // This pin will read the input from the mux. 


void setup() {
  Serial.begin(9600);
  
  pinMode(PIN_D_MUX_S0,   OUTPUT);
  pinMode(PIN_D_MUX_S1,   OUTPUT);
  pinMode(PIN_D_MUX_S2,   OUTPUT);
  pinMode(PIN_D_MUX_S3,   OUTPUT);

  digitalWrite(PIN_D_MUX_S0, LOW);
digitalWrite(PIN_D_MUX_S1, LOW);
digitalWrite(PIN_D_MUX_S2, LOW);
digitalWrite(PIN_D_MUX_S3, LOW);
 
}
void loop(){
  //Loop through and read all 16 values
  //Reports back Value at channel 6 is: 346
    for(int i = 0; i < MUX_CH_COUNT; i ++){ 
      Serial.print("Value at channel "); 
      Serial.print(i); 
      Serial.print("is : "); 
      Serial.println(readMux(i)); 
      delay(1000); 
    }
  }
  
  int readMux(int channel){ 
  
  int controlPin[] = {PIN_D_MUX_S0, PIN_D_MUX_S1, PIN_D_MUX_S2, PIN_D_MUX_S3}; 
  int muxChannel[MUX_CH_COUNT][4]={ {0,0,0,0}, //channel 0 
  {1,0,0,0}, //channel 1 
  {0,1,0,0}
  }; //loop through the 4 sig 
  
  for(int i = 0; i < 4 ; i ++){ digitalWrite(controlPin[i], muxChannel[channel][i]); } //read the value at the SIG pin 
  
  int val = analogRead(PIN_A_MUX_SIG); //return the value 
  return val;
}

Do you know how a mux works? You supply it with an address (in this case, four bits) and it connects one of 16 I/O's to a common I/O. So your sketch has to issue an address and read the common I/O.

Nobody here can really help you unless you supply a schematic.

After all the things I told you, you're still doing an analog read. Why? It's a digital signal. You should use the digitalRead() function for that.

I dont know how Mux works, and I want to know, hence trying this example.
Dont know what the output like 346, 270 etc numbers mean.

You are using technical terms like common I/O etc. and I dont know what commo I/O is [ is it SIG?]

If you/others know then- I want to know how to find the active channel. i.e. C0 to C15 which is active and to get that value, what and where should I read?
If IR Sensor 10 is active then I want answer like 9 [ assuming 0 index] to identify that IR Sensor 10 has an obstruction

S1 S1 S2 S3 are connected to 8, 9 , 10, 11 and I never heard of PORTB. Dont know the purpose

krisferrari:
I dont know how Mux works, and I want to know ....

Well, the Datasheet would be the likely place to start. Page 2 has a nice picture.

Thank you . I am aware of the data sheet, the theory part. hexadecimal number system

But I want to understand, how I can interpret the active sensor. and I am not getting that answer.

If 16 sensors are attched to pins C0 to C15, - How to identify active sensor using 16 channel analaog multiplexer

It's not clear to me that you've looked at the datasheet I referenced. For starters, there are no pins labeled C0 - C15. They are I0 - I15.

That aside, and assuming that you have it wired correctly in the schematic you haven't shown us (despite repeated requests), then the 4-bit that you apply to select pins S0 - S3 determine which of the 16 inputs is routed to the common output.

gfvalvo:
It's not clear to me that you've looked at the datasheet I referenced. For starters, there are no pins labeled C0 - C15. They are I0 - I15.

That aside, and assuming that you have it wired correctly in the schematic you haven't shown us (despite repeated requests), then the 4-bit that you apply to select pins S0 - S3 determine which of the 16 inputs is routed to the common output.

Is the code not telling the detail , you are looking for? Not sure how many requests you made?

C0 to C15 as in the picture