Use a Button to Execute Commands on a Raspberry Pi

Introduction: Use a Button to Execute Commands on a Raspberry Pi

I am sharing the project i did for the 2016 fll competition with the hackground team where i made a mouse trap that detected when a mouse entered and sent you an email.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Lets Go!

the first thing you need to do is download andrewH7's python code which will be perfect for what we are doing

link to andrewH7's instructable

once you download this to your raspberry pi you must edit the code so it works

Change gpio_pin_number=YOUR_CHOSEN_GPIO_NUMBER_HERE

To gpio_pin_number=21

and once you do that you can change os.system("sudo shutdown -h now")

to any command you want within the parenthesis

now you are finished all you need to do now is run the script and connect pin 21 on the gpio to ground

1 Person Made This Project!


  • Backyard Contest

    Backyard Contest
  • Silly Hats Speed Challenge

    Silly Hats Speed Challenge
  • Finish It Already Speed Challenge

    Finish It Already Speed Challenge

3 Discussions


9 months ago

This is a good script that i did use in my project "satellite antenna" and comes from Project Arthur - Apollo50. Idid put a servo and the Speaker pHat for playing a ;wav.....i am using Raspberry pi Zero W.
i did change the GPIO into: gpio_pin_number=5
I did change: os.system("sudo shutdown -h now")
into : os.system("sudo python :home/pi/")

Now, i did put the "" into the " /etc/rc.local " and it is working but just for one push on the button in front. (normaly, i think it is used for shutdown)
There is already another line above the exit 0 and it looks like :
python /home/pi/ &
python /home/pi/my_python/ &
So, two scripts are running and it is working only on the first push of the red button.
My question is: can i change the script or a script , so it is working every time i push the button in front? What must i put in the script please?
Into : '' or into '' ?

script '' below :
import pygame.mixer
from pygame.mixer import sound
from gpiozero import button
from signal import pause


btn_eagle = Button(5)

eagle = pygame.mixer.Sound("/home/pi/gpio-music-box/samples/Eagle-Has-Landed-Audio.wav")
#above in one line

btn_eagle.when_pressed =



script ' below :
import RPi.GPIO as GPIO
import time

GPIO.setup(servoPin, GPIO.OUT)
for i in range(0,90) :
for i in range(90,0,-1)


Sorry for this long question.......
Guido Monstrey Belgium

DIY Hacks and How Tos

Nice intro to Raspberry pi project. I am actually using this in a project that I am working on right now.


Reply 3 years ago