Saturday, June 27, 2020

0000 0000 0011 0101

CD4051 multiplexer/demultiplexer


For a long time I wondered why anyone would ever buy any of these cheap old school logic chips. Why not just use a microcontroller? Then one day I caved in and bought a few shift registers, and now years later I have buckets of lovely chips that give me great delight, and inspire me to make a few interesting circuits as well.

None more so than the versatile CD4051 (and it's cousins CD4052 and CD4053) which I am sure will appear on this blog more than once. They are multiplexer/demultiplexer ICs, which is a fancy way of saying that a few wires from the microcontroller (or indeed just a couple of buttons) can make for multiple outputs or inputs.

The first thing to establish is if the chips work or if they are full of tiny socks (long story). I hooked up a CD4051 chip with some simple buttons and LEDs to demonstrate the truth table shown below.



The "enable" pin is pin 6, and the "input/output" mode is selected from pin 3. On the breadboard above you can see the button on the left is select/com (input when low, button not pushed, output when high, button pushed) and then the three buttons on the right connect to A (pin 11), B (pin 10) and C (pin 9) respectively.

Then I've just hooked up some LEDs to A1 (pin 14) and A2 (pin 15) for toggling output, and LEDs to A5 (pin 5) and A7 (pin 4) for toggling input. The input LEDs do not carry as much current and seem to be dimmer.

After establishing that the chip was functioning, I then thought about how it could be used to select for different voltage outputs using a couple of voltage dividers. For instance from a single supply can we digitally select different voltage outputs (e.g for different sensors in the same circuit). Some back of the envelope calculations (actually using this site) gave me the idea of wiring up four 1206 resistors as follows.


Now if I could change the outputs of the CD4051 using the buttons, then I could selectively change the voltage using the dividers. Output A1 gives 5V and output A2 gives 3.3V.

Note that the voltage for the breadboard needs to be higher than 9V (in this case 9.3V) to give 9V at the divider thanks to a slight voltage drop across the CD4051. Note also that due to the 1206 resistors being a little inaccurate (+/- 5%), the 5.0V divider is a tad high and the 3.3V divider is a bit low!



Finally, I wanted to make this voltage divider digitally driven by my old friend the ATTiny13a instead of using my sausage fingers to mash buttons as seen above. I thought that the process of switching to a microcontroller was going to be easy, but it wasn't!

Firstly, the ATTiny13a would definitely not be happy with a 9V VCC rail, so I had to bring in an LM7805 linear voltage regulator to supply it with a more palatable 5V.

Secondly, I could not get 9V output from the CD4051 with a 5V signal (Duh! It's an analog multiplexer/demultiplexer) so the voltage divider was only being fed the 5V output from the CD4051 and therefore wasn't making the 3.3V and 5V I had calculated.

To fix this issue I firstly brought in a couple of transistors (SS8050 NPN) to switch 9V for the divider. Of course the NPN has load on the collector side and therefore the voltage divider was either at 9V when not switched by the transistor or at 5V or 3.3V when switched. So out comes the NPN and in goes the PNP 2SA1020 which worked fine (see diagram below).

Finally the voltage meters were blinding me at 9V (and 5V), so I also swapped in a separate 3.6V power rail for them (with a common ground) so that they weren't dazzling the camera.


It ended up being a bit of a Frankenstein circuit, but the concept does work!


Only a little bit of code was needed for the microcontroller to flip the transistors on and off.

void setup() {
DDRB = 0b00010111; // PB0, PB1, PB2 and PB4 set as OUTPUT
}

void loop() {
PORTB = 0b00010100; // PB4=1, PB3=0, PB2=1, PB1=0, PB0=0
delay(1500);
PORTB = 0b00010000; // PB4=1, PB3=0, PB2=0, PB1=0, PB0=0
delay(3000);
PORTB = 0b00010010; // PB4=1, PB3=0, PB2=1, PB1=0, PB0=0
delay(1500);
PORTB = 0b00010000; // PB4=1, PB3=0, PB2=0, PB1=0, PB0=0
delay(3000);
}

Purists will note that I could have just switched the transistors directly using just two output pins from the ATTiny13a without the need for the CD4051 at all, and as there are only two pins required for the voltage divider they would be correct!

But if more outputs are needed then the addition of a CD4051 makes the sacrifice of three output pins needed from the ATTiny13 worthwhile. For instance, I can add some code to make a few nice flashing LEDs - not because I like flashing LEDs of course, but because it proves the worthiness of the chip - right?




No comments:

Post a Comment