# logic circuit for 2 push buttons bitshift

I'm trying to understand how I could implement, using logic ICs (and not a uC), the following:

there are two momentary push buttons and 3 leds. The leds indicates the logic "state", that can be 0 (initial), +1 and -1. Pressing the push buttons, you change the state.

I drawn this little diagram that should help to better understand the topic.

So at the beginning you are on state 0 (and the middle led is lit), if you press +1 you go to state +1 (and only the +1 led is lit). If you press again +1 nothing happens. And so on..

How can I get that using a logic circuit? so far I only understand how to make the switches to latch using D-FlipFlop

• to be completely honest, you want a state machine, so build one ... and the way to do that would be getting the easiest/cheapest/tiniest... µC that you can find. Other than that, search online for "logic gate", "k-map", and realize that you can just "OR" together your buttons to generate a clock pulse to also drive your logic gates. Apr 4, 2017 at 12:25
• Trying to do this without a microcontroller is just silly. You can wire up a bunch of gates to perform the logic, but that will end up being larger than a microcontroller solution. The real gotcha you haven't even thought about apparently is debouncing the buttons. Your state diagram just assumes "press" events, but glosses over that those are more complicated than a button pulling a line low. A micro can do the debouncing and the state machine logic, all in the same single chip. Apr 5, 2017 at 13:17

I'd recommend abstracting things first:

• You have three states (two bits are enough to represent that). The output is three LEDs. You can directly infer which LEDs should be on from the state.
• You know about logic gates (AND, OR, NAND and so on). You thus know how to have two inputs (the two bits of the state) and three outputs (LEDs). If that's not clear, draw a truth table, and find the minterms needed to turn on the right LEDs.
• The next state only depends on the current state + one bit (namely, which one of the two buttons was pressed).

So, what you need is a

1. two flipflops to store the state
2. Logic gates to light up the LEDs corresponding to the two state bits
3. Logic gates to determine the next state based on the current state + which button has been pressed
4. a "clock" input that says "ok, now evaluate the state transition logic and go into the next state".

Let's roll it up from the bottom:

The clock is easiest. Just use an OR gate on both buttons. When either button has been pressed, the next state transition should be loaded into the fliflops:

+ button \
OR ---> Latch of flipflops
- button /


The LED logic is easy, too: for example, assume the internal stat 00 is your initial state. So you just build a NOR gate on the initial state that lights up center LED. You do that for all LEDs.

The "next state" logic is easy, too: just have the same logic as for the LEDs, but ANDed with the right button to produce the next internal state.

As Marcus indicated there really is only two latches needed for this. One for the left led and one for the right one. The centre LED would be on when neither of the other two are.

Once you come to that realization, the logic required to set and reset each latch becomes fairly simple. I'd Use tow J-K latches for this

Clock = Either button pressed delayed through two gates.

Centre Led On = Left Led Off AND Right Led Off

Button 1 Action
Set Left Led = Button1_Pressed AND Right Led Is Off
Reset Right Led

Button 2 Action
Set Right Led = Button2_Press AND Left Led Is Off
Reset Left Led


BTW: You also need to de-bounce those buttons.

simulate this circuit – Schematic created using CircuitLab