Wednesday, October 8, 2014

Select One From Several Inputs

Yesterday, we selected one of many outputs with only 4 pins from an Adafruit Trinket microcontroller.  Here is the mirror project.  Say you have 8 digital data lines in and only few microcontroller lines (Adafruit Trinket only has 5 I/O pins).  You can use a circuit like the one below:


The chip used is the 74XX151 8 input multiplexer.  The binary address of the line you want is sent on Trinket Pins #0, #1, and #2.  Then the data line selected is read in on Trinket Pin #3.  This leaves Pin #4 as an output for serial data or other use.  The code would similar to the sample code below if we only wanted to read the 74151 input pin 3:

void setup() {
   pinMode(0,OUTPUT);
   pinMode(1,OUTPUT);
   pinMode(2,OUTPUT);
   pinMode(3,INPUT);
}

void loop() {
   digitalWrite(0, HIGH);
   digitalWrite(1, HIGH);
   digitalWrite(2, LOW);
   int mydata = digitalRead(3);
}

For a robust program, you'd probably cycle through each 74151 pin address and read the values you want. 

If you only want to multiplex 4 inputs, you can use a smaller multiplexer or just ground S2 low, freeing Trinket Pin #2.

No comments:

Post a Comment