Hi everyone,
I am quite struggling to make some 4051 working together in order to get more than 8 analog inputs to be read for my Arduino... I will need 24 analog inputs to be read by the arduino but I don't know how to tell the arduino to do so
I did one 4051, but when I need to cascade them it all becomes quite hard for a beginner like me.
Moreover I would also like to have a feedback (e.g. a Serial.println) in order to know exactely which input has passed the threshold, because I'll need LEDs to fadeIn and out according to it.
So far I tried to came out with something like this with only one 4051
int s0 = 10;
int s1 = 9;
int s2 = 8;
int rowTTL = 0;
int binTTL [] = {
0, 1, 2, 3, 4, 5, 6, 7};//bin = bin?r, some times it is so easy
int inPin = 4;
int result;
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
Serial.begin(9600);
}
void loop(){
for(int i=0; i<8; i++){
rowTTL = binTTL[i];
sendABCTTL(rowTTL);
result = analogRead(inPin);
if(result < 1020) { // no contact
Serial.println(0);
}
else{
Serial.println(1); // contact!
}
delay(500);
}
}
void sendABCTTL(int rowTTL){
switch(rowTTL) {
case 0:
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
break;
case 1:
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
break;
case 2:
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
break;
case 3:
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
break;
case 4:
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, HIGH);
break;
case 5:
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
digitalWrite(s2, HIGH);
break;
case 6:
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
digitalWrite(s2, HIGH);
break;
case 7:
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
digitalWrite(s2, HIGH);
break;
}
}
I guess I'll need as many "for (...){}" as 4051 to make it work, but I don't know how exactely. Moreover... Some doubts with the circuits:
in this link www.arduino.cc/playground/Learning/4051 the mux are in cascade in a way (left img) but in this other link I found they are set in a different way 4051.jpg (image)
Anyone has an idea on how to help me? I am doing my thesis project and this part is slowing me down quite a lot. Help would be extremely appreciated. Thanks to everyone!