## Part 5 - More logic

And into the deep end we go.

In second half of Part 2, we looked at designing a piece of logic to replicate an XOR gate. This was really an academic exercise as XOR gates are already available via the 7486 IC. So, lets apply all this new knowledge to solving a real-world application... traffic lights.

Traffic lights in the UK consist of three coloured indicators, red, yellow and green, and operate in the following sequence.

In second half of Part 2, we looked at designing a piece of logic to replicate an XOR gate. This was really an academic exercise as XOR gates are already available via the 7486 IC. So, lets apply all this new knowledge to solving a real-world application... traffic lights.

Traffic lights in the UK consist of three coloured indicators, red, yellow and green, and operate in the following sequence.

Looking at the above pattern, it can be seen that there are four possible combinations of lit indicators; red, red + amber, green, and then amber on it's own. After this we are returning back to the first combination of just red on it's own, and four combinations means four possible states.

Interestingly, the 2-bit binary counter constructed previously can also produce four patterns but it does this via two outputs, and we need to control three indicators in a very specific sequence.

Interestingly, the 2-bit binary counter constructed previously can also produce four patterns but it does this via two outputs, and we need to control three indicators in a very specific sequence.

## Step 1 - Know your inputs and outputs

In anything you design, you always need to know and understand the goal; what are you trying to accomplish or the goal. Well, in this case the goal is to produce the UK traffic light sequence and to do this we will need to control three coloured indicators in a very specific pattern.

The above table shows the four patterns of illuminated lights that are required to replicate a traffic light display.

To make this table a little clearer and put it into a format that is more usable for our needs:

To make this table a little clearer and put it into a format that is more usable for our needs:

The obvious changes are replacing On/Off with Ones and Zeros. Next I've changed the Step number to start from zero instead of one. The reason is that whilst as humans we tend to start counting from one, computers and electronics tend to count from zero. Remember the 2-bit binary counter from the previous thrilling instalment? It could count from zero (all LED's off) thought to three (all LEDs on).

I also added the A and B columns which give the binary representation of the step number.

This table also shows us our TrafficLight function inputs and outputs. If we input A=1 and B=0 we would expect to see just the red and amber lights illuminated.

So, how do we go about converting this table into an equivalent collection of logic gates.

I also added the A and B columns which give the binary representation of the step number.

This table also shows us our TrafficLight function inputs and outputs. If we input A=1 and B=0 we would expect to see just the red and amber lights illuminated.

So, how do we go about converting this table into an equivalent collection of logic gates.

## Step 2 - Controlling the Red

Deciding where to start when designing a complex logic function can be tricky. Do you begin with what to do on each step, or with the outputs.

For this activity, we will look at the outputs, red, amber and then green.

Using the truth table above, we can see that red LED is only lit for steps 0 and 1, and in the following circumstances:

A = 0 and B = 0

A = 1 and B = 0

But look more closely at the table... there's a very simple pattern for the red indicator. Irrespective of what the logic value is for A, the red indicator ONLY illuminates if B = 0.

For this activity, we will look at the outputs, red, amber and then green.

Using the truth table above, we can see that red LED is only lit for steps 0 and 1, and in the following circumstances:

A = 0 and B = 0

A = 1 and B = 0

But look more closely at the table... there's a very simple pattern for the red indicator. Irrespective of what the logic value is for A, the red indicator ONLY illuminates if B = 0.

So, we are now controlling the red indicator correctly.

Next we move to the amber and once again, we look at the truth table. It would appear that the amber ALWAYS follows the logic level of the A input.

Well that's easy to do.

Next we move to the amber and once again, we look at the truth table. It would appear that the amber ALWAYS follows the logic level of the A input.

Well that's easy to do.

All we've done is connect the A input directly to the amber indicator.

Now the final indicator; green.

This is a little trickier as there is only one combination that should illuminate the green and that's when A = 0 and B = 1.

We have two choices here. If we were to NOT the A (invert it's logic level), /A = 1 AND B = 1 would illuminate the green. So, we could use a NOR gate as an inverter by connecting it's two inputs together, and feed the output of that, plus B into an AND gate and that would give us the correct control for the green. Like this:

Now the final indicator; green.

This is a little trickier as there is only one combination that should illuminate the green and that's when A = 0 and B = 1.

We have two choices here. If we were to NOT the A (invert it's logic level), /A = 1 AND B = 1 would illuminate the green. So, we could use a NOR gate as an inverter by connecting it's two inputs together, and feed the output of that, plus B into an AND gate and that would give us the correct control for the green. Like this:

Now that will work, but there is a simpler way.

But don't take my word that this works, and you should build the above and prove that it works for yourself.

Can you change the design so the sequence runs in reverse?

Now, a word of warning.

When designing logic blocks it's very easy to over engineer the solution. The previous example where I used two extra gates (an AND plus a NOR) is a classic example. You should always attempt to optimise your creation to make sure you've not missed a simple trick.

Just as an example, when I sat down to design the logic for this traffic lights scenario I first came up with this:

Can you change the design so the sequence runs in reverse?

Now, a word of warning.

When designing logic blocks it's very easy to over engineer the solution. The previous example where I used two extra gates (an AND plus a NOR) is a classic example. You should always attempt to optimise your creation to make sure you've not missed a simple trick.

Just as an example, when I sat down to design the logic for this traffic lights scenario I first came up with this:

Now this works, I know it does because I built it. It requires seven logic gates and three IC's. I was rather proud of this design. The reason this design popped up was because I concentrated on what to do at each of the four steps in the sequence, instead of concentrating on the outputs.

If my first attempt had just required two NOR gates, I would have left it at that, but when I came up with this behemoth of a design it was worth looking at the problem a different way - though to be honest I only expected to save one or two gates, not five.

The above diagram does raise a good point to do with combining logic gate outputs, and we will look at that next time.

There is of course an obvious problem with any of these traffic light solutions. If the oscillator is running at 1Hz, then the traffic light pattern changes every second. That's not a lot of time to drive though the lights. Real traffic lights spend a significant time on red (so it appears to me anyway), lesser time on green and maybe only one second when amber is part of the pattern. Without some rather complex electronics to control the oscillator speed the above design, whilst an interesting exercise, would be useless in a real-world application. We will come back to the traffic lights problem again.

If my first attempt had just required two NOR gates, I would have left it at that, but when I came up with this behemoth of a design it was worth looking at the problem a different way - though to be honest I only expected to save one or two gates, not five.

The above diagram does raise a good point to do with combining logic gate outputs, and we will look at that next time.

There is of course an obvious problem with any of these traffic light solutions. If the oscillator is running at 1Hz, then the traffic light pattern changes every second. That's not a lot of time to drive though the lights. Real traffic lights spend a significant time on red (so it appears to me anyway), lesser time on green and maybe only one second when amber is part of the pattern. Without some rather complex electronics to control the oscillator speed the above design, whilst an interesting exercise, would be useless in a real-world application. We will come back to the traffic lights problem again.