Getting into digital electronics

by  in Technology

Hardware. For a software engineer. What could possibly go wrong?

Back when I was a kid I had an electronic set that explained the basics of electronics. Plug in a battery, plug in the parts, and you have something working. It started with basics like switches, resistors, realizing that LEDs need to be plugged in in a specific direction, down to the point where the guide had you build a flip-flop using transistors. Suffice to say, it was wonderful. The German company (Kosmos) who made it still makes tons of minilabs for kids, I can thoroughly recommend them.

A picture of a gray plastic box with metal contacts and electronic parts that can be plugged in.
The Kosmos Electronic XN 3000 system I used to have. Sadly, it is no longer produced. Copyright © Franckh-Kosmos Verlags-GmbH & Co. KG

Forward time about 30 years, and I am sitting with a youngster suddenly becoming interested in programming, electronics, and installing Logic World on Steam. The next school project? Explaining how traffic lights work. So, what else is left to do, it was time to dust off the memories from childhood and the two semesters I took in digital electronics at university.

How does a software engineer start at hardware? Of course, with a rather large order of parts. Since I knew the basics (AND gate, OR gate, etc) I scoured the Internet for integrated circuits (ICs) that contained these gates. Soon enough I realized that I wouldn’t find an IC with a single gate anymore, most ICs nowadays are either 2 or 4 channel, meaning that there are 2 or 4 gates on one IC. I also realized that I wouldn’t just need to plug in the logic inputs and outputs, I would need to provide power to the ICs. In order words, things would be a bit more complicated than in Logic World. (Big surprise, I know.)

Finally, I settled on a bunch of SN74LSxxN and SN74HCxxN series ICs. I also ordered a bunch of breadboards, cables, resistors, colored LEDs, and most importantly, a 5V power supply.

Getting burnt… by a simple circuit ▲ Back to top

Being bullish about my memories of electronics I put together a simple circuit. An LED and a resistor. I did remember that I need one of those. Do you know what I forgot? If you’re into electronics, you’ll spot this one easily:

A breadboard with a circuit consisting of an LED and a resistor with the markings of brown-black-brown.

Yes, that’s right. I had no idea what size resistor to put in. It was too long ago, and I didn’t remember, so I just put a 10Ω resistor in. What could go wrong?

Well, it blew out the LED and when I wanted to pull the resistor afterwards, I burned my hand. That’s what.

Inverter circuit ▲ Back to top

Needless to say, I got a lot more careful after that. I wanted to try a slightly more complicated circuit with a NOT gate. After a substantial amount of searching the Internet I picked the SN74HC04N IC from the bunch. Thankfully, Texas Instruments has very nice datasheets which explain how the pins are supposed to be connected.

One thing I learned while looking at these sheets was the orientation of the ICs. Each of these ICs has a little notch. On the drawing the side with the notch will usually be on the top or to the left.

Picture of the pinout of the SN74HC04N. The notch is to the left, pins from the top left: Vcc, 6A, 6Y, 5A, 5Y, 4A, 4Y. In the bottom row: 1A, 1Y, 2A, 2Y, 3A, 3Y, GND. The NOT gates go from the A pins to the Y pins.

Enough theory, let’s get started by powering the IC. I connected the Vcc pin to the positive power rail and the GND pin to the negative rail.

A picture of a breadboard with a IC plugged in. The cable from the positive rail goes to the left top pin, the bottom right pin is connected to the negative power rail. The notch on the IC points to the left.

Next up, I needed to add a button. The buttons I got were weird: they had 4 pins. Luckily, I couldn’t resist the shopping urge and also ordered a multimeter. With a disconnected power supply, I measured the resistance on the button pins and found out that when pressed the button connected the two pins on the left to the two pins on the right.

A picture of a multimeter measuring the resistance between two pins on the button.

Having figured that out I added the wiring for the button from the positive power rail to the 1A pin of the IC.

A picture of the button fully wired up to the 1A pin.

Next, I wired up the LED and turned on the power. Behold, the power of a NOT gate! When I pressed the button the light turned off!

A picture showing the 1Y output connected to an LED and a 330 ohm resistor to the negative powerline.

As it turned out later, I made a critical mistake: I left out the pull-down resistor. I learned that when connecting a button one must always add a large resistor to the opposite power rail. This is needed to ensure that zero is actually zero when the button is not pressed and the input on the IC does not receive power. Not adding a pull-down resistor would leave the voltage hovering somewhere between zero and one. Looking through a phone camera this is even visible: the LED will actually flicker. This is called a floating input, as I found out.

I spent considerable time trying to find out how large this resistor should be and finally settled on using a 10 one. I’m pretty sure that for more complex circuits I’d need precise calculations, but for smaller circuits it would apparently not matter as much.

A picture showing the pull-down resistor connected between the 1A pin and the negative power rail..

SR-Latch ▲ Back to top

Next, I tried out a couple more circuits, such as the AND and OR gates, which worked just as expected. Then I wanted to build something more complicated: an SR-Latch. This latch contains two inputs, one to set and one to reset the value sent to the output pins.

The SR-Latch can be built in multiple ways, I chose to use two NOR (not-OR) gates.

A diagram of the SR-latch.

In my setup I added two buttons to serve as inputs. Each button was connected to one NOR gate. The second input of each gate was the output of the other, respectively. When the button was pushed and at the same time the other part of the latch was active, it would switch over.

An animation showing the SR-latch in action. Pushing one button always switches over to the other LED.

By the time I was done it dawned on me that building anything useful out of simple logic gates would probably consume quite a few parts and take up a lot of space. There was a reason why we used FPGAs at university after all!

Traffic lights ▲ Back to top

The real reason why I did all this was to build traffic lights. Thankfully, the box of ICs I ordered contained an SN74HC164N 8-bit shift register. A shift register takes an input and then moves it up to the higher bits when it gets a clock signal.

In the traffic light example, I wanted to use this to simply feed it a 1, and then use the clock signal to move it to the second, third, fourth output and so on. Each output would then switch the traffic light into a specific state. For example, state 1 would be one signal being green, the other being red. The second state would be one yellow, the other red. You get the idea.

The IC I had in my inventory had two inputs, A and B, both of which needed to be set to 1 in order to set the input to the shift register. The outputs labeled QA through QH were the outputs of the register, one for each bit. It also had an input called CLK, which triggered moving the bits, and an RST input, which would reset the register to all zeroes when it didn’t get an input. Yes, it’s a negated reset, so I needed to power it constantly to keep it working.

A layout of the SN74HC164N IC. Inputs from the top left are Vcc, QH through QE, a negated CLR input, CLK input. In the lower row: A and B inputs, QA through to QD, and GND.

I proceeded to write up which step should light up which LED.

QA
First LED green, second red.
QB
First LED yellow, second red.
QC
First LED red, second yellow.
QD
First LED red, second green.
QE
First LED red, second yellow.
QF
First LED yellow, second red.

This gave me the following formulas:

Green 1  = QA
Yellow 1 = QB or QF
Red 1    = QC or QD or QE
Green 2  = QD
Yellow 2 = QC or QE
Red 2    = QA or QB or QF 

In order to implement these formulas I had to add two OR gate ICs (SN74HC32N) connected to the outputs of the shift register. I also connected manual switches for the initial input signal and the clock signal for testing.

As a final piece of the puzzle I connected the last (QF) output to the A and B inputs via a diode to make the traffic light loop around. I needed a diode so that pressing the start button doesn’t light up the LEDs connected to QF. A diode only lets electricity flow one way.

A picture of a the schematic. The center has the shift register and the two sides have OR gates to implement the logic explained above.
The finished traffic light circuit. The center is the shift register, the two ICs on the side are OR gates implementing the rules above.

So far the theory, let’s look at the mess I made in the real world:

A photo of the finished circuit with lots and lots of cables.
The finished circuit, one traffic light on the left, one on the right, a glorious mess of cables in-between.

I’m pretty happy with the results. As a next step I want to automate switching the lights using an NE555P timer IC. I also want to automate the initial start using AND and NOT gates.

Is this how you are supposed to do things? Probably not. Thankfully, I’m not building a real-world traffic light here. I hope this little post inspired you to try out something new. Happy hacking!