Hello everyone
I am trying to get 16 buttons into my teensy 3.1 with as little pins as possible. I have been using the 4051 multiplexer
http://www.mouser.com/ProductDetail/Texas-Instruments/CD4051BE/?qs=q2XTDbzbm6DxulBsMcV7tA==
but am coming across some completely unexplainable (to me and a few other tinkerers at least) issues
***I am considering the possibility that the issue is actually with the teensy and not my hardware or software, because when I keep everything exactly the same, but replace the teensy 3.1 with an arduino or chipKit, everything then works smoothly***
I know the multiplexers work, because when I use them as demultiplexers to consecutively power some LEDS, it works as expected.
Here is a picture of my breadboard
As you can see, I am currently testing with only 4 buttons. The power for the rails is 5V from the teensy VUSB.
The code functions as follows
___
pin5 of the teensy is connected to the common out/in of the multiplexer and is pulled high with the internal pullup resistor. The 4 buttons, through the multiplexer, are meant to pull this pin low when pressed.
set multiplexer to first address - this should make a connection between the common out/in (pin 3) and channel 0 (pin 13)
read value of common pin and store to variable (b0)
set multiplexer to second address - this should make a connection between the common out/in (pin 3) and channel 1 (pin 14)
read value of common pin and store to variable (b1)
set multiplexer to first address - this should make a connection between the common out/in (pin 3) and channel 2 (pin 15)
read value of common pin and store to variable (b2)
set multiplexer to fourth address - this should make a connection between the common out/in (pin 3) and channel 3 (pin 12)
read value of common pin and store to variable (b3)
print values of stored variables in serial monitor
_____
ISSUE
what is happening is that when I read these variables, am I given a consistent, incorrect, and seemingly unexplainable response.
b0 should read high until I press down button one - which pulls teensypin 5 to ground when the address is set to the first (pin2=LOW, pin3=LOW, pin4=LOW))
this happens, but b0 also reads low when button2 is pressed (meaning that pin15 is connected to common out/in pin when the address doesnt tell it to...)
b1 reads low when button1 is pressed, but also when button0 is pressed
b2 reads low when button0, button1, and button3 are pressed
b3 reads low when button2 is pressed
this makes NO sense to me
at first thought problem was using 3.3v for 4051 that calls for 5v. now using 5v, no help.
thought maybe multiplexer just fried - however when setting pin5 to an output and wiring to LEDs instead of buttons, everything works perfectly
and the main concern - THIS WORKS PERFECTLY WITH ARDUINO WITH THE SAME PIN/WIRING CONFIGURATION
here is my arduino code
____________________________________
int ax = 2; //address pins
int bx = 3; //address pins
int cx = 4; //address pins
int b0 = 0; //button values
int b1 = 0; //button values
int b2 = 0; //button values
int b3 = 0; //button values
void setup(){
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT_PULLUP);
}
void loop(){
digitalWrite(ax, LOW);
digitalWrite(bx, LOW);
digitalWrite(cx, LOW);
b0 = digitalRead(5);
digitalWrite(ax, HIGH);
digitalWrite(bx, LOW);
digitalWrite(cx, LOW);
b1 = digitalRead(5);
digitalWrite(ax, LOW);
digitalWrite(bx, HIGH);
digitalWrite(cx, LOW);
b2 = digitalRead(5);
digitalWrite(ax, HIGH);
digitalWrite(bx, HIGH);
digitalWrite(cx, LOW);
b3 = digitalRead(5);
Serial.println(b0); //i swap b0 with b1, b2, b3 to look at those values
}
________________________________
The problem must be with either something internal to the teensy, or something with a difference with teensyduino code vs the way an arduino reads arduino language...
Any ideas?? this is driving me insane!!!!!
Thanks in advance for your time
Zach
I am trying to get 16 buttons into my teensy 3.1 with as little pins as possible. I have been using the 4051 multiplexer
http://www.mouser.com/ProductDetail/Texas-Instruments/CD4051BE/?qs=q2XTDbzbm6DxulBsMcV7tA==
but am coming across some completely unexplainable (to me and a few other tinkerers at least) issues
***I am considering the possibility that the issue is actually with the teensy and not my hardware or software, because when I keep everything exactly the same, but replace the teensy 3.1 with an arduino or chipKit, everything then works smoothly***
I know the multiplexers work, because when I use them as demultiplexers to consecutively power some LEDS, it works as expected.
Here is a picture of my breadboard
As you can see, I am currently testing with only 4 buttons. The power for the rails is 5V from the teensy VUSB.
The code functions as follows
___
pin5 of the teensy is connected to the common out/in of the multiplexer and is pulled high with the internal pullup resistor. The 4 buttons, through the multiplexer, are meant to pull this pin low when pressed.
set multiplexer to first address - this should make a connection between the common out/in (pin 3) and channel 0 (pin 13)
read value of common pin and store to variable (b0)
set multiplexer to second address - this should make a connection between the common out/in (pin 3) and channel 1 (pin 14)
read value of common pin and store to variable (b1)
set multiplexer to first address - this should make a connection between the common out/in (pin 3) and channel 2 (pin 15)
read value of common pin and store to variable (b2)
set multiplexer to fourth address - this should make a connection between the common out/in (pin 3) and channel 3 (pin 12)
read value of common pin and store to variable (b3)
print values of stored variables in serial monitor
_____
ISSUE
what is happening is that when I read these variables, am I given a consistent, incorrect, and seemingly unexplainable response.
b0 should read high until I press down button one - which pulls teensypin 5 to ground when the address is set to the first (pin2=LOW, pin3=LOW, pin4=LOW))
this happens, but b0 also reads low when button2 is pressed (meaning that pin15 is connected to common out/in pin when the address doesnt tell it to...)
b1 reads low when button1 is pressed, but also when button0 is pressed
b2 reads low when button0, button1, and button3 are pressed
b3 reads low when button2 is pressed
this makes NO sense to me
at first thought problem was using 3.3v for 4051 that calls for 5v. now using 5v, no help.
thought maybe multiplexer just fried - however when setting pin5 to an output and wiring to LEDs instead of buttons, everything works perfectly
and the main concern - THIS WORKS PERFECTLY WITH ARDUINO WITH THE SAME PIN/WIRING CONFIGURATION
here is my arduino code
____________________________________
int ax = 2; //address pins
int bx = 3; //address pins
int cx = 4; //address pins
int b0 = 0; //button values
int b1 = 0; //button values
int b2 = 0; //button values
int b3 = 0; //button values
void setup(){
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT_PULLUP);
}
void loop(){
digitalWrite(ax, LOW);
digitalWrite(bx, LOW);
digitalWrite(cx, LOW);
b0 = digitalRead(5);
digitalWrite(ax, HIGH);
digitalWrite(bx, LOW);
digitalWrite(cx, LOW);
b1 = digitalRead(5);
digitalWrite(ax, LOW);
digitalWrite(bx, HIGH);
digitalWrite(cx, LOW);
b2 = digitalRead(5);
digitalWrite(ax, HIGH);
digitalWrite(bx, HIGH);
digitalWrite(cx, LOW);
b3 = digitalRead(5);
Serial.println(b0); //i swap b0 with b1, b2, b3 to look at those values
}
________________________________
The problem must be with either something internal to the teensy, or something with a difference with teensyduino code vs the way an arduino reads arduino language...
Any ideas?? this is driving me insane!!!!!
Thanks in advance for your time
Zach