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?




Saturday, June 20, 2020

0000 0000 0011 0100

Joule Thief stable output development PCB


I have pushed the button on so many PCB designs in the past that I now look back at the older iterations with a degree of regret. I have come to recognise and appreciate the value of this digital evolution because I learned a lot in the process - but I still want the money and all of that development time back please!

The design I'm going to look at in this blog is the stable joule thief PCB shown in an earlier blog. To be fair to the circuit it has always worked well in each iteration, and I have always hurriedly opened up the packets from the manufacturer, soldered them up and huffed and puffed about the place quite...er...chuffed.

But...the latest incarnation is the huffiest, puffiest and chuffiest I think (well, until the next one I guess). The requirements for the PCB have changed over time (along with my understanding of PCB design). 

Originally the PCB was specifically developed for a dedicated night-time "candle" project, but the project evolved to require the following:
  1. Stable voltage output for running a microcontroller
  2. All pins of microcontroller broken out
  3. Solar panel input to rechargeable battery if required
  4. Breadboard friendly for development
  5. Less SMD and more through hole components for ease of soldering
Blurry for a reason - not proud of all of these!

All versions do work as intended, but the latest is really nice as it fulfils all of the design criteria. Here are the steps along the way.








As per usual as soon as the PCB arrives and is soldered up I am already thinking of the next iteration, but in this case the result is so useful I've thrown it on my Tindie Store in case anyone might find it useful/fun as well.

Next I want to use two QX5252 ICs and (maybe) drive the output throughout the day when a solar panel is connected (at the moment using one QX5252, the output current is shut off when charging the battery). I get the feeling that I might be doing iterations of this damn thing for a few years yet! And loving it...



I sell on Tindie

Saturday, June 13, 2020

0000 0000 0011 0011

CD4020 Counting Guy (sort of...)


In an earlier blog I used a shift register and an ATTiny13a to count using LEDs as a visual output, but there might be times when you want a non-programmed IC to do the same thing. Enter the strange and eventually usable CD4020B 14-bit Binary Counter/Divider.

I say "eventually usable" because it has an interesting design twist, and as well I totes messed up the wiring to the LEDs (twice) in the first instance.

Let's look at the design twist first. When I was trying to see how I could hook up the LEDs so that they count in binary (e.g. 8 LEDs to count from 0-255), I was surprised to learn that two of the so-called "outputs" are inside the chip (!) and so despite a lot of well meaning gif makers on the internet showing how this chip works, I got this strange counting pattern where the first LED flickered away merrily with seemingly no relation to the second LED (see video for explanation). 

The data sheet reveals the truth as it leaves the Q2 and Q3 "outputs" off in the following pinout diagram.


I wanted to see how these "missing" outputs affected the light show, so I set up a roughly 1Hz output from a 555 timer and also a reset button to start the "counter".


The 1Hz input proved too fast for my old eyes to follow, so I hooked up the reset button to the clock input and then manually clicked and recorded each input and output. The result was a rather tedious spreadsheet which does however show the following timing.
Note that the coloured columns are an educated guess at the "inside" outputs. The timing chart is confirmed when I go back to the 1Hz clock and then time from when the reset button is hit to when the Q6 output flips. So the chip is working and not full of tiny socks (long story).

So whilst this is all fine if you want to use the chip as a timer (e.g. to turn on something after 1 hour), it is a bit tougher to use it to "count" in binary as the missing outputs throw the LED activated count out.

The obvious answer was to simply not use Q1, Q2 and Q3 and start the counter at zero by hooking up the first LED to Q4 with an increase in the clock signal frequency to compensate. Here it is diagrammatically with 25 indicated in binary. Note the wacky relationship between pins and outputs - someone at the factory is having a laugh!

Pin 15Pin 14Pin 12Pin 13Pin 6Pin 4Pin 5Pin 7
 Q11  Q10  Q9  Q8  Q7  Q6  Q5  Q4
0 0 0 1 1 0 0 1

I also originally, for some strange reason, wired up the CD020 to output to the LEDs from left to right, even though I wanted a "normal" binary counter which for me is right to left. I rewired the rat's nest and then worked out later that a much smarter me would have just turned the breadboard around 180 degrees!

The final issue to be addressed was my ridiculous original wiring of the SMD-to-DIP adapter which had a comical line of 470Ω resistors down it's spine. The result of this nonsense of course is an effective row of parallel resistors as shown in the following picture which blinded me with the lowered overall resistance (300Ω).


Noob mistake = damn bright LEDs

Not only did I re-wire it in a sane manner, but I also increased the resistance on both the adapter (1k) and the breadboard (3k3) in the final version to reduce the chance of burnt out retinas.

So the binary counter as originally desired was finally possible with:
  1. Omission of Q1, Q2 and Q3 from the counter
  2. Re-wiring LEDs left to right
  3. Changing resistors to be series not parallel
  4. Increasing resistance to sensible levels







Saturday, June 6, 2020

0000 0000 0011 0010

Blinken LEDs, and lots of them

Despite all of my earnest blogging about fancy circuits and exotic components, at the heart of my electronics obsession is just the love of blinking LEDs. This is thanks mainly to Big Clive and a host of other influences online that use "der Blinkenlichten" to either illustrate their work, or to test if various circuits are working. And I follow their LEaD.

The more interesting an LED, the more I have to have it in the buckets. It is true that a blinking light is a delight when testing a microcontroller or circuit because:
  1. if the light blinks, then there is contact between the programmer and the μC
  2. if the light blinks when you want it to, then the timing of the chip and/or code is correct, and finally
  3. they look pretty
I have two rather indulgently full buckets devoted to LEDs, one bucket which is for single lights and the other for LED strips. Looking the other day at the single lights bucket I gathered a few and planted them on a breadboard for your my pleasure.

Just add juice

I will write on this blog at some point in the future about how I put together the "candle" 5050 yellow LEDs as it is a worthwhile construction process, but for this blog I have chosen some simpler lights which can be used pretty much out of the bag.

Mostly these little guys are multi-coloured, and some have a few surprises, but I will let you judge their worthiness in the following video - enjoy!