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(" | ");
}
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);
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.
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.
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
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?