Demux in cascade tutorial

For a particular circuit I needed to control 19 solid state relays (SSR) with as little as possible microcontroller outputs. But I had to be sure that no more than one SSR is active at a any time.

One solution would be to use shift registers in cascade, it would only need at most 2 or 3 outputs of the microcontroller. Only with regard to the second point, no more than one SSR is active at a any time, it is not reliable. It only needs one external ripple to occur on the data line and there may be two or more SSR's are active.

I therefore opted for a demultiplexer (demux) IC. A well known analog mux / demux is the 4051. It has one input which is passed on to channeled to 1 of 8 selectable outputs. This selection is done by 3 address lines.
However, I have opted for a digital demux. There is no input on a digital demux, but via the address lines, you can select which output is low, the remaining outputs will remain high.
I've chosen the SN74LS138 by Texas Instruments. This is a '3-line to 8-line decoder / demultiplexer. This means that it has 3 address lines  and 8 outputs. But the good thing is that he has an extra 3 lines to enable or disable the IC.

By using these extra enable lines the demux can easily be used in cascade without any external inverters. This way we can control 24 outputs with only three ICs through 5 address lines! Exactly what I need.
The datasheet also mentions this possibility, but it doesn't explain very clear how to do this and also an example circuit is missing.
That's why I took it upon myself to write this tutorial

If we look at the data sheet, we find truth table

INPUTS OUTPUTS
ENABLE SELECT
G1 ~G2A + ~G2B C B A 0 1 2 3 4 5 6 7
L H X X X   H H H H H H H H
H L X X X   H H H H H H H H
H H X X X   H H H H H H H H
L L L L L   L H H H H H H H
L L L L H   H L H H H H H H
L L L H L   H H L H H H H H
L L L H H   H H H L H H H H
L L H L L   H H H H L H H H
L L H L H   H H H H H L H H
L L H H L   H H H H H H L H
L L H H H   H H H H H H H L

As you can see the IC is disabled if G2A or G2B or G1 is high. This fact we're gonna use to make sure only no more than one IC is enabled at any time.

For a better overview, it is clearer to put the first 3 lines of the truth table at the bottom. Here we can also fill in the X's.
This way we get 5 address lines that go from 00000 to 11000, or from 0 to 24.

INPUTS OUTPUTS
ENABLE SELECT IC1 IC2 IC3
G1 ~G2A + ~G2B C B A 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
L L L L L   L H H H H H H H H H H H H H H H H H H H H H H H
L L L L H   H L H H H H H H H H H H H H H H H H H H H H H H
L L L H L   H H L H H H H H H H H H H H H H H H H H H H H H
L L L H H   H H H L H H H H H H H H H H H H H H H H H H H H
L L H L L   H H H H L H H H H H H H H H H H H H H H H H H H
L L H L H   H H H H H L H H H H H H H H H H H H H H H H H H
L L H H L   H H H H H H L H H H H H H H H H H H H H H H H H
L L H H H   H H H H H H H L H H H H H H H H H H H H H H H H
L H L L L   H H H H H H H H L H H H H H H H H H H H H H H H
L H L L H   H H H H H H H H H L H H H H H H H H H H H H H H
L H L H L   H H H H H H H H H H L H H H H H H H H H H H H H
L H L H H   H H H H H H H H H H H L H H H H H H H H H H H H
L H H L L   H H H H H H H H H H H H L H H H H H H H H H H H
L H H L H   H H H H H H H H H H H H H L H H H H H H H H H H
L H H H L   H H H H H H H H H H H H H H L H H H H H H H H H
L H H H H   H H H H H H H H H H H H H H H L H H H H H H H H
H L L L L   H H H H H H H H H H H H H H H H L H H H H H H H
H L L L H H H H H H H H H H H H H H H H H H L H H H H H H
H L L H L H H H H H H H H H H H H H H H H H H L H H H H H
H L L H H H H H H H H H H H H H H H H H H H H H L H H H H
H L H L L H H H H H H H H H H H H H H H H H H H H L H H H
H L H L H H H H H H H H H H H H H H H H H H H H H H L H H
H L H H L H H H H H H H H H H H H H H H H H H H H H H L H
H L H H H H H H H H H H H H H H H H H H H H H H H H H H L
H H L L L H H H H H H H H H H H H H H H H H H H H H H H H

Connecting the ICs

The table above is how we want it, but how we connect it to the demux ICs? I'm going to drive the circuit with a PIC16F690. The lines A till G1 I connect to PORTC of the PIC, these are RC0 till RC4.

The connection of IC1 is quite simple:
A   -> RCO
B   -> RC1
C   -> RC2
G2A -> RC3
G2B -> RC4
G1  -> Vdd

This is it for the first 8 outputs. In other words, for PORTC counting from 0 till 7 (0b00111). But PORTC keeps counting till 23! The next step is 8, in binary 0b01000. At this point IC1 must be disabled and IC2 enabled.
If PORTC=8 then only RC3 high, therefore we connect RC3 with G2A of IC1. In the truth table from the datasheet we can see that the IC1 is then disabled. IC2 should now be enabled, therefore we conclude that RC3 must be connected with G1 of IC2.

The connections for IC2 look like this:
A   -> RCO
B   -> RC1
C   -> RC2
G2A -> RC4
G2B -> GND
G1  -> RC3

So we can now count up to 15 (0b01111), after that IC2 should stop and IC3 begin. When PORTC reaches 16 only RC4 will be high, so we connect this to G1 of IC3.
We can now count till 23. If you do not need all outputs to be high then connect G2A to ground and you're done. You could also connect G2A to RC3 and when PORTC reaches 24 (0b11000) all the outputs will be off.

The connections for IC2 look like this:
A   -> RCO
B   -> RC1
C   -> RC2
G2A -> RC3 / GND
G2B -> GND
G1  -> RC4

The schematic

Now all together in one schematic. Instead of the SSRs I used leds because it makes it easier to see the effect.

And the end result of all this:

The green LEDs are the 24 outputs of the demux ICs. The yellow LEDs at the bottom right of the screen indicates the state of PORTC, only it's left Justified (RC0 is the left LED and RC4 the right).

I hope this tutorial was of use. Please let me know your opinion.