Draw State Machine with Logic Gates and D-Flip-Flops

Thread Starter

sarh124

Joined Sep 30, 2025
3
Hi! I have made two truth tables but have a hard time writing the boolean expression in order to be able to see how to draw a diagram of the state machine using logic gates and D elements. I am using Moore FSM and below are the instrctions and my truth tables. WOuld really appreciate help on how to go about writing the boolean expression so I can draw the circuit! The problem for me to understand is what truth table to use and how to write the boolean expression without it getting too long and complex of an equation. Any guidance is very much appreciated!


StateDescriptionQ2Q1Q0Danger
S000 inside the cage0000
S110 (by G1)0011
S211 (between G1 och G2)0101
S301 (byG2)0111
S400 outside 1001


Current (Q2Q1Q0)G1G2Next (Q2’Q1’Q0’)
000 (S0)00000 (S0)
000 (S0)10001 (S1)
001 (S1)10001 (S1)
001 (S1)11010 (S2)
001 (S1)00000 (S0)
010 (S2)11010 (S2)
010 (S2)01011 (S3)
010 (S2)10001 (S1)
011 (S3)01011 (S3)
011 (S3)11010 (S2)
011 (S3)00100 (S4)
100 (S4)00100 (S4)
100 (S4)01011 (S3)




Lioncage for one lion
There exists a lion pen in a zoo somewhere. The pen is composed of two parts, a cage ("lejonbur") and a larger
outside area, connected to each other by one hallway. The shape of the pen makes it difficult for the zookeeper
to know where the lion is when cleaning the outside area. To alert the zookeeper of when the lion exits the
cage, two sensors, G1 and G2, are placed in the hallway. Looking from inside the cage, G1 is placed before G2.
The sensor will send logical ones (1) when a lion is infront of it, and logical zeros (0) when it is not. A lion
leaving the cage will therefore trigger the following sequence.
(G1, G2) : 00 → 10 → 11 → 01 → 00
A lion entering the cage would instead trigger the following sequence.
(G1, G2) : 00 → 01 → 11 → 10 → 00
The above sequences only mark the switching of the sensors. In reality, the sensors will repeat each combination
many times between each step in the sequence.
We will start by solving the lioncage when there is only one lion in the pen before solving the problem for
multiple lions. All lions follow a set of rules:
• No lion can turn around when it has entered the hallway.
• No lion can dodge a sensor when walking through the hallway.
• No lion can fit its entire body between the two sensors, they are too close to each other.
• Only one lion at a time can walk through the hallway.
Note
The corresponding finite-state machine (FSM) of a state graph can be divided into two parts: combinational
logic and state memory.
An FSM starts in some state and has some input signals that are together sent through some combinational
logic K, giving an output and also a next state. The current state then changes from the previous state to
the next +
as the state memory D updates, see figure 1.
To figure out the size of the state memory we calculate the number of bits, , needed to store the number of
states the FSM has.
= ⌈2(#states)⌉
Task 1.3.2
Create a truth table for the combinational logic used to realize the state graph. Write boolean
expressions for the output functions in the truth table. Draw a diagram of the state machine using logic gates
and D-elements.
 

Thread Starter

sarh124

Joined Sep 30, 2025
3
hi sarh,
Welcome to AAC.
Is this a School or College assignment?
Moderation.
Hi, this is part of a college assignment. I’ve already made the state diagrams and two truth tables on my own, but I’m struggling with how to correctly derive and simplify the Boolean expressions for the next-state logic.

  • I have 3 D flip-flops storing the state bits: Q2,Q1,Q0
  • On each clock tick, these flip-flops load their new values: Q2’,Q1’,Q0’
  • The inputs of the flip-flops are called D2,D1,D0
  • So, mathematically:
    Q2’=D2,Q1’=D1,Q0’=D0

Danger=Q2+Q1+Q0

I think Danger is Q2+Q1+Q0, and for D2 I came up with ¬G1·¬G2·¬Q2·Q1·Q0, but I’m not sure how to derive or simplify D1 and D0 without them becoming too complex.
 

WBahn

Joined Mar 31, 2012
32,702
The first step is to fully enumerate your state transition table, which is often easiest to do after you have drawn your state diagram.

For each state, you must account for four possible combinations of G1F2, although some combinations may not be possible (in theory) in each state.
 

Thread Starter

sarh124

Joined Sep 30, 2025
3
The first step is to fully enumerate your state transition table, which is often easiest to do after you have drawn your state diagram.

For each state, you must account for four possible combinations of G1F2, although some combinations may not be possible (in theory) in each state.
Thank you I get that so regarding the state diagram I must identify the states and would really appreciate to know if I am thinking right about what the states are before moving forward with doing the truth tables ect.

The instructions says:

A lion
leaving the cage will therefore trigger the following sequence.
(G1, G2) : 00 → 10 → 11 → 01 → 00
A lion entering the cage would instead trigger the following sequence.
(G1, G2) : 00 → 01 → 11 → 10 → 00

At first, I thought that 4 states should be enough (corresponding to the sensor patterns 00, 10, 11, 01). But then I realized that I cannot distinguish between “inside” and “outside”, since both correspond to 00. That makes me think I actually need 5 states, but I’m not fully sure. How should the difference between these two “00” states (inside vs. outside) best be defined in the state graph? It deepends on if I do Moore or Mealy probably since I am doing moore FSM the output depends on only which state I am in and not the inputs (g1 and g2) so should it be five states where S0 and S4 differs in the output called Danger as you can see below.

S0: 00 Danger(output) = 0 (lion is inside cage)
S1: 10 Danger = 1
S2: 11 Danger = 1
S3: 01 Danger = 1
S4: 00 Danger = 1
 

WBahn

Joined Mar 31, 2012
32,702
How many states you need depends on what information you need to know in order to be able to interpret the inputs and make a correct decision on what to do next.

One way to do this is to consider what the most important things you need to be able to distinguish between irregardless of what the sensors say. Since you are using a Moore machine, and since the outputs are dependent only on the state, you at least need a separate state for each possible output condition. You've defined a single output, indicating "Danger" and Not Danger". So start with two states that mean those two things.

Then, ask yourself if ALL you know is that you are in the "Danger" state, can you tell whether or not you are still in the "Danger" state for all possible combinations of the sensors. If so, do the same for the "Not Danger" state. If you can do it there, then you are done. But if you can't, then add another state that represents the additional information that you need to keep track of.
 
Top