Tuesday, October 7, 2014

Triggering Ten Pins with Four Outputs

A common issue in microcontrollers: what do you do when the pins on your controller do not match well with the outputs to your controlled items?  This is something I wanted to write about in Getting Started with Adafruit Trinket but I was over on page count as it was, so I'll post it here for you.

What the designer may not normally think about is using TTL logic chips.  Often called 74 series after the original numbering by Texas Instruments, there are hundreds of pre-existing integrated circuits to help make outstanding projects.  A number of years back it was one of the only ways to make circuits.... anyway...

Take Trinket.  There are only 5 I/O pins.  What if you want to use some sensor or serial input to output one of ten outputs (say to the delicious Adafruit Audio FX Sound Board that just came out).  You could be satisfied triggering only 4 of the sound board's trigger pins. But we want more!

So 4 binary digits can actually represent 16 different outputs (0 to 15 or 0 to F in hexadecimal).  Would one create a bunch of 7400 series logic (AND, OR, NAND) gates to change a number to one of several outputs?  You could, but it takes alot of gates;

Gates to decode 4 digital signals to 10 outputs
There is a way to reduce this down to one 16-pin DIP chip - use the 7442 BCD to decimal decoder IC.  All the logic above is already pre-wired into one easy to use chip!  So how would we wire Trinket to use the 7442 to the Adafruit Audio FX Sound Board which wants one of many pins to trigger a file to be played?  See the wiring diagram below:


If you set the right 4 bit number on Trinket pins 0 to 3, it will trigger one of ten outputs to the sound board, triggering the playing of up to ten sounds.  The code to trigger output 5 would include:

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

void loop() {
   digitalWrite(0, HIGH);
   digitalWrite(1, LOW);
   digitalWrite(2, HIGH);
   digitalWrite(3, LOW);
}

If you hooked a transmitter like an Adafruit Bluefruit or other receiver to Trinket Pin #4, you could use Trinket to set the right combination of 4 states on Pins #0-3 to trigger the right sound output. (Note: if you connect to pins #3 and #4 on Trinket, be sure to disconnect these when you use the USB programming connection, then reconnect when programming is done). And the Sound Board can hold alot of WAV file sound compared to a tiny bit on Trinket itself.

Note the data sheet I link to is the 74HC42.  The letters in the middle (if any) designate different types of logic (some improvements have been made in these chips over the years.  I'm partial to LS logic, although nearly any will do that I'm aware of, just be sure if you use 5 volt logic you are consistent).

You can see the usefulness of older chips in making the most of newer microcontrollers and modules.  With a bit of study of the 74 series chips, I'm sure you can find some that will help you in projects on your drawing board.