Getting into digital electronics
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.
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 (
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
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
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:
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.
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.
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.
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.
Having figured that out I added the wiring for the button from the positive power rail to the 1A pin of the IC.
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!
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 10kΩ 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.
Next, I tried out a couple more circuits, such as the
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
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.
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!
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,
B, both of which needed to be set to
1 in order to set the input to the shift register. The outputs labeled
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.
I proceeded to write up which step should light up which LED.
- First LED green, second red.
- First LED yellow, second red.
- First LED red, second yellow.
- First LED red, second green.
- First LED red, second yellow.
- 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
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.
So far the theory, let’s look at the mess I made in the real world:
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
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!