First, total noob here. Apologies in advance if this has already been addressed, but I have not found anything that specifically answers my question, yet.

I have some experience with structured text programming in CoDeSys and Visual Basic, and interested in learning GPIO and Python. The first foray into Pi world was with my home network, using two Pi 4's to run Pi-hole (love it, btw). Pi's are running 24/7 as DNS servers 1 & 2.

I want to use one of them to also operate a door lock via combination button press. One sequence would activate the output if all buttons were pressed simultaneously, and another alternative would activate the output only if the buttons were pressed in a specific order. Not sure if the latter is feasible or not, but would like to give it a try. Thinking the timing would need specific pauses to reset the sequence if the wrong button was pressed, but I'll defer to the experts here for that.

EDITED: added code for one button, but having difficulty figuring out how to nest the other buttons in the code as a condition for activating the output.

Credit to Paul McWhorter for the code to get me going. enter image description here

Thank you in advance for your help!

  • sequential ... do not nest if statements .... if state == 0 and buton1 then state = 1 , if state == 1 and button2 then state = 2 else state = 0 .... etc ..... research FSM (finite state machine)
    – jsotola
    Dec 2, 2020 at 5:37

1 Answer 1


No one will write your code for you, but will help if you try and have problems.

I suggest you start with something simpler - using 1 button.

See https://gpiozero.readthedocs.io/en/stable/recipes.html#button which has a number of excellent examples.

You should try some simple Python first - See https://www.raspberrypi.org/documentation/usage/python/README.md

The Foundation has a number of publications.

If you start with 2 buttons you could increment a state variable by 1 if button 1 pressed, decrement the state variable by 1 if button 1 released. Do similar for button 2 (±2) -this will give you a state 0-3. This could be expanded for more buttons, so you will know which buttons are pressed.

You could do something similar to detect the order buttons are pressed using a state machine.

  • I found an modified code for one button, but having trouble figuring out how to nest the buttons together. I have looked through the Python programming docs and can't find clear direction on how to require the buttons to be pressed simultaneously in order to activate the output. Waiting on buttons and an LED to test the code once its done. Added code to original question.
    – numan
    Dec 3, 2020 at 3:11
  • @numan You are going about this the wrong way (it is possible, but very complicated and unreliable to test button states). You should use the Button.when_pressed (and possibly Button.when_released) to define callbacks. These can be used to drive a state machine. What is missing from your Question is a definition of the possible states, and the transitions between them. I would suggest you avoid the "if all buttons were pressed simultaneously" condition as this has serious timing problems (how much delay is permissible between presses, as there will always be some).
    – Milliways
    Dec 3, 2020 at 3:54
  • @numan See gpiozero.readthedocs.io/en/stable/… for examples of callbacks.
    – Milliways
    Dec 3, 2020 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.